1. 程式人生 > >【XSY2190】Alice and Bob VI 樹形DP 樹剖

【XSY2190】Alice and Bob VI 樹形DP 樹剖

pri struct include n) mar 代碼 所有結點 沒有 除了

題目描述

  Alice和Bob正在一棵樹上玩遊戲。這棵樹有\(n\)個結點,編號由\(1\)\(n\)。他們一共玩\(q\)盤遊戲。

  在第\(i\)局遊戲中,Alice從結點\(a_i\)出發,Bob從結點\(b_i\)出發。開始時,除了\(a_i\)\(b_i\)這兩個結點外,所有結點都沒有染色。結點\(a_i\)被Alice染色,結點\(b_i\)被Bob染色。

  接下來,兩位玩家輪流移動,兩位玩家移動步數之和為\(k_i\)步。Alice走第一步,Bob走第二步,Alice走第三步\(\cdots\)在每一步中,玩家可以移動到相鄰的結點並把該結點染色。註意一個結點可以被多次染色:在任意時刻,每個被染過色的結點的顏色為最後到達過該結點的玩家染的顏色。

  記遊戲結束時Alice染色的結點數為\(A\) , Bob染色的結點數為\(B\) 。Alice 想要\((A - B)\)盡量大,Bob 想要\((A - B)\)盡量小。如果兩個玩家都以最優策略玩的話,我們想知道最後的\((A - B)\)值是多少。

  \(n,q\leq 20000\)

題解

  設兩人之間距離為\(d\)

  兩個人可以相遇或不相遇。

  相遇:

  \(k\)\(d\)偶:\(1\)

  \(k\)\(d\)奇:\(2\)

  \(k\)\(d\)偶:\(-1\)

  \(k\)\(d\)奇:\(0\)

  不相遇:

  \(k\)奇:1

  \(k\)偶:0

  如果\(k\)

奇,那麽對\(Bob\)來說不相遇更優,他就需要逃跑。

  如果\(k\)偶,那麽\(Alice\)就要逃跑。

  判斷逃跑是否成功就是看這個人在不經過另一個人能到達的點的情況下能走多少步。

  可以發現逃跑路徑的終點一定是直徑的一個端點。

  可以通過樹形DP求出子樹內直徑和子樹外直徑。

  時間復雜度:\(O(n+q\log n)\)

