41 lines
791 B
C++
41 lines
791 B
C++
// Building Roads
|
|
|
|
#include<bits/stdc++.h>
|
|
|
|
using namespace std;
|
|
|
|
void dfs(vector<vector<int>> &g, int s, vector<bool> &v) {
|
|
v[s] = true;
|
|
for (auto e: g[s]) {
|
|
if (!v[e]) dfs(g, e, v);
|
|
}
|
|
}
|
|
|
|
|
|
int main() {
|
|
int n, m; cin >> n >> m;
|
|
vector<vector<int>> g(n);
|
|
for (int i{}; i < m; i++) {
|
|
int a, b;
|
|
cin >> a >> b;
|
|
a--; b--;
|
|
g[a].push_back(b);
|
|
g[b].push_back(a);
|
|
}
|
|
int count = -1;
|
|
vector<pair<int, int>> roads;
|
|
vector<bool> v(n);
|
|
for (int i{}; i < n; i++) {
|
|
if (v[i]) continue;
|
|
count ++;
|
|
dfs(g, i, v);
|
|
if (i > 0) roads.push_back({i, i - 1});
|
|
}
|
|
|
|
cout << count << endl;
|
|
for (auto [x, y]: roads) {
|
|
cout << x+1 << " " << y + 1 << endl;
|
|
}
|
|
return 0;
|
|
}
|