-
Notifications
You must be signed in to change notification settings - Fork 2k
/
BuildRoads.cpp
54 lines (46 loc) · 1.05 KB
/
BuildRoads.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
#include <iostream>
#include <vector>
#include<list>
using namespace std;
#define ll long long int
#define OJ \
freopen("input.txt", "r", stdin); \
freopen("output.txt", "w", stdout);
#define FIO \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
bool visited[100001] = {false};
list<int> adj[100001];
void dfs(int index, list<int> *adj){
visited[index] = true;
for(auto a: adj[index]){
if(visited[a] == false){
dfs(a, adj);
}
}
}
int main()
{
OJ;
FIO;
int n, r;
cin >> n >> r;
int a, b;
for(int i=0; i<r; i++){
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
vector<long long int> ans;
for(int i=1; i<=n; i++){
if(visited[i] == false){
ans.push_back(i);
dfs(i, adj);
}
}
cout << ans.size()-1 << endl;
for(int i=1; i<ans.size(); i++){
cout << ans[i] << " " << ans[i]-1 << endl;
}
}