-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathUnionFind.cpp
47 lines (43 loc) · 857 Bytes
/
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
struct UnionFind
{
VI dad, mass;
int N;
UnionFind(int _N)
{
N = _N;
dad.resize(N);
mass.resize(N);
forall(i, 0, N)
dad[i] = -1, mass[i] = 1;
}
int find(int a)
{
if(dad[a] < 0) return a;
return dad[a] = find(dad[a]);
}
void merge(int a, int b)
{
int c1 = find(a), c2 = find(b);
if(c1 != c2)
{
N--;
if(mass[c1] < mass[c2]) swap(c1, c2);
mass[c1] += mass[c2];
dad[c2] = c1;
}
}
int size()
{
return N;
}
};
int main()
{
UnionFind uf(20);
for(int i = 0; i < 20; i++)
for(int j = 0; j < 20; j++)
if( i + j == 30) uf.merge(i,j);
int heads = uf.size();
for(int i = 0; i < N; i++)
cout << uf.find(i) << endl;
}