-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1666-building-roads.cpp
82 lines (65 loc) · 1.45 KB
/
1666-building-roads.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define ll long long
#define vi vector<int>
#define vl vector<long long>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define umap unordered_map
#define uset unordered_set
#define rall(x) (x).rbegin(), (x).rend()
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mp make_pair
const long long LLINF = LLONG_MAX;
const int INF = INT_MAX;
const int MOD = 1e9 + 7;
vector<vi> adj;
vector<bool> visited;
void dfs(int v) {
visited[v] = true;
for (int u : adj[v]) {
if (!visited[u]) {
dfs(u);
}
}
}
void solve() {
int n, m;
cin >> n >> m;
adj.resize(n);
visited.assign(n, false);
vector<pii> roads;
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
a--;
b--;
adj[a].pb(b);
adj[b].pb(a);
roads.pb({a, b});
}
vector<pii> new_roads;
for (int i = 0; i < n; i++) {
if (!visited[i]) {
if (i > 0) {
new_roads.pb({i - 1, i});
}
dfs(i);
}
}
cout << new_roads.size() << "\n";
for (auto road : new_roads) {
cout << road.first + 1 << " " << road.second + 1 << "\n";
}
}
int main() {
fastio
int tc = 1; // Number of test cases
// cin >> tc;
while (tc--) {
solve();
}
return 0;
}