Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int n, m;
- vector<int> adj[100001];
- vector<int> start;
- bool visited[100001];
- void dfs(int node){
- if(visited[node]) return;
- visited[node] = true;
- for(auto v: adj[node]){
- if(!visited[v]){
- dfs(v);
- }
- }
- }
- int main(){
- //freopen("buildingroads.in", "r", stdin);
- cin >> n >> m;
- for(int i=0; i<m; i++){
- int a, b;
- cin >> a >> b;
- adj[a].push_back(b);
- adj[b].push_back(a);
- }
- for(int i=1; i<=n; i++){
- if(!visited[i]){
- start.push_back(i);
- dfs(i);
- }
- }
- cout << start.size()-1 << '\n';
- for(int i=1; i<start.size(); i++){
- cout << start[0] << " " << start[i] << '\n';
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement