-
Notifications
You must be signed in to change notification settings - Fork 0
/
UFDS.cpp
62 lines (57 loc) · 1.5 KB
/
UFDS.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
59
60
61
62
#include <bits/stdc++.h>
int n, m, pSet[105], wrote[105];
std::vector <int> a[105];
void initUFDS(int q){
for (int i = 1; i <= q; ++i) pSet[i] = i;
}
int findSet(int i){
return (pSet[i] == i ? i : findSet(pSet[i]));
}
void fusionUFDS(int i, int j){
pSet[findSet(i)] = findSet(j);
}
int main(){
/*FILE*/
#ifndef ONLINE_JUDGE
freopen("UFDS.inp", "r", stdin);
freopen("UFDS.out", "w", stdout);
#endif // ONLINE_JUDGE
/*READ*/
scanf("%d%d", &n, &m);
/*PROCESS*/
initUFDS(n);
for (int i = 0; i < m; ++i){
int temp1, temp2;
scanf("%d%d", &temp1, &temp2);
fusionUFDS(temp1, temp2);
}
for (int i = 1; i <= n; ++i){
pSet[i] = findSet(i);
a[pSet[i]].push_back(i);
}
/*WRITE*/
/*This way is very unconvinient use the way below*/
/*for (int i = 1; i <= n; ++i){
if (!wrote[i]){
for (int j = 1; j <= n; ++j){
if (pSet[i] == pSet[j]){
wrote[j] = true;
printf("%d ", j);
}
}
printf("\n");
}
}*/
for (int i = 1; i <= n; ++i){
printf("pSet[%d]: ", i);
for (int j = 0; j < a[i].size(); ++j){
printf("%d ", a[i][j]);
}
printf("\n");
}
/*TESTING ZONE
printf("\nTESTING ZONE\n");
printf("\npSet[1] = %d", pSet[1]);
printf("\npSet[2] = %d", pSet[2]);*/
return 0;
}