题意:给你一个n个点的树,你要求出以第i个点为根,把边全部变成父亲到儿子的有向边,你可以添加不超过k条有向边,使得距离根的最大距离最小,你要求出每个点作为根的答案。
数据范围: 1<=n*k<=2e5
一道很有难度的题目,想了很久才有思路。
首先,考虑怎么在固定根和最大距离d(容易想到二分)的时候判断是否可以。我们只需要不断找距离根最远的点,然后往上跳d-1个,然后把子树截掉。如果k次操作以后最远的小于等于d,那么可以。
然后可以想到可以用换根法来维护距离,然后二分处理。但是线段树的常数太大,会T。
观察发现,相邻点之间的答案只差不超过1,因此可以先求出一个的答案,然后和换根法一起遍历一遍即可,就能优化掉一个log。
#include<bits/stdc++.h>
#define rep(i,x,y) for(int i=x;i<=y;i++)
#define dwn(i,x,y) for(int i=x;i>=y;i--)
#define ll long long
using namespace std;
template<typename T>inline void qr(T &x){
x=0;int f=0;char s=getchar();
while(!isdigit(s))f|=s=='-',s=getchar();
while(isdigit(s))x=x*10+s-48,s=getchar();
x=f?-x:x;
}
int cc=0,buf[31];
template<typename T>inline void qw(T x){
if(x<0)putchar('-'),x=-x;
do{buf[++cc]=int(x%10);x/=10;}while(x);
while(cc)putchar(buf[cc--]+'0');
}
const int N=2e5+10,inf=1e9;
int n,m,k;
vector<int>e[N];
int dep[N],ori[N<<2],ans[N];
int z,L[N],R[N],yss[N];
int fa[N][22];
struct SGT{
int l,r,lazy;
pair<int,int>c;
bool bk1,bk2;//bk1: the vertex; bk2 : the subtree
}t[N<<2];
#define lc p<<1
#define rc p<<1|1
#define mid (t[p].l+t[p].r)/2
void pushdown(int p){
if(t[p].lazy){
t[lc].c.first+=t[p].lazy;
t[lc].lazy+=t[p].lazy;
t[rc].c.first+=t[p].lazy;
t[rc].lazy+=t[p].lazy;
t[p].lazy=0;
}
}
void update(int p){
t[p].c=max(t[lc].c,t[rc].c);
t[p].bk2=t[lc].bk2|t[rc].bk2|t[p].bk1;
}
void build(int p,int l,int r){
t[p].l=l,t[p].r=r;
t[p].lazy=0;
t[p].bk1=t[p].bk2=0;
if(l==r)t[p].c=make_pair(dep[yss[l]]-1,yss[l]);
else{
build(lc,l,mid);
build(rc,mid+1,r);
update(p);
}
}
void change(int p,int x,int y,int k){
if(x<=t[p].l&&t[p].r<=y){
t[p].c.first+=k;
t[p].lazy+=k;
return;
}
pushdown(p);
if(x<=mid)change(lc,x,y,k);
if(mid<y)change(rc,x,y,k);
update(p);
}
void changeinf(int p,int x,int y){
if(t[p].bk1)return;
if(x<=t[p].l&&t[p].r<=y){
ori[p]=t[p].c.first;
t[p].c.first=-inf;
t[p].bk1=t[p].bk2=1;
return;
}
pushdown(p);
if(x<=mid)changeinf(lc,x,y);
if(mid<y)changeinf(rc,x,y);
update(p);
}
void recover(int p,int l,int r){
if(!t[p].bk2)return;
t[p].bk2=0;
if(t[p].bk1){
t[p].c.first=ori[p];
t[p].bk1=0;
}
if(l==r)return;
pushdown(p);
recover(lc,l,mid);
recover(rc,mid+1,r);
update(p);
}
void dfs1(int x){
yss[L[x]=++z]=x;
for(int y:e[x]){
if(y==fa[x][0])continue;
dep[y]=dep[x]+1;
fa[y][0]=x;
dfs1(y);
}
R[x]=z;
}
int getlca(int x,int y){
if(dep[x]<dep[y])swap(x,y);
dwn(i,18,0)
if(dep[fa[x][i]]>=dep[y])
x=fa[x][i];
if(x==y)return x;
dwn(i,18,0)
if(fa[x][i]!=fa[y][i])
x=fa[x][i],y=fa[y][i];
return fa[x][0];
}
int jump(int x,int dep){
dwn(i,18,0)
if(dep>=(1<<i)){
dep-=(1<<i);
x=fa[x][i];
}
return x;
}
bool query(int root,int lim){
rep(i,1,m){
int x=t[1].c.second;
int dis=t[1].c.first;
if(dis<=lim)break;
int ff=getlca(x,root);
if(dep[x]-dep[ff]>lim-1){
int y=jump(x,lim-1);
changeinf(1,L[y],R[y]);
}
else{
int y=jump(root,dep[x]+dep[root]-2*dep[ff]-(lim-1)-1);
if(L[y]>1)changeinf(1,1,L[y]-1);
if(R[y]<n)changeinf(1,R[y]+1,n);
}
}
bool ans=0;
if(t[1].c.first<=lim)ans=1;
recover(1,1,n);
return ans;
}
void dfs2(int x){
for(int y:e[x]){
if(y==fa[x][0])continue;
//SGT
if(1<L[y])change(1,1,L[y]-1,1);
if(R[y]<n)change(1,R[y]+1,n,1);
change(1,L[y],R[y],-1);
//calc ans
if(query(y,ans[x])){
if(ans[x]==1)ans[y]=1;
else if(query(y,ans[x]-1))ans[y]=ans[x]-1;
else ans[y]=ans[x];
}
else ans[y]=ans[x]+1;
dfs2(y);
//SGT
if(1<L[y])change(1,1,L[y]-1,-1);
if(R[y]<n)change(1,R[y]+1,n,-1);
change(1,L[y],R[y],1);
}
}
void solve(){
qr(n),qr(m),qr(k);
rep(i,1,n-1){
int x,y;qr(x),qr(y);
e[x].push_back(y);
e[y].push_back(x);
}
dep[1]=1;dfs1(1);
build(1,1,n);
rep(j,1,18)
rep(i,1,n)
fa[i][j]=fa[fa[i][j-1]][j-1];
ans[1]=n;
while(ans[1]>1&&query(1,ans[1]-1))ans[1]--;
dfs2(1);
if(!k){
qw(ans[1]);puts("");
}
else{
rep(i,1,n)qw(ans[i]),putchar(' ');
puts("");
}
}
int main(){
int tt;tt=1;
while(tt--)solve();
return 0;
}