-
Notifications
You must be signed in to change notification settings - Fork 5
/
DisjointSetUnit.cpp
58 lines (49 loc) · 968 Bytes
/
DisjointSetUnit.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
52
53
54
55
56
57
58
/*
@data_structure : Disjoint Set Unit (DSU)
@implementation : Kruskal's MST, Tarjan's offline LCA
@author : Amirul Islam
*/
#include <bits/stdc++.h>
using namespace std;
const int mx = 1e5+5;
int par[mx];
// return the representation of r
int Find(int r) {
if (par[r] == r) return r;
return par[r] = Find(par[r]);
}
// make Union
void Union(int u, int v) {
par[u] = v;
}
// at the beginning, everyone's representative is it itself
void init(int n) {
for (int i = 1; i <= n; i++) par[i] = i;
}
int main() {
//freopen("in", "r", stdin);
int node, relation, a, b;
scanf("%d %d", &node, &relation);
init(node);
while (relation--) {
scanf("%d %d", &a, &b);
int u = Find(a);
int v = Find(b);
if (u == v) printf("%d and %d are already friend\n", a, b);
else Union(u, v);
}
return 0;
}
/*
Input:
3 5
1 2
2 3
1 2
3 2
1 3
Output:
1 and 2 are already friend
3 and 2 are already friend
1 and 3 are already friend
*/