Advertisement
Riz1Ahmed

Disjoint Set

Feb 19th, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.46 KB | None | 0 0
  1. #include <cstdio>
  2. using namespace std;
  3. int r[100005], p[100005]; //r=Child,p=Parent
  4. int findp(int x) {
  5.     return p[x] == x ? x : (p[x]=findp(p[x]));
  6. }
  7. int main() {
  8.     int n,e,u,v,i,ans;
  9.     scanf("%d %d", &n,&e);
  10.     int size=0;
  11.     for (i=0; i<=n; i++) r[i]=1, p[i]=i; ///Itself p.
  12.     while (e--) {
  13.         scanf("%d %d",&u,&v);
  14.         int pu=findp(u), pv=findp(v);
  15.         if (pu!=pv) r[pu]+=r[pv], p[pv]=pu;
  16.         ans=r[pu];
  17.         printf("Total Connected Node of %d is %d.\n",u,ans);
  18.     }
  19.     return 0;
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement