-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1666 - Building Roads.cpp
More file actions
46 lines (39 loc) · 934 Bytes
/
Copy path1666 - Building Roads.cpp
File metadata and controls
46 lines (39 loc) · 934 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;
int n, m;
pair<vector<int>, bool> v[100000 + 1];
vector<vector<int>> scc;
void call(int city) {
v[city].second = true;
for (auto i : v[city].first) {
if (!v[i].second) {
scc.back().push_back(i);
call(i);
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin >> n >> m;
for (int i = 0; i < m; ++i) {
pair<int, int> r;
cin >> r.first >> r.second;
v[r.first].first.push_back(r.second);
v[r.second].first.push_back(r.first);
}
for (int i = 1; i <= n; ++i) {
if (!v[i].second) {
auto *s = new vector<int>;
scc.push_back(*s);
scc.back().push_back(i);
call(i);
}
}
cout << scc.size() - 1 << "\n";
for (int i = 1; i < scc.size(); ++i) {
cout << scc[i][0] << " " << scc[i - 1][0] << "\n";
}
return 0;
}