代碼

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
vector<int
> g[200010]; int n,q; struct node { int f,t,w,d,s,ms; }; node a[200010]; int ti; int w[200010]; void dfs(int x,int fa,int dep) { a[x].f=fa; a[x].d=dep; a[x].s=1; int s=0; for(vector<int>::iterator p=g[x].begin();p!=g[x].end();p++) { int v=*p; if(v!=fa) { dfs(v,x,dep+1); a[x].s+=a[v].s; if(a[v].s>s) { s=a[v].s; a[x].ms=v; } } } } void dfs2(int x,int top) { a[x].t=top; a[x].w=++ti; w[ti]=x; if(a[x].ms) dfs2(a[x].ms,top); for(vector<int>::iterator p=g[x].begin();p!=g[x].end();p++) { int v=*p; if(v!=a[x].f&&v!=a[x].ms) dfs2(v,v); } } int getlca(int x,int y) { while(a[x].t!=a[y].t) if(a[a[x].t].d>a[a[y].t].d) x=a[a[x].t].f; else y=a[a[y].t].f; return a[x].d<a[y].d?x:y; } int jump(int x,int d) { while(a[x].w-a[a[x].t].w<d) { d-=a[x].w-a[a[x].t].w+1; x=a[a[x].t].f; } return w[a[x].w-d]; } struct p1{int d,x;p1(int a=0,int b=0):x(a),d(b){}}; int operator <(p1 a,p1 b){return a.d<b.d;} int operator >(p1 a,p1 b){return a.d>b.d;} p1 operator +(p1 a,int b){a.d+=b;return a;} struct p2{int d,x,y;p2(int a=0,int b=0,int c=0):x(a),y(b),d(c){}}; int operator <(p2 a,p2 b){return a.d<b.d;} int operator >(p2 a,p2 b){return a.d>b.d;} p2 operator +(p1 a,p1 b){p2 c;c.d=a.d+b.d;c.x=a.x;c.y=b.x;return c;} p2 operator +(p2 a,int b){a.d+=b;return a;} p1 f1[200010]; p2 f2[200010]; p1 fir[200010]; p1 sec[200010]; p1 thi[200010]; p2 fir1[200010]; p2 sec1[200010]; p1 g1[200010]; p2 g2[200010]; void dfs3(int x) { f1[x]=p1(x,1); f2[x]=p2(x,x,1); fir[x].d=sec[x].d=thi[x].d=fir1[x].d=sec1[x].d=-1; for(vector<int>::iterator p=g[x].begin();p!=g[x].end();p++) { int v=*p; if(v!=a[x].f) { dfs3(v); f2[x]=max(f2[x],f1[x]+f1[v]); f2[x]=max(f2[x],f2[v]); f1[x]=max(f1[x],f1[v]+1); if(f1[v]>fir[x]) { thi[x]=sec[x]; sec[x]=fir[x]; fir[x]=f1[v]; } else if(f1[v]>sec[x]) { thi[x]=sec[x]; sec[x]=f1[v]; } else if(f1[v]>thi[x]) thi[x]=f1[v]; if(f2[v]>fir1[x]) { sec1[x]=fir1[x];; fir1[x]=f2[v]; } else if(f2[v]>sec1[x]) sec1[x]=f2[v]; } } } void dfs4(int x) { for(vector<int>::iterator p=g[x].begin();p!=g[x].end();p++) { int v=*p; if(v!=a[x].f) { g1[v]=p1(x,1); g2[v]=p2(x,x,1); if(f1[v].x==fir[x].x) { g2[v]=max(g2[v],max(max(sec[x]+thi[x],sec[x]+g1[x]),thi[x]+g1[x])+1); g2[v]=max(g2[v],max(sec[x],g1[x])+p1(x,1)); g1[v]=max(g1[v],max(sec[x],g1[x])+1); } else if(f1[v].x==sec[x].x) { g2[v]=max(g2[v],max(max(fir[x]+thi[x],fir[x]+g1[x]),thi[x]+g1[x])+1); g2[v]=max(g2[v],max(fir[x],g1[x])+p1(x,1)); g1[v]=max(g1[v],max(fir[x],g1[x])+1); } else { g2[v]=max(g2[v],max(max(fir[x]+sec[x],fir[x]+g1[x]),sec[x]+g1[x])+1); g2[v]=max(g2[v],max(fir[x],g1[x])+p1(x,1)); g1[v]=max(g1[v],max(fir[x],g1[x])+1); } if(f2[v].x==fir1[x].x) g2[v]=max(g2[v],max(sec1[x],g2[x])); else g2[v]=max(g2[v],max(fir1[x],g2[x])); dfs4(v); } } } int getdist(int x,int y) { return a[x].d+a[y].d-2*a[getlca(x,y)].d; } int gao(int x,int y,int lca,int d) { int dist=a[x].d+a[y].d-2*a[lca].d; if(dist<=d) return -1; if(d>=a[x].d-a[lca].d) { int z=jump(y,dist-d-1); int x1=f2[z].x; int x2=f2[z].y; return max(getdist(y,x1),getdist(y,x2)); } else { int z=jump(x,d); int x1=g2[z].x; int x2=g2[z].y; return max(getdist(y,x1),getdist(y,x2)); } } int main() { #ifndef ONLINE_JUDGE freopen("c.in","r",stdin); freopen("c.out","w",stdout); #endif scanf("%d%d",&n,&q); int i,x,y; for(i=1;i<n;i++) { scanf("%d%d",&x,&y); g[x].push_back(y); g[y].push_back(x); } dfs(1,0,1); dfs2(1,1); dfs3(1); g1[1]=p1(1,-1); g2[1]=p2(1,1,-1); dfs4(1); int k; for(i=1;i<=q;i++) { scanf("%d%d%d",&x,&y,&k); int lca=getlca(x,y); int d=a[x].d+a[y].d-2*a[lca].d; if(k&1) { if(gao(x,y,lca,(k+1)/2)>=k/2) printf("1\n"); else if(d&1) printf("2\n"); else printf("1\n"); } else { if(gao(y,x,lca,k/2)>=(k+1)/2) printf("0\n"); else if(d&1) printf("0\n"); else printf("-1\n"); } } return 0; }

【XSY2190】Alice and Bob VI 樹形DP 樹剖