-
Notifications
You must be signed in to change notification settings - Fork 1
/
F2.cpp
116 lines (87 loc) · 1.87 KB
/
F2.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <bits/stdc++.h>
using namespace std;
#define ll long long
struct node {
ll u, v;
};
vector <node> g, ans;
vector <ll> tree, lagbe;
ll par[200009], par2[200009];
ll fnd(ll u)
{
if(u == par[u])
return u;
return par[u] = fnd(par[u]);
}
ll fnd2(ll u)
{
if(u == par2[u])
return u;
return par2[u] = fnd2(par2[u]);
}
int main()
{
for(ll i = 0; i <= 200005; i++) {
par[i] = i;
par2[i] = i;
}
ll n, m, d;
cin >> n >> m >> d;
for(ll i = 1; i <= m; i++) {
ll u, v;
scanf("%lld %lld", &u, &v);
if(u == 1)
tree.push_back(v);
else if(v == 1)
tree.push_back(u);
g.push_back({u, v});
}
for(ll i = 0; i < g.size(); i++) {
ll u = g[i].u;
ll v = g[i].v;
if(u == 1 || v == 1)
continue;
ll pu = fnd(u), pv = fnd(v);
if(pu == pv)
continue;
par[pv] = pu;
}
ll counter = 0;
for(ll i = 0; i < tree.size(); i++) {
ll u = 1;
ll v = tree[i];
ll pv = fnd(v);
if(pv == u)
lagbe.push_back(v);
else {
ans.push_back({1, v});
par[pv] = u;
par2[v] = u;
}
}
if(ans.size() > d || tree.size() < d) {
cout << "NO" << endl;
return 0;
}
ll rem = d - ans.size();
for(ll i = 0; i < rem; i++) {
par2[ lagbe[i]] = 1;
ans.push_back({1, lagbe[i]});
}
for(ll i = 0; i < g.size(); i++) {
ll u = g[i].u;
ll v = g[i].v;
if(u == 1 || v == 1)
continue;
ll pu = fnd2(u), pv = fnd2(v);
if(pu == pv)
continue;
par2[pv] = pu;
ans.push_back({u, v});
}
cout << "YES" << endl;
for(node u: ans) {
printf("%lld %lld\n", u.u, u.v);
}
return 0;
}