-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnion.cpp
More file actions
62 lines (58 loc) · 1.19 KB
/
Copy pathUnion.cpp
File metadata and controls
62 lines (58 loc) · 1.19 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
#include<bits/stdc++.h>
using namespace std;
set<int>s;
int parent[1005], _size[1005];
void make_set(int v)
{
for(int i = 1; i < v+1; i ++){
parent[i] = i; _size[i] = 1;
//cout << i << " " << parent[i] <<endl;
}
}
int find_set(int i)
{
while(i != parent[i]) i = parent[i];
return i;
}
void union_set(int i, int j)
{
int a = find_set(i); int b = find_set(j);
if(_size[a] < _size[b]) swap(a, b);
if(a == 1 ){
parent[b] = a;
} else if (b == 1) {
parent[a] = b;
} else {
// cout << a << b << " ";
parent[a] = b;
//cout << parent[a] <<" " <<parent[b]<<endl;
_size[a] += _size[b];
}
}
int main()
{
int house, num_conn;
cin >> house >> num_conn;
make_set(house);
int a,b;
for(int i = 0; i < num_conn; i++){
cin >> a >> b;
union_set(a,b);
//cout << i << " ";
}
for(int i = 1; i < house+1; i++){
cout << parent[i] << " ";
s.insert(parent[i]);
}
cout << endl;
if(s.size() == 1){
cout << "connected" <<endl;
}else{
for(int i = 1; i < house + 1; i++){
if(parent[i] != 1){
cout << i << endl;
}
}
}
return 0;
}