Lct
The Link-cut-tree ...
SrO ZYF
Http://www.cnblogs.com/zyfzyf/p/4149109.html
Visually, I'm the No. 222 one? .................. Don't mind the details ...
There are some differences between the splay and the previous writing ...
such as splay in the push_down of the wording ... And rotate's new posture ~
Algorithm to look at the paper or something ... Very understood
1 /**************************************************************2 problem:32823 User:tunix4 language:c++5 result:accepted6 time:2596 Ms7 memory:8596 KB8 ****************************************************************/9 Ten //Bzoj 3282 One#include <cstdio> A#include <vector> -#include <cstring> -#include <cstdlib> the#include <iostream> -#include <algorithm> - #defineRep (i,n) for (int i=0;i<n;++i) - #defineF (i,j,n) for (int i=j;i<=n;++i) + #defineD (i,j,n) for (int i=j;i>=n;--i) - #definePB Push_back + using namespacestd; A Const intn=300010; at #defineDebug - - intc[n][2],fa[n],v[n],s[n],st[n],top=0; - BOOLRev[n]; - intn,m; - in voidPUSH_UP (intx) { - intl=c[x][0],r=c[x][1]; tos[x]=s[l]^s[r]^V[x]; + } - voidPush_down (intx) { the intl=c[x][0],r=c[x][1]; * if(Rev[x]) { $rev[x]^=1; rev[l]^=1; rev[r]^=1;Panax NotoginsengSwap (c[x][0],c[x][1]); - } the } + BOOLIsRootintx) { A returnc[fa[x]][0]!=x && c[fa[x]][1]!=x; the } + voidRotateintx) { - inty=fa[x],z=fa[y],l=c[y][1]==x,r=l^1; $ if(!isroot (y)) c[z][c[z][1]==y]=x; $Fa[x]=z; Fa[y]=x; fa[c[x][r]]=y; -C[Y][L]=C[X][R]; c[x][r]=y; - push_up (y); PUSH_UP (x); the } - voidSplay (intx) {Wuyitop=0; st[top++]=x; the for(inti=x;! IsRoot (i); i=Fa[i]) -st[top++]=Fa[i]; Wu while(top--) Push_down (St[top]); - About while(!isroot (x)) { $ inty=fa[x],z=Fa[y]; - if(!isroot (y)) { - if(c[y][0]==x^c[z][0]==y) rotate (x); - Elserotate (y); A } + rotate (x); the } - } $ voidAccessintx) { the for(intt=0; x;t=x,x=Fa[x]) theSplay (x), c[x][1]=t,push_up (x); the } the voidMakeroot (intx) { -Access (x); Splay (x); rev[x]^=1; in } the intFindintx) { the access (x); splay (x); About while(c[x][0]) x=c[x][0]; the returnx; the } the voidCutintXinty) { + makeroot (x); Access (y); splay (y) ; - if(c[y][0]==X) c[y][0]=fa[x]=0; the }Bayi voidLinkintXinty) { theMakeroot (x); fa[x]=y; the } - //LCT End - intMain () { the #ifndef Online_judge theFreopen ("file.in","R", stdin); the //freopen ("File.out", "w", stdout); the #endif -scanf"%d%d",&n,&m); theF (I,1, n) {scanf ("%d", &v[i]); s[i]=v[i];} the the intc,x,y;94F (I,1, M) { thescanf"%d%d%d",&c,&x,&y); the Switch(c) { the Case 0: Makeroot (x); Access (y); Splay (y); printf"%d\n", S[y]); Break;98 Case 1:if(Find (x)!=find (y)) Link (x, y); Break; About Case 2:if(Find (x) ==find (y)) cut (x, y); Break; - Case 3: Access (x); Splay (x); V[x]=y; PUSH_UP (x); Break;101 }102 }103 return 0;104}
View Code
"Bzoj" "3282" Tree