Skip to content

Commit

Permalink
AC Code
Browse files Browse the repository at this point in the history
  • Loading branch information
theadanielskocher authored Aug 10, 2022
1 parent bcf43ce commit 6f60f15
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions cses1666.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include<bits/stdc++.h>
using namespace std;

#define int long long
const int maxN = 1e5 + 5;

vector<int>nodes[maxN];
vector<int>result;
vector<bool>vis(maxN, false);


void dfs (int node)
{
vis[node] = true;
for (auto v : nodes[node])
{
if (vis[v] == false)
{
dfs(v);
}
}
}

signed main()
{
int n, m;
cin >> n >> m;
for (int i = 1; i <= m; i++)
{
int u, v;
cin >> u >> v;
nodes[u].push_back(v);
nodes[v].push_back(u);
}
for (int i = 1; i <= n; i++)
{
if (vis[i] == false)
{
result.push_back(i);
dfs(i);
}
}
cout << result.size() - 1 << endl;
for (int i = 0; i < result.size() - 1; i++) {
cout << result[i] << " " << result[i + 1] << endl;
}
}

0 comments on commit 6f60f15

Please sign in to comment.