-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdisjoint_set.cpp
43 lines (38 loc) · 1.14 KB
/
disjoint_set.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
// a disjoint-set data structure, also called a union–find data structure or
// merge–find set, is a data structure that keeps track of a set of elements
// partitioned into a number of disjoint (nonoverlapping) subsets.
// Disjoint-set forests
// The first improvement: union by rank
void MakeSet(DataType x) {
x.parent = x;
x.rank = 0;
}
void Union(DataType x, DataType y) {
DataType xRoot = Find(x);
DataType yRoot = Find(y);
if (xRoot == yRoot) return;
// x and y are not already in same set. Merge them.
if (xRoot.rank < yRoot.rank)
xRoot.parent = yRoot;
else if (xRoot.rank > yRoot.rank)
yRoot.parent = xRoot;
else {
yRoot.parent = xRoot;
xRoot.rank = xRoot.rank + 1;
}
}
// The second improvement: path compression
DataType Find(DataType x) {
if (x.parent != x) x.parent = Find(x.parent);
return x.parent;
}
//常用比赛代码
int parent[MAXN + 1];
void initial() {
for (int i = 1; i <= MAXN; i++) parent[i] = i;
return;
}
int Find(int n) {
if (parent[n] != n) parent[n] = Find(parent[n]);
return parent[n];
}