题解 tree
首先将问题抽象成
给定一条链,链上每个点可能挂着一个子树,两个人从链的两端对向行走,随时可以钻进当前节点的子树里
而且一旦进了子树就再也出不来了
那么预处理每个节点子树内的最长链 \(mdep_i\)
令 \(a_i=i-1+mdep_i,b_i=top-i+mdep_i\)
那么这个东西实际上就是两个人到点 \(i\) 钻进子树里分别能得到的最大步数
发现两个人都是轮流走一步,那么写个爆搜吧
令 \(dfs(i, j)\) 为两人分别在位置 \(i, j\) 时的最大差值
\[dfs(i, j)=\max\begin{cases} a_i-\max\limits_{k=i+1}^jb_k \\ \min(\max\limits_{k=i+1}^{j-1}\{a_k-b_j\}, dfs(i+1, j-1))\end{cases}
\]
发现这个爆搜的状态数只有 \(O(n)\) 种,那么就做完了
复杂度 \(O(n\log n)\),可以用 lxl ST 表弄到近似 \(O(n)\)
点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define N 500010
#define ll long long
//#define int long long
char buf[1<<21], *p1=buf, *p2=buf;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf, 1, 1<<21, stdin)), p1==p2?EOF:*p1++)
inline int read() {
int ans=0, f=1; char c=getchar();
while (!isdigit(c)) {if (c=='-') f=-f; c=getchar();}
while (isdigit(c)) {ans=(ans<<3)+(ans<<1)+(c^48); c=getchar();}
return ans*f;
}
int n, s, t;
int head[N], ecnt;
struct edge{int to, next;}e[N<<1];
inline void add(int s, int t) {e[++ecnt]={t, head[s]}; head[s]=ecnt;}
namespace force{
int dfs(int now, int x, int y, int s) {
bool anyx=0, anyy=0;
for (int i=head[x],v; ~i; i=e[i].next) {
v = e[i].to;
if (s&(1<<v)) continue;
anyx=1;
}
for (int i=head[y],v; ~i; i=e[i].next) {
v = e[i].to;
if (s&(1<<v)) continue;
anyy=1;
}
if (!anyx && !anyy) return 0;
if (now==1) {
if (!anyx) return dfs(now^1, x, y, s);
int ans=-INF;
for (int i=head[x],v; ~i; i=e[i].next) {
v = e[i].to;
if (s&(1<<v)) continue;
ans=max(ans, dfs(now^1, v, y, s|(1<<v))+1);
}
return ans;
}
else {
if (!anyy) return dfs(now^1, x, y, s);
int ans=INF;
for (int i=head[y],v; ~i; i=e[i].next) {
v = e[i].to;
if (s&(1<<v)) continue;
ans=min(ans, dfs(now^1, x, v, s|(1<<v))-1);
}
return ans;
}
}
void solve() {
cout<<dfs(1, s, t, (1<<s)|(1<<t))<<endl;
}
}
namespace task1{
bool inchain[N];
int a[N], b[N], pre[N], suf[N], ans=-INF;
int dep[N], mdep[N], back[N], sta[N], top;
void dfs1(int u, int fa) {
for (int i=head[u],v; ~i; i=e[i].next) {
v = e[i].to;
if (v==fa) continue;
dep[v]=dep[u]+1;
back[v]=u;
dfs1(v, u);
}
}
void split(int u, int v) {
while (1) {
if (dep[u]<dep[v]) swap(u, v);
inchain[u]=inchain[v]=1;
if (u==v) break;
u=back[u];
}
}
void getchain(int u, int fa) {
sta[++top]=u;
for (int i=head[u],v; ~i; i=e[i].next) {
v = e[i].to;
if (v==fa||!inchain[v]) continue;
getchain(v, u);
}
}
void dfs2(int u, int fa) {
mdep[u]=dep[u];
for (int i=head[u],v; ~i; i=e[i].next) {
v = e[i].to;
if (v==fa||inchain[v]) continue;
dep[v]=dep[u]+1;
dfs2(v, u);
mdep[u]=max(mdep[u], mdep[v]);
}
}
void solve() {
dep[1]=1; dfs1(1, 0);
split(s, t);
// cout<<"inchain: "; for (int i=1; i<=n; ++i) cout<<inchain[i]<<' '; cout<<endl;
getchain(s, 0);
// cout<<"sta: "; for (int i=1; i<=top; ++i) cout<<sta[i]<<' '; cout<<endl;
for (int i=1; i<=top; ++i) dep[sta[i]]=0, dfs2(sta[i], 0);
// cout<<"mdep: "; for (int i=1; i<=top; ++i) cout<<mdep[sta[i]]<<' '; cout<<endl;
for (int i=1; i<=top; ++i) a[i]=i-1+mdep[sta[i]], b[i]=top-i+mdep[sta[i]];
for (int i=top; i; --i) suf[i]=max(suf[i+1], b[i]);
for (int i=1; i<=top; ++i) pre[i]=max(pre[i-1], a[i]);
// cout<<"suf: "; for (int i=1; i<=top; ++i) cout<<suf[i]<<' '; cout<<endl;
int len=(top+1)/2, tem=INF;
for (int i=1; i<=len; ++i) ans=max(ans, a[i]-suf[i+1]);
for (int i=len+1; i<=top; ++i) tem=min(tem, pre[i-1]-b[i]);
ans=max(ans, tem);
// cout<<"tem: "<<tem<<endl;
printf("%d\n", ans);
}
}
namespace task2{
bool inchain[N];
int a[N], b[N], pre[N], suf[N], ans=-INF;
int dep[N], mdep[N], back[N], sta[N], top;
int tl[N<<2], tr[N<<2], amx[N<<2], bmx[N<<2];
#define tl(p) tl[p]
#define tr(p) tr[p]
#define pushup(p) amx[p]=max(amx[p<<1], amx[p<<1|1]), bmx[p]=max(bmx[p<<1], bmx[p<<1|1])
void build(int p, int l, int r) {
tl(p)=l; tr(p)=r;
if (l==r) {amx[p]=a[l]; bmx[p]=b[l]; return ;}
int mid=(l+r)>>1;
build(p<<1, l, mid);
build(p<<1|1, mid+1, r);
pushup(p);
}
int amax(int p, int l, int r) {
if (l<=tl(p)&&r>=tr(p)) return amx[p];
int mid=(tl(p)+tr(p))>>1, ans=-INF;
if (l<=mid) ans=max(ans, amax(p<<1, l, r));
if (r>mid) ans=max(ans, amax(p<<1|1, l, r));
return ans;
}
int bmax(int p, int l, int r) {
if (l<=tl(p)&&r>=tr(p)) return bmx[p];
int mid=(tl(p)+tr(p))>>1, ans=-INF;
if (l<=mid) ans=max(ans, bmax(p<<1, l, r));
if (r>mid) ans=max(ans, bmax(p<<1|1, l, r));
return ans;
}
void dfs1(int u, int fa) {
for (int i=head[u],v; ~i; i=e[i].next) {
v = e[i].to;
if (v==fa) continue;
dep[v]=dep[u]+1;
back[v]=u;
dfs1(v, u);
}
}
void split(int u, int v) {
while (1) {
if (dep[u]<dep[v]) swap(u, v);
inchain[u]=inchain[v]=1;
if (u==v) break;
u=back[u];
}
}
void getchain(int u, int fa) {
sta[++top]=u;
for (int i=head[u],v; ~i; i=e[i].next) {
v = e[i].to;
if (v==fa||!inchain[v]) continue;
getchain(v, u);
}
}
void dfs2(int u, int fa) {
mdep[u]=dep[u];
for (int i=head[u],v; ~i; i=e[i].next) {
v = e[i].to;
if (v==fa||inchain[v]) continue;
dep[v]=dep[u]+1;
dfs2(v, u);
mdep[u]=max(mdep[u], mdep[v]);
}
}
// int amax(int l, int r) {
// int ans=-INF;
// for (int i=l; i<=r; ++i) ans=max(ans, a[i]);
// return ans;
// }
// int bmax(int l, int r) {
// int ans=-INF;
// for (int i=l; i<=r; ++i) ans=max(ans, b[i]);
// return ans;
// }
int dfs(int i, int j) {
if (i+1==j) return a[i]-b[j];
int ans=a[i]-bmax(1, i+1, j);
int tem=amax(1, i+1, j-1)-b[j];
if (i+1<j-1) tem=min(tem, dfs(i+1, j-1));
ans=max(ans, tem);
return ans;
}
void solve() {
dep[1]=1; dfs1(1, 0);
split(s, t);
// cout<<"inchain: "; for (int i=1; i<=n; ++i) cout<<inchain[i]<<' '; cout<<endl;
getchain(s, 0);
// cout<<"sta: "; for (int i=1; i<=top; ++i) cout<<sta[i]<<' '; cout<<endl;
for (int i=1; i<=top; ++i) dep[sta[i]]=0, dfs2(sta[i], 0);
// cout<<"mdep: "; for (int i=1; i<=top; ++i) cout<<mdep[sta[i]]<<' '; cout<<endl;
for (int i=1; i<=top; ++i) a[i]=i-1+mdep[sta[i]], b[i]=top-i+mdep[sta[i]];
build(1, 1, top);
// cout<<"top: "<<top<<endl;
printf("%d\n", dfs(1, top));
}
}
signed main()
{
freopen("tree.in", "r", stdin);
freopen("tree.out", "w", stdout);
n=read(); s=read(); t=read();
memset(head, -1, sizeof(head));
for (int i=1,u,v; i<n; ++i) {
u=read(); v=read();
add(u, v); add(v, u);
}
// force::solve();
// task1::solve();
task2::solve();
return 0;
}