-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcses_1666.cpp
More file actions
57 lines (48 loc) · 907 Bytes
/
cses_1666.cpp
File metadata and controls
57 lines (48 loc) · 907 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
47
48
49
50
51
52
53
54
55
56
57
//DSU
#include <iostream>
#include <vector>
using namespace std;
vector<int> fa;
vector<pair<int, int>> road;
int ans;
int find_fa(int x)
{
return x == fa[x] ? x : fa[x] = find_fa(fa[x]);
}
void unite(int x, int y)
{
fa[find_fa(y)] = find_fa(x);
}
void print()
{
cout << ans << "\n";
for (auto x : road)
cout << x.first << " " << x.second << "\n";
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n, m, a, b;
cin >> n >> m;
fa.resize(n + 1);
road.reserve(n + 1);
for (int i = 1; i <= n; i++)
fa[i] = i;
while (m--)
{
cin >> a >> b;
unite(a, b);
}
for (int i = 2; i <= n; i++)
{
if (find_fa(i) != find_fa(1))
{
unite(find_fa(1), find_fa(i));
road.emplace_back(make_pair(find_fa(1), i));
ans++;
}
}
print();
return 0;
}