Bzoj2049[sdoi2008]cave Cave Survey
Test instructions
Some points, three kinds of operations: Point and Point Edge, point to point separation, ask whether two points are connected.
Exercises
The LCT is weaker than the one above, just remember to flip the line.
Code:
1#include <cstdio>2#include <cstring>3#include <algorithm>4 #defineInc (I,J,K) for (int i=j;i<=k;i++)5 #defineMAXN 500006 using namespacestd;7 8 intfa[maxn],ch[maxn][2],REV[MAXN];9 voidPushdown (intx) {Ten if(Rev[x]) { OneSwap (ch[x][0],ch[x][1]);if(ch[x][0]) rev[ch[x][0]]^=1;if(ch[x][1]) rev[ch[x][1]]^=1; rev[x]^=1; A } - } - BOOLIs_root (intx) { the if(x==0|| fa[x]==0)return 1;returnx!=ch[fa[x]][0]&&x!=ch[fa[x]][1]; - } - voidRotateintx) { - if(x==0|| Is_root (x))return; + intA1=FA[X],A2=FA[FA[X]],A3;BOOLB1= (x==ch[a1][1]), b2= (a1==ch[a2][1]), B3=is_root (A1); a3=ch[x][!B1]; - if(!B3) ch[a2][b2]=x; FA[X]=A2; CH[A1][B1]=A3; FA[A3]=A1; CH[X][!B1]=A1; fa[a1]=x; + } A intdts,dt[maxn],y; at voidSplay (intx) { - if(x==0)return; dts=0; Y=x; while(! Is_root (y)) dt[++dts]=y,y=Fa[y]; -Dt[++dts]=y; while(dts) pushdown (Dt[dts]), dts--; - while(!is_root (x)) { - if(! Is_root (Fa[x])) (x==ch[x][1]) ^ (fa[x]==ch[fa[fa[x]]][1])?Rotate (x): Rotate (fa[x]); - rotate (x); in } - } to intAccessintx) { + if(x==0)return 0;intt=0; - while(x) {splay (x); ch[x][1]=t;if(t) fa[t]=x; T=x; x=fa[x];} the returnT; * } $ voidLinkintXinty) {Panax Notoginseng if(x==0|| y==0)return; Access (x); Splay (x); rev[x]^=1; fa[x]=y; - } the voidCutintXinty) { + if(x==0|| y==0)return; Access (x); Splay (x); rev[x]^=1; Access (y); Splay (y); ch[y][0]=fa[x]=0; A } the intFindintx) { +Access (x); Splay (x); while(ch[x][0]) x=ch[x][0];returnx; - } $ intN,m;Chars[Ten]; $ intMain () { -scanf"%d", &n); Inc (I,1, N) fa[i]=ch[i][0]=ch[i][1]=0; scanf"%d",&m); -Inc (I,1, M) { the intb; scanf"%s%d%d",s,&a,&b); - if(s[0]=='C') Link (A, b);Wuyi if(s[0]=='D') Cut (A, b); the if(s[0]=='Q') Find (a) ==find (b)? puts ("Yes"):p UTS ("No"); - } Wu return 0; -}
20160422
Bzoj2049[sdoi2008]cave Cave Survey