Code 404
UnionFind : C++
Unify()
void unify(int p, int q) {
int root1 = find(p);
int root2 = find(q);
if (root1 == root2)
return;
if (sz[root1] < sz[root2]) {
sz[root2] += sz[root1];
id[root1] = root2;
} else {
sz[root1] += sz[root2];
id[root2] = root1;
}
numComponents--;
}
Find()
int find(int p) {
int root = p;
while (root != id[root])
root = id[root];
while (p != root) {
int next = id[p];
id[p] = root;
p = next;
}
return root;
}