Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct var{
- int x, y;
- } mu[500001];
- int t[100001], h[100001], c1, c2, m, n, sol[500001];
- int Find(int x)
- {
- if(t[x]!=0)
- return Find(t[x]);
- return x;
- }
- void Union(int x,int y)
- {
- if(h[x]==h[y])
- {
- t[x]=y;
- h[y]++;
- }
- else
- if(h[x]<h[y])
- t[x]=y;
- else
- t[y]=x;
- }
- int x, y;
- int main() {
- cin >> n >> m;
- for(int i = 1; i <= m; ++i) {
- cin >> mu[i].x >> mu[i].y;
- }
- for(int i = m; i >= 1; --i) {
- sol[i] = n;
- c1 = Find(mu[i].x);
- c2 = Find(mu[i].y);
- if(c1 != c2)
- --n, Union(c1, c2);
- }
- for(int i = 1; i <= m; ++i)
- cout << sol[i] << '\n';
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement