Browse Source

change union find

Olivier Marty 8 years ago
parent
commit
0fea1cf8f8
1 changed files with 5 additions and 3 deletions
  1. 5 3
      code/UnionFind.cc

+ 5 - 3
code/UnionFind.cc

@@ -1,3 +1,5 @@
-//union-find set: the vector/array contains the parent of each node
-int find(vector <int>& C, int x){return (C[x]==x) ? x : C[x]=find(C, C[x]);} //C++
-int find(int x){return (C[x]==x)?x:C[x]=find(C[x]);} //C
+// union-find set: the array contains the parent of each node
+// path compression but no balancing
+int make(int *C, int n) { for(int i = 0; i < n; i++) C[i]=i; }
+int find(int *C, int x) { return (C[x]==x) ? x : C[x]=find(C, C[x]); }
+void unite(int *C, int x, int y) { C[find(x)] = find(y); }