-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroads_construct.cpp
More file actions
46 lines (35 loc) · 801 Bytes
/
Copy pathroads_construct.cpp
File metadata and controls
46 lines (35 loc) · 801 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
#include <bits/stdc++.h>
using namespace std;
void dfs(int s, vector<int> adj[], vector<int>& vis) {
vis[s] = 1;
for (int n_node : adj[s]) {
if (vis[n_node]) continue;
dfs(n_node, adj, vis);
}
}
int main() {
int n, m;
cin >> n >> m;
vector<int> adj[n + 1];
int u, v;
for (int i = 0; i < m; i++) {
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
vector<int> vis(n + 1, 0);
dfs(1, adj, vis);
int nos = 0;
vector<int> roads;
for (int i = 2; i <= n; i++) {
if (vis[i]) continue;
nos++;
roads.push_back(i);
dfs(i, adj, vis);
}
cout << nos << endl;
for (int i = 0; i < nos; i++) {
cout << "1 " << roads[i] << endl;
}
return 0;
}