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
| const int N = 2e5+10; int n,dtop; ll k; struct{ int to,nex; }edge[N<<1]; int head[N],edge_num; inline void add(int x,int y){ edge[++edge_num].to = y; edge[edge_num].nex = head[x]; head[x] = edge_num; } struct node{ int siz,pos; inline int operator < (const node&G) const{ return siz < G.siz; } }g[N]; inline void dfs1(int now,int fu){ g[now] = {1,now}; for(int i=head[now];i;i=edge[i].nex){ int tto = edge[i].to; if(tto==fu) continue; dfs1(tto,now); g[now].siz += g[tto].siz; } } struct WP{ int L,pos,dfn; inline int operator < (const WP&G){ if(L^G.L) return L > G.L; return dfn < G.dfn; } }wp[N]; inline void dfs2(int now,int fu){ wp[now].L += wp[fu].L; wp[now].pos = now; wp[now].dfn = ++dtop; for(int i=head[now];i;i=edge[i].nex){ int tto = edge[i].to; if(tto==fu) continue; dfs2(tto,now); } } int ans[N]; int main(){
read(n,k); if(k<n){ puts("No"); return 0; } for(int i=1,u,v;i<n;++i){ read(u,v); add(u,v); add(v,u); } dfs1(1,0); sort(g+1,g+1+n); for(int i=n;i && k;--i){ if(k>=g[i].siz){ k -= g[i].siz; ++wp[g[i].pos].L; } } if(k){ puts("No"); return 0; } dfs2(1,0); sort(wp+1,wp+1+n); for(int i=1;i<=n;++i) ans[wp[i].pos] = n-i+1; puts("Yes"); for(int i=1;i<=n;++i) printf("%d ",ans[i]);
return 0; }
|