sacgajcvs

DSU

Dec 6th, 2019
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.33 KB | None | 0 0
  1. ll high[N];
  2. ll p[N]; // set p of i equal to i initially
  3. ll find(ll x)
  4. {
  5. if(p[x]!=x)
  6. p[x]=find(p[x]);
  7. return p[x];
  8. }
  9. void merge(ll x,ll y)
  10. {
  11. ll Px,Py;
  12. Px=find(x);
  13. Py=find(y);
  14. if(high[Px]>high[Py])
  15. p[Py]=Px;
  16. else
  17. p[Px]=Py;
  18. if(high[Px]==high[Py])
  19. high[Py]++;
  20. }
Advertisement
Add Comment
Please, Sign In to add comment