Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int N = 100100;
- vector <vector <int>> g(N);
- bool used[N];
- void dfs(int v) {
- used[v] = true;
- for (int i = 0; i < g[v].size(); ++i) {
- int u = g[v][i];
- if (!used[u]) {
- dfs(u);
- }
- }
- }
- signed main() {
- ios_base::sync_with_stdio(false);
- cin.tie(NULL);
- int n, m; cin >> n >> m;
- for (int i = 0; i < m; ++i) {
- int u1, u2; cin >> u1 >> u2;
- g[u1].push_back(u2);
- g[u2].push_back(u1);
- }
- int connect = 0;
- for (int i = 1; i <= n; ++i) {
- if (!used[i]) {
- dfs(i);
- connect++;
- }
- }
- cout << connect << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement