-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdisjoint_setU.cpp
More file actions
76 lines (57 loc) · 904 Bytes
/
disjoint_setU.cpp
File metadata and controls
76 lines (57 loc) · 904 Bytes
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
class DSU
{
vector<int> par;
vector<int> rank;
int total_compo;
public:
void init(int n)
{
par.resize(n);
rank.resize(n);
total_compo=n;
for(int i=0;i<n;i++)
{
par[i]=i;
rank[i]=1;
}
}
int get_superParent(int child)
{
if(child == par[child])
{
return child;
}
return par[child]=get_superParent(par[child]);//path compression
}
void make_union(int x,int y)
{
int x_par=get_superParent(x);
int y_par=get_superParent(y);
if(x_par==y_par)
return;
if(rank[x_par]>rank[y_par])
{
par[y_par]=x_par;
rank[x_par]+=rank[y_par];
}
else
{
par[x_par]=y_par;
rank[y_par]+=rank[x_par];
}
total_compo--;
}
};
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
DSU d;
d.init(6);
return 0;
}