Advertisement
newb_ie

DSU

Aug 23rd, 2021
1,051
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.27 KB | None | 0 0
  1. const int maxN = 200010;
  2. int p[maxN],r[maxN];
  3.  
  4. int __find__ (int a) {
  5.     return p[a] = (p[a] == a ? a : __find__(p[a]));
  6. }
  7.  
  8. void __union__ (int a,int b) {
  9.     a = __find__(a);
  10.     b = __find__(b);
  11.     if (r[a] == r[b]) ++r[a];
  12.     if (r[a] > r[b]) p[b] = a;
  13.     else p[a] = b;
  14. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement