Another LCT template problem.
How to find out if it's on the same tree? Just find the smallest point of depth is not the same point.
That's the point at the far left of Splay.
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#defineMAXV 10050#defineMaxe 20050using namespacestd;intn,m,fath[maxv],size[maxv],tree[maxv][3],X,Y,REV[MAXV];intstack[maxv],top=0;Chars[ -];BOOLIsRootintx) { returntree[fath[x]][1]!=x&&tree[fath[x]][2]!=x;}voidPushdown (intx) { if(Rev[x]) {intls=tree[x][1],rs=tree[x][2]; REV[LS]^=1; rev[rs]^=1; rev[x]=0; Swap (tree[x][1],tree[x][2]); }}voidPushup (intx) { intls=tree[x][1],rs=tree[x][2]; SIZE[X]=size[ls]+size[rs]+1;}voidRotateintx) { inty=fath[x],z=Fath[y],l,r; if(tree[y][1]==X) l=1;ElseL=2; R=3-M; if(!isroot (y)) { if(tree[z][1]==y) tree[z][1]=x; Elsetree[z][2]=x; } Fath[x]=z;fath[y]=x;fath[tree[x][r]]=y; TREE[Y][L]=tree[x][r];tree[x][r]=y; Pushup (y);p ushup (x);}voidSplay (intx) {Top=0; stack[++top]=x; for(inti=x;! IsRoot (i); i=Fath[i]) stack[++top]=Fath[i]; for(inti=top;i>=1; i--) pushdown (Stack[i]); while(!isroot (x)) { inty=fath[x],z=Fath[y]; if(!isroot (y)) { if((tree[y][1]==x) ^ (tree[z][1]==y)) rotate (x); Elserotate (y); } rotate (x); }}voidAccessintx) { intregis=0; while(x) {splay (x); tree[x][2]=Regis; Regis=x;x=Fath[x]; }}voidMakeroot (intx) {access (x); Splay (x); REV[X]^=1;}voidlink () {scanf ("%d%d",&x,&y); Makeroot (x); FATH[X]=y; Splay (x);}voidCut () {scanf ("%d%d",&x,&y); Makeroot (x); Access (y); Splay (y); tree[y][1]=0; fath[x]=0;}intFindintx) {access (x); splay (x); inty=x; while(tree[y][1]) y=tree[y][1]; returny;}voidquery () {scanf ("%d%d",&x,&y); intU=find (x), v=find (y); if(u==v) printf ("yes\n"); Elseprintf"no\n");}intMain () {scanf ("%d%d",&n,&m); for(intI=1; i<=n;i++) size[i]=1; for(intI=1; i<=m;i++) {scanf ("%s", s); if(s[0]=='C') Link (); Else if(s[0]=='D') cut (); Else if(s[0]=='Q') query (); } return 0;}
Bzoj 2049 Cave Survey