Advertisement
_Tucha

DSU example

Nov 14th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.28 KB | None | 0 0
  1.  
  2. void create(int v) {
  3.     p[v] = v;
  4.     sz[v] = 1;
  5. }
  6.  
  7. int find(int v) {
  8.     if(p[v] == v)
  9.         return v;
  10.     return p[v] = find(p[v]);
  11. }
  12.  
  13. void unite(int a, int b) {
  14.     a = find(a);
  15.     b = find(b);
  16.    
  17.     if(a != b) {
  18.         if(sz[a] < sz[b])
  19.             swap(a, b);
  20.            
  21.         p[b] = a;
  22.         sz[a] += sz[b];
  23.     }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement