Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <queue>
- #define first ff
- #define second ss
- const int maxN = 1e5 + 1;
- using namespace std;
- int n,m;
- int root[maxN];
- int getRoot(int i)
- {
- if(root[i] == i)
- return i;
- return root[i] = getRoot(root[i]);
- }
- int main()
- {
- freopen("DSF.inp","r",stdin);
- freopen("DSF.out","w",stdout);
- int x,y;
- cin >> n >> m;
- for(int i = 1; i <= n; ++i)
- root[i] = i;
- int cnt = n;
- for(int i = 1; i <= m; ++i)
- {
- cin >> x >> y;
- int xRoot = getRoot(x);
- int yRoot = getRoot(y);
- if(xRoot != yRoot)
- {
- --cnt;
- root[xRoot] = yRoot;
- }
- cout << cnt << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement