-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisjoint-set.cpp
49 lines (42 loc) · 931 Bytes
/
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
44
45
46
47
48
49
#ifndef DISJOINT_SET_CPP
#define DISJOINT_SET_CPP
#include <vector>
class DisjointSet {
std::vector<unsigned int> parent;
std::vector<unsigned int> noChildren;
public:
DisjointSet(unsigned int _size = 0) {
parent.push_back(0);
noChildren.push_back(0);
for(unsigned int i=1;i<=_size;i++) {
parent.push_back(i);
noChildren.push_back(1);
}
}
unsigned int makeSet() {
unsigned int id = parent.size();
parent.push_back(id);
noChildren.push_back(1);
return id;
}
unsigned int find(unsigned int a) {
if(parent[a] != a) parent[a] = find(parent[a]);
return parent[a];
}
void merge(unsigned int a, unsigned int b) {
a = find(a);
b = find(b);
if(a==b) return;
if(noChildren[a] < noChildren[b]) {
parent[a] = b;
noChildren[b] += noChildren[a];
} else {
parent[b] = a;
noChildren[a] += noChildren[b];
}
}
unsigned int size() const {
return parent.size()-1;
}
};
#endif