-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDSU.cpp
51 lines (45 loc) · 1.01 KB
/
DSU.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
50
51
/* find_set(v) : This operation will return the id of the representative of the group in which v is present
union_set(u, v) : This operation will join the groups if u and v are present in different groups
make_set(v) : This will make a new group containing single node v */
#include<bits/stdc++.h>
using namespace std;
int par[100000]; //index->node no and value->parent of that node
int sz[1000000]; //index->node no and value->size of group in which node is present
int connected;
void make_set (int v) {
par[v] = v;
sz[v] = 1;
}
void init( int n ) {
for (int i=1; i<=n; i++) {
make_set(i);
}
connected = n;
}
int find_set (int v) {
if (v == par[v])
return v;
return( par[v] = find_set(par[v]) );
}
void union_set (int u, int v) {
u = find_set(u);
v = find_set(v);
if (u != v) {
connected--;
if (sz[u] <= sz[v]) {
par[v] = u;
sz[u] += sz[v];
sz[v] = 0;
}
else {
par[u] = v;
sz[v] += sz[u];
sz[u] = 0;
}
}
}
int main() {
make_set(1);
cout << find_set(1) << endl;
return 0;
}