Advertisement
Guest User

Untitled

a guest
Apr 10th, 2020
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <queue>
  4. #define first ff
  5. #define second ss
  6. const int maxN = 1e5 + 1;
  7. using namespace std;
  8.  
  9. int n,m;
  10. int root[maxN];
  11.  
  12. int getRoot(int i)
  13. {
  14.     if(root[i] == i)
  15.         return i;
  16.     return root[i] = getRoot(root[i]);
  17. }
  18.  
  19. int main()
  20. {
  21.     freopen("DSF.inp","r",stdin);
  22.     freopen("DSF.out","w",stdout);
  23.  
  24.     int x,y;
  25.     cin >> n >> m;
  26.  
  27.     for(int i = 1; i <= n; ++i)
  28.         root[i] = i;
  29.  
  30.     int cnt = n;
  31.     for(int i = 1; i <= m; ++i)
  32.     {
  33.         cin >> x >> y;
  34.  
  35.         int xRoot = getRoot(x);
  36.         int yRoot = getRoot(y);
  37.  
  38.         if(xRoot != yRoot)
  39.         {
  40.             --cnt;
  41.             root[xRoot] = yRoot;
  42.         }
  43.  
  44.         cout << cnt << endl;
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement