跳到正文

题解

我不会银牌题。

meyi约 10 分钟
原文
文章信息与版本

文章信息

发布
2024年10月3日
收录
2025年12月12日
更新
2026年11月16日
篇幅
约 10 分钟
获赞
1

版本

  1. v1当前

给定一棵 nn 个点的树,从 uu 走到 vv 的代价 cost(u,v)=(min⁡w∈path(u,v)w)×(max⁡w∈path(u,v)w)cost(u,v)=(\min\limits_{w\in path(u,v)} w)\times (\max\limits_{w\in path(u,v)} w),其中 path(u,v)path(u,v) 是 uu 到 vv 的简单路径。再给定起点 ss,对 i=1∼ni=1\sim n 求出 ss 走到 ii 的最小代价。

代价最小的路径的长度一定不超过 33,即 ii 的答案要么是 cost(s,i)cost(s,i) 要么是 cost(s,j)+cost(j,i)cost(s,j)+cost(j,i),且 j<min⁡(s,i)j<\min(s,i)。

证明:

假设能从代价最小的路径中找到连续的三个点 x,y,zx,y,z 且满足 x<y<zx<y<z 或 x>y>zx>y>z,那么从 xx 走到 zz 的代价是 x×y+y×z=y×(x+z)<min⁡(x,z)×max⁡(x,z)=cost(x,z)x\times y+y\times z=y\times(x+z)<\min(x,z)\times \max(x,z)=cost(x,z),直接将 yy 删掉一定更优。

所以这个路径一定有 y>max⁡(x,z)y>\max(x,z) 或 y<min⁡(x,z)y<\min(x,z),类似折线图。但对于 y>max⁡(x,z)y>\max(x,z) 的情况,从 xx 走到 zz 的代价是 x×y+y×z>(max⁡(x,z))2>cost(x,z)x\times y+y\times z>(\max(x,z))^2>cost(x,z),此时也必须要将 yy 删掉。

综上,对于路径中连续的三个点 x,y,zx,y,z,一定有 y<min⁡(x,z)y<\min(x,z),显然只有长度 ≤3\le 3 的时候能满足这一点。

但是直接枚举 jj 并计算 costcost 的复杂度仍然是 O(n2log⁡n)O(n^2\log n) 的,无异于暴力,如何优化?

考虑令 j=1j=1,那么 cost(s,j)+cost(j,i)<n+n=2ncost(s,j)+cost(j,i)<n+n=2n,也就是说 j×(s+i)<2nj\times (s+i)<2n,j<2ns+ij<\frac{2n}{s+i},根据调和级数可知合法的 (i,j)(i,j) 只有 O(nlog⁡n)O(n\log n) 对,直接枚举并计算即可。

总时间复杂度 O(nlog⁡2n)O(n\log^2 n),写个 O(1)lcaO(1)\text{lca} 状物好像可以做到 O(nlog⁡n)O(n\log n),但是没必要,下面这份代码只跑了 0.2s,但是数据疑似挺脑残的,有人把 jj 直接取 min⁡(n,20)\min(n,20) 也过了。。。

C++
#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;
}
原文