-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconcomp1.cpp
50 lines (39 loc) · 910 Bytes
/
concomp1.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
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pi = pair<ll, ll>;
using vi = vector<ll>;
using vpi = vector<pi>;
using vb = vector<bool>;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n, m;
cin >> n >> m;
vector<set<ll>> ed(n);
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
a--, b--;
ed[a].insert(b);
ed[b].insert(a);
}
vb vis(n);
function<void(int)> dfs = [&](int i) {
if (vis[i]) return;
vis[i] = true;
for (int ch : ed[i]) dfs(ch);
};
vi path;
for (int i = 0; i < n; i++) {
if (!vis[i]) {
dfs(i);
path.push_back(i);
}
}
cout << path.size() - 1 << endl;
for (int i = 1; i < path.size(); i++) {
cout << path[i - 1] + 1 << " " << path[i] + 1 << endl;
}
return 0;
}