-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathE.cpp
126 lines (113 loc) · 2.92 KB
/
E.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
117
118
119
120
121
122
123
124
125
126
#include <bits/stdc++.h>
#define fi first
#define se second
#define db double
#define U unsigned
#define P std::pair<int,int>
#define LL long long
#define pb push_back
#define MP std::make_pair
#define all(x) x.begin(),x.end()
#define CLR(i,a) memset(i,a,sizeof(i))
#define FOR(i,a,b) for(int i = a;i <= b;++i)
#define ROF(i,a,b) for(int i = a;i >= b;--i)
#define DEBUG(x) std::cerr << #x << '=' << x << std::endl
const int MAXN = 1e5 + 5;
struct Edge{
int to,nxt;
}e[MAXN<<1];
int head[MAXN],bk[MAXN],cnt;
inline void add(int u,int v){
e[++cnt] = (Edge){v,head[u]};head[u] = cnt;
e[++cnt] = (Edge){u,head[v]};head[v] = cnt;
}
int n;LL k;
int sz[MAXN],mx[MAXN],rt;
inline void dfs1(int v,int fa=0){
sz[v] = 1;
for(int i = head[v];i;i = e[i].nxt){
if(e[i].to == fa) continue;
dfs1(e[i].to,v);sz[v] += sz[e[i].to];
mx[v] = std::max(mx[v],sz[e[i].to]);
}
mx[v] = std::max(mx[v],n-sz[v]);
if(mx[rt] > mx[v]) rt = v;
}
LL ansl,ansr;
int tp[MAXN],dep[MAXN],deg[MAXN],fa[MAXN];
std::set<P> S[MAXN];
inline void dfs2(int v,int fa,int rr){
sz[v] = 1;tp[v] = rr;dep[v] = dep[fa]+1;::fa[v] = fa;
for(int i = head[v];i;i = e[i].nxt){
if(e[i].to == fa) continue;
deg[v]++;dfs2(e[i].to,v,rr);sz[v] += sz[e[i].to];
}
if(deg[v]) S[rr].insert(MP(dep[v],v));
ansl += (sz[v]&1);ansr += sz[v];
}
bool vis[MAXN];
std::set<P> q;
inline void del(int v){
if(!--deg[fa[v]]){
S[tp[fa[v]]].erase(MP(dep[fa[v]],fa[v]));
}
}
std::vector<int> rem;
inline void dfs3(int v,int fa=0){
if(!vis[v]) rem.pb(v);
for(int i = head[v];i;i = e[i].nxt){
if(e[i].to == fa) continue;
dfs3(e[i].to,v);
}
}
int main(){
scanf("%d%lld",&n,&k);
FOR(i,2,n){
int u,v;scanf("%d%d",&u,&v);
add(u,v);
}
memcpy(bk,head,sizeof(bk));
mx[0] = n;dfs1(1);sz[rt] = n;
for(int i = head[rt];i;i = e[i].nxt){
dfs2(e[i].to,rt,e[i].to);
if(sz[e[i].to] > 1){
q.insert(MP(sz[e[i].to],e[i].to));
}
}
deg[rt] = 114514;
if(k < ansl || k > ansr || (ansr-k)&1){
puts("NO");return 0;
}
puts("YES");
LL d = ansr-k;
while(d){
int r = q.rbegin()->se;q.erase(--q.end());
int v,gx;std::tie(gx,v) = *S[r].rbegin();
if(d < 2*gx){
d >>= 1;
v = S[r].lower_bound(MP(d,0))->se;
std::vector<int> t;
for(int &i = head[v];i&&t.size()<2;i = e[i].nxt) if(e[i].to != fa[v] && !vis[e[i].to]) t.pb(e[i].to);
if(t.size() < 2) t.pb(v);
vis[t[0]] = vis[t[1]] = 1;
printf("%d %d\n",t[0],t[1]);
break;
}
else{
d -= 2*gx;
std::vector<int> t;
for(int &i = head[v];i&&t.size()<2;i = e[i].nxt) if(e[i].to != fa[v] && !vis[e[i].to]) t.pb(e[i].to);
if(t.size() < 2) t.pb(v);
printf("%d %d\n",t[0],t[1]);
vis[t[0]] = vis[t[1]] = 1;
del(t[0]);del(t[1]);
}
sz[r] -= 2;
if(sz[r]) q.insert(MP(sz[r],r));
}
memcpy(head,bk,sizeof(head));
dfs3(rt);
int t = rem.size()/2;
FOR(i,0,t-1) printf("%d %d\n",rem[i],rem[i+t]);
return 0;
}