题解:【CF1881F】 Minimum Maximum Distance

题目链接

先钦定一个根跑出所有点的深度,这样就得到了钦定的根到所有关键点的距离最大值。换根的过程考虑对于一条边 \(u \to v\)\(v\) 子树内的点深度会减一,子树外的点深度会加一。拍到 dfs 序上就变成了区间加减一,求全局最大值,可以线段树维护。对于标记点的限制,建线段树的时候将标记点权值设为 \(dep\),其余都设为 -INF 即可。简单更改线段树维护的内容,就可以做更强的东西。这样复杂度是 \(\mathcal O(n \log n)\)

#include<bits/stdc++.h>
#define ld long double
#define ui unsigned int
#define ull unsigned long long
#define int long long
#define eb emplace_back
#define pb pop_back
#define ins insert
#define mp make_pair
#define pii pair<int,int>
#define fi first
#define se second
#define power(x) ((x)*(x))
using namespace std;

namespace FastIO
{
    template<typename T=int> inline T read()
    {
        T s=0,w=1; char c=getchar();
        while(!isdigit(c)) {if(c=='-') w=-1; c=getchar();}
        while(isdigit(c)) s=(s*10)+(c^48),c=getchar();
        return s*w;
    }
    template<typename T> inline void read(T &s)
    {
        s=0; int w=1; char c=getchar();
        while(!isdigit(c)) {if(c=='-') w=-1; c=getchar();}
        while(isdigit(c)) s=(s*10)+(c^48),c=getchar();
        s=s*w;
    }
    template<typename T,typename... Args> inline void read(T &x,Args &...args)
    {
        read(x),read(args...);
    }
    template<typename T> inline void write(T x,char ch)
    {
        if(x<0) x=-x,putchar('-');
        static char stk[25]; int top=0;
        do {stk[top++]=x%10+'0',x/=10;} while(x);
        while(top) putchar(stk[--top]);
        if(ch!='~') putchar(ch);
        return;
    }
}
using namespace FastIO;

namespace MTool
{   
    #define TA template<typename T,typename... Args>
    #define TT template<typename T>
    static const int Mod=998244353;
    TT inline void Swp(T &a,T &b) {T t=a;a=b;b=t;}
    TT inline void cmax(T &a,T b) {a=max(a,b);}
    TT inline void cmin(T &a,T b) {a=min(a,b);}
    TA inline void cmax(T &a,T b,Args... args) {a=max({a,b,args...});}
    TA inline void cmin(T &a,T b,Args... args) {a=min({a,b,args...});}
    TT inline void Madd(T &a,T b) {a=a+b>=Mod?a+b-Mod:a+b;}
    TT inline void Mdel(T &a,T b) {a=a-b<0?a-b+Mod:a-b;}
    TT inline void Mmul(T &a,T b) {a=a*b%Mod;}
    TT inline void Mmod(T &a) {a=(a%Mod+Mod)%Mod;}
    TT inline T Cadd(T a,T b) {return a+b>=Mod?a+b-Mod:a+b;}
    TT inline T Cdel(T a,T b) {return a-b<0?a-b+Mod:a-b;}
    TT inline T Cmul(T a,T b) {return a*b%Mod;}
    TT inline T Cmod(T a) {return (a%Mod+Mod)%Mod;}
    TA inline void Madd(T &a,T b,Args... args) {Madd(a,Cadd(b,args...));}
    TA inline void Mdel(T &a,T b,Args... args) {Mdel(a,Cadd(b,args...));}
    TA inline void Mmul(T &a,T b,Args... args) {Mmul(a,Cmul(b,args...));}
    TA inline T Cadd(T a,T b,Args... args) {return Cadd(Cadd(a,b),args...);}
    TA inline T Cdel(T a,T b,Args... args) {return Cdel(Cdel(a,b),args...);}
    TA inline T Cmul(T a,T b,Args... args) {return Cmul(Cmul(a,b),args...);}
    TT inline T qpow(T a,T b) {int res=1; while(b) {if(b&1) Mmul(res,a); Mmul(a,a); b>>=1;} return res;}
    TT inline T qmul(T a,T b) {int res=0; while(b) {if(b&1) Madd(res,a); Madd(a,a); b>>=1;} return res;}
    TT inline T spow(T a,T b) {int res=1; while(b) {if(b&1) res=qmul(res,a); a=qmul(a,a); b>>=1;} return res;}
    TT inline void exgcd(T A,T B,T &X,T &Y) {if(!B) return X=1,Y=0,void(); exgcd(B,A%B,Y,X),Y-=X*(A/B);}
    TT inline T Ginv(T x) {T A=0,B=0; exgcd(x,Mod,A,B); return Cmod(A);}
    #undef TT
    #undef TA
}
using namespace MTool;

