我不会银牌题。
给定一棵
代价最小的路径的长度一定不超过
证明:
假设能从代价最小的路径中找到连续的三个点
所以这个路径一定有
综上,对于路径中连续的三个点
但是直接枚举
考虑令
总时间复杂度
#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
#define ALL(v) v.begin(),v.end()
#define For(i,_) for(int i=0,i##end=_;i<i##end;++i) // [0,_)
#define FOR(i,_,__) for(int i=_,i##end=__;i<i##end;++i) // [_,__)
#define Rep(i,_) for(int i=(_)-1;i>=0;--i) // [0,_)
#define REP(i,_,__) for(int i=(__)-1,i##end=_;i>=i##end;--i) // [_,__)
typedef long long ll;
typedef unsigned long long ull;
#define V vector
#define pb push_back
#define pf push_front
#define qb pop_back
#define qf pop_front
#define eb emplace_back
typedef pair<int,int> pii;
typedef pair<ll,int> pli;
#define fi first
#define se second
const int dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}},inf=0x3f3f3f3f,mod=1e9+7;
const ll infl=0x3f3f3f3f3f3f3f3fll;
template<class T>inline bool ckmin(T &x,const T &y){return x>y?x=y,1:0;}
template<class T>inline bool ckmax(T &x,const T &y){return x<y?x=y,1:0;}
int init=[](){return cin.tie(nullptr)->sync_with_stdio(false),0;}();
template<class T,T(*merge)(T,T)>
struct ST{
V<V<int>>st;
inline ST(){}
inline ST(const V<T> &a){
int n=a.size(),B=__lg(n);
V<V<int>>(B+1).swap(st);
st[0]=a;
FOR(i,1,B+1){
st[i].resize(n-(1<<i)+1);
For(j,n-(1<<i)+1)st[i][j]=merge(st[i-1][j],st[i-1][j+(1<<i-1)]);
}
}
inline ST(const V<T> &a,const V<int> &pos){
assert(a.size()==pos.size());
int n=a.size(),B=__lg(n);
V<V<int>>(B+1).swap(st);
For(i,B+1){
st[i].resize(n-(1<<i)+1);
if(i)For(j,n-(1<<i)+1)st[i][j]=merge(st[i-1][j],st[i-1][j+(1<<i-1)]);
else For(i,n)st[0][pos[i]]=a[i];
}
}
inline T query(int l,int r){
int n=st[0].size();
assert(0<=l),assert(l<=r),assert(r<n);
int k=__lg(r-l+1);
return merge(st[k][l],st[k][r-(1<<k)+1]);
}
};
int mgmin(int x,int y){return min(x,y);}
int mgmax(int x,int y){return max(x,y);}
int main(){
int t_case=1;
scanf("%d",&t_case);
while(t_case--){
int n,s;
scanf("%d%d",&n,&s);
V<V<int>>to(n);
For(_,n-1){
int x,y;
scanf("%d%d",&x,&y),--x,--y;
to[x].pb(y),to[y].pb(x);
}
V<int>dep(n),dis(n,inf),fa(n),siz(n,1),son(n,-1);
V<pii>m(n);
m[s-1]={s,s};
auto dfs1=[&](auto &&self,int p,int f)->void{
if(~f)dep[p]=dep[f]+1;
fa[p]=f;
ckmin(m[p].fi,p+1),ckmax(m[p].se,p+1);
for(int i:to[p])if(i!=f){
m[i]=m[p],self(self,i,p);
siz[p]+=siz[i];
if(!~son[p]||siz[i]>siz[son[p]])son[p]=i;
}
ll prod=(p+1==s?0:1ll*m[p].fi*m[p].se);
if(prod<inf)dis[p]=prod;
};
dfs1(dfs1,s-1,-1);
int cnt=0;
V<int>dfn(n),mn(n,inf),mx(n,-1),top(n,-1),tot(n);
auto dfs2=[&](auto &&self,int p,int k)->void{
ckmin(mn[k],p+1),ckmax(mx[k],p+1),++tot[k];
dfn[p]=cnt++;
top[p]=k;
if(~son[p]){
self(self,son[p],k);
for(int i:to[p])if(!~top[i])self(self,i,i);
}
};
dfs2(dfs2,s-1,s-1);
V<int>id(n);
iota(ALL(id),1);
ST<int,mgmin>st1(id,dfn);
ST<int,mgmax>st2(id,dfn);
V<bool>vis(n);
FOR(i,1,n+1){
if(i!=s)FOR(j,1,min(n,2*n/(s+i))+1)if(i!=j&&j!=s&&!vis[j-1]){
int nm=i,xm=i;
{
int x=i-1,y=j-1;
while(top[x]!=top[y]){
if(dep[top[x]]<dep[top[y]])swap(x,y);
if(dep[x]-dep[top[x]]+1==tot[top[x]])ckmin(nm,mn[top[x]]),ckmax(xm,mx[top[x]]);
else ckmin(nm,st1.query(dfn[top[x]],dfn[x])),ckmax(xm,st2.query(dfn[top[x]],dfn[x]));
x=fa[top[x]];
}
if(dep[x]>dep[y])swap(x,y);
ckmin(nm,st1.query(dfn[x],dfn[y])),ckmax(xm,st2.query(dfn[x],dfn[y]));
}
ll prod=dis[j-1]+1ll*nm*xm;
if(prod<inf&&ckmin(dis[i-1],(int)prod))vis[i-1]=true;
}
printf("%d%c",dis[i-1]," \n"[i==n]);
}
}
return 0;
}