-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdsu.cpp
More file actions
70 lines (63 loc) · 1.17 KB
/
dsu.cpp
File metadata and controls
70 lines (63 loc) · 1.17 KB
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
#include <bits/stdc++.h>
using namespace std;
#define ll long long
// @author: __Taohid
class DSU {
vector<int>rank, parent, size;
public:
DSU(int n) {
rank.resize(n + 1, 1);
parent.resize(n + 1);
size.resize(n + 1, 1);
for (int i = 0; i <= n; i++) {
parent[i] = i;
}
}
int findPar(int node) {
if (node == parent[node]) return node;
return parent[node] = findPar(parent[node]);
}
void unionByRank(int u, int v) {
u = findPar(u);
v = findPar(v);
if (u != v) {
if (rank[u] < rank[v]) {
swap(u, v);
}
parent[v] = u;
rank[u] += rank[v];
}
}
void unionBySize(int u, int v) {
u = findPar(u);
v = findPar(v);
if (u != v) {
if (size[u] < size[v]) {
swap(u, v);
}
parent[v] = u;
size[u] += size[v];
}
}
};
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
DSU ds(7);
ds.unionBySize(1, 2);
ds.unionBySize(2, 3);
ds.unionBySize(4, 5);
ds.unionBySize(6, 7);
ds.unionBySize(5, 6);
// if 3 and 7 same or not
if (ds.findPar(3) == ds.findPar(7)) {
cout << "Same\n";
}
else cout << "Not Same\n";
ds.unionBySize(3, 7);
if (ds.findPar(3) == ds.findPar(7)) {
cout << "Same\n";
}
else cout << "Not Same\n";
return 0;
}