inline void file()
{
    freopen(".in","r",stdin);
    freopen(".out","w",stdout);
    return;
}

bool Mbe;

namespace LgxTpre
{
    static const int MAX=200010;
    static const int inf=2147483647;
    static const int INF=4557430888798830399;
    
    int T,n,m,x,y,ans;
    int dep[MAX],lt[MAX];
    int L[MAX],R[MAX],id[MAX],tot;
    vector<int> G[MAX];
    
    namespace SegmentTree
    {
    	int info[MAX<<2],tag[MAX<<2];
    	inline void pushup(int i) {info[i]=max(info[i<<1],info[i<<1|1]);}
    	inline void down(int i,int k) {tag[i]+=k,info[i]+=k;}
    	inline void pushdown(int i) {if(tag[i]) down(i<<1,tag[i]),down(i<<1|1,tag[i]),tag[i]=0;}
    	inline void build(int i,int l,int r)
    	{
    		info[i]=0,tag[i]=0;
    		if(l==r) return info[i]=lt[id[l]]?dep[id[l]]:-INF,void();
    		int mid=(l+r)>>1;
    		build(i<<1,l,mid),build(i<<1|1,mid+1,r);
    		pushup(i);
		}
    	void modify(int i,int l,int r,int L,int R,int k)
    	{
    		if(r<L||R<l) return;
    		if(l<=L&&R<=r) return down(i,k);
    		pushdown(i); int mid=(L+R)>>1;
    		if(l<=mid) modify(i<<1,l,r,L,mid,k);
    		if(r>mid)  modify(i<<1|1,l,r,mid+1,R,k);
    		pushup(i);
		}
		inline void modify(int l,int r,int k) {modify(1,l,r,1,n,k);}
	}
	using namespace SegmentTree;
    
    inline void lmy_forever()
    {
    	read(T);
    	while(T--)
    	{
    		read(n,m),ans=INF,tot=0,dep[0]=-1;
    		for(int i=1;i<=n;++i) G[i].clear(),lt[i]=L[i]=R[i]=id[i]=dep[i]=0;
    		for(int i=1;i<=m;++i) lt[read()]=1;
    		for(int i=1;i<n;++i)  read(x,y),G[x].eb(y),G[y].eb(x);
    		
    		auto dfs1=[&](auto dfs1,int now,int father)->void
    		{
    			L[now]=++tot,id[tot]=now,dep[now]=dep[father]+1;
    			for(auto to:G[now]) if(to!=father) dfs1(dfs1,to,now);
    			R[now]=tot;
			}; dfs1(dfs1,1,0);
			
			build(1,1,n);
			
			auto dfs2=[&](auto dfs2,int now,int father)->void
			{
				cmin(ans,info[1]);
				for(auto to:G[now]) if(to!=father)
				{
					modify(1,L[to]-1,1),modify(R[to]+1,n,1),modify(L[to],R[to],-1);
					dfs2(dfs2,to,now);
					modify(1,L[to]-1,-1),modify(R[to]+1,n,-1),modify(L[to],R[to],1);
				}
			}; dfs2(dfs2,1,0);
			
    		write(ans,'\n');
    	}
	}
}

bool Med;

signed main()
{
    //file();
    fprintf(stderr,"%.3lf MB\n",abs(&Med-&Mbe)/1048576.0);
    int Tbe=clock();
    LgxTpre::lmy_forever();
    int Ted=clock();
    cerr<<1e3*(Ted-Tbe)/CLOCKS_PER_SEC<<" ms\n";
    return (0-0);
}
posted @ 2023-10-13 08:08  LgxTpre  阅读(104)  评论(0编辑  收藏  举报