-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDSU.cpp
More file actions
49 lines (38 loc) · 890 Bytes
/
DSU.cpp
File metadata and controls
49 lines (38 loc) · 890 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
// 1 based indexing
struct DSU{
vll p;
ll n , connected;
vll sz;
void init(ll n){
p.resize(n+1);
iota(p.begin(), p.end(), 0);
sz.assign(n+1, 1);
connected = n;
}
ll get(ll x) {
if(x == p[x]){
return x;
}
return p[x]=get(p[x]);
}
ll getsz(ll u)
{
return sz[get(u)];
}
bool unite(int x, int y) {
x = get(x);
y = get(y);
if(x == y) return false;
connected--;
if(sz[x] > sz[y])
swap(x, y);
sz[y] += sz[x];
sz[x] = 0;
p[x] = p[y];
return true;
}
};
// Question : https://codeforces.com/problemset/problem/1609/D
// soln: https://codeforces.com/contest/1609/submission/174662908
//Question: https://codeforces.com/gym/103055/problem/G (2D DSU)
//soln : https://codeforces.com/gym/103055/submission/199426552