前言
接近 3h 只写了这一个题,宝宝你真糖。
思路
非常暴力根本不带脑子的做法。
考虑 ,给定第一个粉色点 、棕色点 ,如何快速求出第二个粉色点放在哪里最优。
钦定根为 ,进行分讨,记 为 重子树大小, 为 非严格次重子树大小。
-
,选择有两种:放在 和放在 重子树,贡献是 ,其中 是 向 走一步的节点。
-
,要分讨 是否属于 的重子树,不属于的话可以放在 的重子树和 两个地方,贡献是 ;否则可以放在 的重子树,次重子树和 三个地方,贡献是 。
-
其他情况下,选择有两种:放在 和放在 重子树,贡献是 。
最后对于所有点对 的答案,我们将 相同的取 ,然后取 即可。
考虑优化。我们尝试去枚举一维,快速计算另一维的答案。考虑到我们需要用到节点 ,枚举 会更方便一点。
我们去研究合法 的分布情况。
那么对于情况 , 意味着 在 子树内,枚举 的儿子作为 ,那么满足此时 的 就是子树 ,在 DFS 序上是一个区间;
对于情况 , 时 在 根链上,而且还要根据“ 是否属于 的重子树”进行区分,很容易想到树链剖分的结构,那么“ 属于 的重子树”的 在 DFS 序上就是 个区间,不属于重子树就是 个单点;
对于情况 ,就是 DFS 区间 去掉子树 与 根链,同样是 个区间。
那么问题就变为,给定 与区间 ,要求你在 时快速求出上面那些式子的 。
观察式子,可以发现我们要求的核心部分实际上都是 状物,其中 在询问时给出, 根据分类讨论情况不同而不同,同时给出 令 与询问答案取 ,询问有 次。最终答案就是 的最大值。
考虑将所有询问按 从小到大排序,称 有效当且仅当 ,称 有效当且仅当 无效。那么询问的过程中一定是 从无效到有效,用两棵线段树 分别维护有效的 ,查询时取 即可。
时间复杂度 ,空间复杂度 。
代码
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
using ull=unsigned long long;
using pii=pair<int,int>;
#define fi first
#define se second
#define mp make_pair
#define debug1(x) cerr<<(#x)<<"="<<(x)<<" "
#define debug2(x) cerr<<(#x)<<"="<<(x)<<"\n"
template<class T>
inline void operator += (vector<T>&a,const T &b){a.emplace_back(b);}
constexpr int maxn=1e5+10,maxlog=17;
struct query{
int l,r,x,id,ans;
}q1[maxn*maxlog],q2[maxn*maxlog],q3[maxn*maxlog];
multiset<int>s1,s2;
int fk[maxn];
constexpr int inf=1e9;
namespace exstd{
constexpr int maxs=4e5+10;
//线段树维护区间 min 单点修
struct sgmtree{
#define ls(id) ((id)<<1)
#define rs(id) (((id)<<1)|1)
#define inr(l,r,cl,cr) ((cl)<=(l) and (r)<=(cr))
int w[maxs];
void pushup(int id){
w[id]=min(w[ls(id)],w[rs(id)]);
}
void build(int id,int l,int r,int *a){
if(l==r){
w[id]=a[l];
return;
}
int mid=(l+r)>>1;
build(ls(id),l,mid,a);
build(rs(id),mid+1,r,a);
pushup(id);
}
void INF(int id,int l,int r){
if(l==r){
w[id]=inf;
return;
}
int mid=(l+r)>>1;
INF(ls(id),l,mid);
INF(rs(id),mid+1,r);
pushup(id);
}
void update(int id,int l,int r,int cx,int pos){
if(l==r){
w[id]=pos;
return;
}
int mid=(l+r)>>1;
if(cx<=mid)update(ls(id),l,mid,cx,pos);
else update(rs(id),mid+1,r,cx,pos);
pushup(id);
}
int check(int id,int l,int r,int cl,int cr){
if(inr(l,r,cl,cr))return w[id];
int mid=(l+r)>>1;
if(cr<=mid)return check(ls(id),l,mid,cl,cr);
if(cl>mid)return check(rs(id),mid+1,r,cl,cr);
return min(check(ls(id),l,mid,cl,cr),check(rs(id),mid+1,r,cl,cr));
}
};
}
exstd::sgmtree t1,t2;
int rans[maxn];
void solve(query *q,int tot,int *a,int *b,int n){
//max(a,b+x) 的区间 min
//b-a\ge -x
//按 x 从小到大扫,每一位都只会从 a 有效变成 b 有效
sort(q+1,q+tot+1,[](query pa,query pb){return pa.x<pb.x;});
for(int i=1;i<=n;i++)fk[i]=i;
sort(fk+1,fk+n+1,[&](int pa,int pb){return b[pa]-a[pa]>b[pb]-a[pb];});
//t1 放还有效的 a,t2 放已经有效的 b
t1.build(1,1,n,a);t2.INF(1,1,n);
int z=1;
for(int i=1;i<=tot;i++){
while(z<=n and b[fk[z]]-a[fk[z]]>=(-q[i].x)){
//这一位以后都由 b+x 接管
t1.update(1,1,n,fk[z],inf);
t2.update(1,1,n,fk[z],b[fk[z]]);
z++;
}
//两边分别取区间 min,再把 b 这边加上 x 即可
q[i].ans=min(t1.check(1,1,n,q[i].l,q[i].r),t2.check(1,1,n,q[i].l,q[i].r)+q[i].x);
rans[q[i].id]=min(rans[q[i].id],q[i].ans);
}
}
int pa[maxn],pb[maxn];
namespace ARIS2_0{
vector<int>v[maxn];
int fa[maxn],wc[maxn],dep[maxn],p1[maxn],p2[maxn],size[maxn];
//树剖 DFS1
void dfs1(int x){
size[x]=1;
for(int y:v[x]){
if(y!=fa[x]){
fa[y]=x;
dep[y]=dep[x]+1;
dfs1(y);
size[x]+=size[y];
if(size[y]>size[wc[x]])wc[x]=y;
if(size[y]>p1[x])p2[x]=p1[x],p1[x]=size[y];
else p2[x]=max(p2[x],size[y]);
}
}
}
int d[maxn],p[maxn],dtot=0,top[maxn];
//树剖 DFS2
void dfs2(int x,int V){
d[p[x]=++dtot]=x;
top[x]=V;
if(wc[x])dfs2(wc[x],V);
for(int y:v[x]){
if(y!=fa[x] and y!=wc[x])dfs2(y,y);
}
}
int lca(int x,int y){
while(top[x]!=top[y]){
if(dep[top[x]]<dep[top[y]])swap(x,y);
x=fa[top[x]];
}
return dep[x]<dep[y]?x:y;
}
int n;
//1. p1,max(p2,n-size) q1
//2. n-size,n+p1 q2
//3. n-size,p1 q3
vector<pii>pr;
int q1tot=0,q2tot=0,q3tot=0;
void dfs3(int x){
pr.clear();
//统计 x 答案
//1. 根在上面,此时如果是重链,答案是 max(p1[i],size[x]+max(p2[i],n-size[i])),否则答案是 size[x]+max(p1[i],n-size[i])
//2. 根在下面的子树 y,此时答案是 max(n-size[i],n-size[y]+p1[i])
int pos=x;
//把“x 属于祖先重子树”的祖先按重链塞成区间,对应情况 2
while(top[pos]){
//首先处理 top[pos] 到 fa[pos] 的贡献,这里属于 else
int L=p[top[pos]],R=p[fa[pos]];
pr+=mp(L,R+1);
if(L<=R){
q1[++q1tot]=query{L,R,size[x],x,0};
}
//然后是 fa[top[pos]] 的贡献
pos=fa[top[pos]];
if(pos){
//这个祖先和 x 之间连的是轻边直接算
rans[x]=min(rans[x],size[x]+max(p1[pos],n-size[pos]));
}
}
//枚举 x 的儿子 y,整棵子树 y 就是情况 1 的一个区间
for(int y:v[x]){
if(y!=fa[x]){
int L=p[y],R=p[y]+size[y]-1;
q2[++q2tot]=query{L,R,-size[y],x,0};
}
}
reverse(pr.begin(),pr.end());
if(size[x]>1)pr+=mp(p[x]+1,p[x]+size[x]-1);
//剩下的 DFS 序区间就是情况 3
int L=1;
for(pii pos:pr){
int R=pos.fi-1;
if(L<=R){
q3[++q3tot]=query{L,R,size[x],x,0};
}
L=pos.se+1;
}
if(L<=n){
int R=n;
q3[++q3tot]=query{L,R,size[x],x,0};
}
for(int y:v[x]){
if(y!=fa[x]){
dfs3(y);
}
}
}
//1. p1,max(p2,n-size) q1
//2. n-size,n+p1 q2
//3. n-size,p1 q3
void solve(){
cin>>n;
for(int i=1,x,y;i<n;i++){
cin>>x>>y;
v[x]+=y;
v[y]+=x;
}
dfs1(1);
dfs2(1,1);
for(int i=1;i<=n;i++)rans[i]=inf;
dfs3(1);
//情况 2:x 在 v 的重子树内
for(int i=1;i<=n;i++)pa[i]=p1[d[i]],pb[i]=max(p2[d[i]],n-size[d[i]]);
solve(q1,q1tot,pa,pb,n);
//情况 1:v 在 x 的子树内
for(int i=1;i<=n;i++)pa[i]=n-size[d[i]],pb[i]=n+p1[d[i]];
solve(q2,q2tot,pa,pb,n);
//情况 3:x,v 互不为祖先
for(int i=1;i<=n;i++)pa[i]=n-size[d[i]],pb[i]=p1[d[i]];
solve(q3,q3tot,pa,pb,n);
//每个 x 已经对所有 v 取完 min 最后再取 max
int allans=0;
for(int i=1;i<=n;i++)allans=max(allans,rans[i]);
cout<<allans<<"\n";
}
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
int T=1;
while(T--)ARIS2_0::solve();
return 0;
}
/*
g++ -std=c++14 -Wall -Wextra -Wshadow -Wconversion
*/