Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include "Animal.cpp"
- #include <algorithm>
- #include <vector>
- using namespace std;
- vector<bool> used;
- vector<vector<int>> g;
- void dfs(int from) {
- used[from] = true;
- for (auto to : g[from]) {
- if (!used[to]) dfs(to);
- }
- }
- int main() {
- int n, m; //n -- кол-во вершин, m -- кол-во ребер
- cin >> n >> m;
- g.resize(n);
- for (int i = 0; i < m; i++) {
- int x, y;
- cin >> x >> y;
- x--, y--;
- g[x].push_back(y);
- g[y].push_back(x);
- }
- used.assign(n, false);
- int cnt = 0;
- for (int cur = 0; cur < n; cur++) {
- if (!used[cur]) {
- dfs(cur);
- cnt++;
- }
- }
- cout << cnt;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement