aarshee's blog

By aarshee, history, 9 years ago, In English

What is the algorithm for merging two different sets ?

inline int find(int x){ return (parent[x] == x ? x : find(parent[x])); }

inline bool merge(int x, int y){ x = find(x); y = find(y); if(x == y) return false; if(r[x] > r[y]) x = y + x — (y = x); parent[x] = y; if(r[x] == r[y]) r[y]++; return true; }

Thanks in advance.

  • Vote: I like it
  • 0
  • Vote: I do not like it

»
9 years ago, # |
  Vote: I like it +1 Vote: I do not like it

This is the implementation of disjoint-set union.