-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuildingRoads.cpp
More file actions
52 lines (39 loc) · 871 Bytes
/
Copy pathbuildingRoads.cpp
File metadata and controls
52 lines (39 loc) · 871 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
50
51
52
#include <bits/stdc++.h>
using namespace std;
int parent[100001];
void init() {
for(int i = 0; i <= 100000; i++)
parent[i] = i;
}
int find(int x) {
if(parent[x] == x)
return x;
return parent[x] = find(parent[x]);
}
int main() {
init();
int n, m;
cin >> n >> m;
for(int i = 1; i <= m; i++) {
int x, y;
cin >> x >> y;
int p1 = find(x);
int p2 = find(y);
if(p1 != p2)
parent[p2] = p1;
}
vector<int> nodes;
for(int i = 1; i <= n; i++) {
if(parent[i] == i) {
// cout << i << endl;
nodes.push_back(i);
}
}
int roads = nodes.size();
roads--;
cout << roads << endl;
for(int i = 0; i < roads; i++) {
cout << nodes[i] << " " << nodes[i+1] << endl;
}
return 0;
}