Explorar el Código

change union find

Olivier Marty hace 8 años
padre
commit
0fea1cf8f8
Se han modificado 1 ficheros con 5 adiciones y 3 borrados
  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); }