LCT entry question?
We need to change the root, tat.
This is probably not a very mature version.
1#include <iostream>2#include <cstring>3#include <cstdlib>4#include <algorithm>5#include <cstdio>6 using namespacestd;7 8 Const intMAXN =10010;9 Ten intch[maxn][2],P[MAXN],FLIP[MAXN]; One intn,m; A - Const intD=15000000; - Char inch[D], out[300010*Ten],*i=inch, *o= out; the #defineGC (*i++) - #definePC (x) ((*o++) =x) -Template <typename q> - voidGT (q&x) { + Static Charc,f; - for(c=gc,f=0;! IsDigit (c); c=gc)if(c=='-') f=1; + for(x=0; IsDigit (c); c=gc) x=x*Ten+c-'0'; AF && (x=-x); at } - -Template <typename q> - voidpt (Q x) { - Static Charstk[ -]; - Static inttop; intop=0; - if(x==0) PC ('0'); to for(; x;x/=Ten) Stk[++top] = x percentTen+'0'; + for(; top;top--) PC (Stk[top]); - } the * voidDownintx) { $ if(Flip[x]) {Panax NotoginsengSwap (ch[x][0],ch[x][1]); -flip[ch[x][0]]^=1; theflip[ch[x][1]]^=1; +flip[x]=0; A } the } + - BOOLIsRootintx) { $ returnch[p[x]][0]!=x && ch[p[x]][1]!=x; $ } - - voidRotateintx) { the inty=p[x],z=P[y]; - intl=ch[y][1]==x,r=l^1;Wuyi if(!isroot (y)) ch[z][ch[z][1]==y]=x; thep[ch[x][r]]=y; -p[y]=x; Wup[x]=Z; - Aboutch[y][l]=Ch[x][r]; $ch[x][r]=y; - } - - intStk[maxn],top; A voidSplay (intx) { +stk[top=1]=x; the for(intt=x;! IsRoot (t); T=p[t]) stk[++top]=P[t]; - for(; top;top--) down (Stk[top]); $ the for(;!isroot (x);) { the inty=p[x],z=P[y]; the if(!isroot (y)) { the if((ch[y][1]==x) ^ (ch[z][1]==y)) -Rotate (x);Elserotate (y); in } the rotate (x); the } About } the the voidAccessintx) { the for(intt=0; x;x=p[t=X]) { + splay (x); -ch[x][1]=T; the }Bayi } the the intGetroot (intx) { - for(Access (x), splay (x); ch[x][0];x=ch[x][0]); - returnx; the } the the voidNewroot (intx) { the access (x); - splay (x); theflip[x]^=1; the } the 94 voidCutintx) { the access (x); the splay (x); thep[ch[x][0]]=0;98ch[x][0]=0; About } - 101 voidCombineintXinty) {102 newroot (x);103p[x]=y;104 } the 106 voidWork () {107 GT (N), GT (m);108 CharC;109 intb; the for(intI=1; i<=m;i++) {111 for(; c=gc,c!='Q'&& c!='C'&&c!='D';); the GT (a), GT (b);113 if(c=='Q') printf ("%s\n", Getroot (a) ==getroot (b)?"Yes":"No"); the Else if(c=='C') combine (a, b); the ElseNewroot (a), cut (b); the }117 }118 119 intMain () { - #ifdef DEBUG121Freopen ("In.txt","R", stdin);122Freopen ("OUT.txt","W", stdout);123 #endif124Freadinch,1, D,stdin); the 126 Work ();127 - returnprintf out),0;129}
bzoj2049: [Sdoi2008]cave Cave Survey