-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathunionFind.sublime-snippet
49 lines (43 loc) · 1.18 KB
/
unionFind.sublime-snippet
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
<snippet>
<content><![CDATA[
class UnionFind {
public:
vector<int> group;
vector<int> rank;
UnionFind(int size) {
group = vector<int>(size);
rank = vector<int>(size);
for (int i = 0; i < size; ++i) {
group[i] = i;
}
}
int find(int node) {
if (group[node] != node) {
group[node] = find(group[node]);
}
return group[node];
}
bool join(int node1, int node2) {
int group1 = find(node1);
int group2 = find(node2);
// node1 and node2 already belong to same group.
if (group1 == group2) {
return false;
}
if (rank[group1] > rank[group2]) {
group[group2] = group1;
} else if (rank[group1] < rank[group2]) {
group[group1] = group2;
} else {
group[group1] = group2;
rank[group2] += 1;
}
return true;
}
};
]]></content>
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
<tabTrigger>UnionFind</tabTrigger>
<!-- Optional: Set a scope to limit where the snippet will trigger -->
<!-- <scope>source.python</scope> -->
</snippet>