Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "bits/stdc++.h"
- using namespace std;
- const int maxN = 1e5 + 100;
- vector<int> adj[maxN];
- //~ adj[1] = {2,3}
- bool visited[maxN];
- void dfs (int node) {
- //~ cout << "ENTERING node " << node << '\n';
- visited[node] = true;
- for (int i = 0; i < (int) adj[node].size(); ++i) {
- int child = adj[node][i];
- if (visited[child] == false) {
- dfs(child);
- }
- }
- //~ cout << "EXIT from " << node << '\n';
- }
- int main () {
- ios::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- int n, m;
- cin >> n >> m;
- for (int i = 1; i <= m; ++i) {
- int u,v; //u -> v and v -> u
- cin >> u >> v;
- adj[u].push_back(v);
- adj[v].push_back(u);
- }
- int cc = 0;
- for (int i = 1; i <= n; ++i) {
- if (visited[i] == false) {
- dfs(i);
- ++cc;
- }
- }
- cout << cc << '\n';
- }
- //~ 7 6
- //~ 1 2
- //~ 1 3
- //~ 2 4
- //~ 2 5
- //~ 3 6
- //~ 3 7
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement