-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathUnionFind.cpp
50 lines (43 loc) · 1.34 KB
/
UnionFind.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
class UnionFind {
public:
UnionFind(int count) {
this->count = count;
this->rank = new int[count];
this->parent = new int[count];
for (int i = 0; i < count; ++i) {
parent[i] = i; /* firstly, make the ith node point to itself. */
rank[i] = 1; /* the number of layers of the ith node is 1 as default */
}
}
~UnionFind() {
delete [] rank;
delete [] parent;
}
int find(int p) {
while (p != parent[p]) {
parent[p] = parent[parent[p]];
p = parent[p];
}
return p;
}
void union_(int p, int q) {
int rootP = find(p);
int rootQ = find(q);
if (rootP == rootQ) return;
if (rank[rootP] < rank[rootQ]) parent[rootP] = rootQ;
else if (rank[rootP] > rank[rootQ]) parent[rootQ] = rootP;
else {
parent[rootP] = rootQ;
rank[rootP] += 1;
}
}
bool isConnected(int p, int q) {
return find(p) == find(q);
}
private:
int * rank; /* rank[i] represents the number of layers of the tree
represented by the set whith i as the root. */
int * parent; /* parent[i] represents the parent node pointed by the ith element */
int count; /* total number of elements */
};