Reprint Please specify source: http://www.cnblogs.com/fraud/--by fraud
Dynamic Tree entry questions, no need to maintain any information.
I use the splay, subscript implementation of the LCT.
1#include <iostream>2#include <sstream>3#include <ios>4#include <iomanip>5#include <functional>6#include <algorithm>7#include <vector>8#include <string>9#include <list>Ten#include <queue> One#include <deque> A#include <stack> -#include <Set> -#include <map> the#include <cstdio> -#include <cstdlib> -#include <cmath> -#include <cstring> +#include <climits> -#include <cctype> + using namespacestd; A #defineXinf Int_max at #defineINF 0X3FFFFFFF - #defineMP (x, y) make_pair (x, y) - #definePB (x) push_back (x) - #defineREP (x,n) for (int x=0; x<n; X + +) - #defineREP2 (X,L,R) for (int x=l; x<=r; X + +) - #defineDEP (x,r,l) for (int x=r; x>=l; x--) in #defineCLR (a,x) memset (a,x,sizeof (A)) - #defineIT iterator totypedefLong Longll; +typedef pair<int,int>PII; -typedef vector<pii>VII; thetypedef vector<int>VI; * #defineMAXN 100010 $ intch[maxn][2],KEY[MAXN],PRE[MAXN],SIZE[MAXN],SS[MAXN];Panax Notoginseng intREV[MAXN]; - voidPush_down (intR) { the if(!R)return; + if(Rev[r]) { Arev[ch[r][0]]^=1; therev[ch[r][1]]^=1; +Swap (ch[r][0],ch[r][1]); -rev[r]=0; $ } $ } - voidRotateintXintd) { - inty=Pre[x]; thech[y][!d]=Ch[x][d]; - if(Ch[x][d]) pre[ch[x][d]]=y;Wuyipre[x]=Pre[y]; the if(ch[pre[y]][0]==y) ch[pre[x]][0]=x; - Else if(ch[pre[y]][1]==y) ch[pre[x]][1]=x; Wupre[y]=x; -ch[x][d]=y; About } $ BOOLCheckintXinty) { - returny&& (ch[y][0]==x| | ch[y][1]==x); - } - voidSplay (intx) { A Push_down (x); + inty,z; the while(Check (x,y=Pre[x])) { - if(Check (y,z=Pre[y])) { $ Push_down (z); the Push_down (y); the Push_down (x); the intD= (y==ch[z][0]); the if(X==ch[y][d]) {Rotate (x,!d), rotate (x,d);} - Else{rotate (y,d), rotate (x,d);} in}Else{ the Push_down (y); the Push_down (x); AboutRotate (x,ch[y][0]==x); the Break; the } the } + } - intAccessintu) { the intv=0;Bayi for(; u;u=Pre[u]) { the splay (u); thech[u][1]=v; -v=u; - } the //splay (u); the returnv; the } the intGetroot (intx) { - for(X=access (x);p Ush_down (x), ch[x][0];x=ch[x][0]); the returnx; the } the voidMakeroot (intx) {94Rev[access (x)]^=1; the splay (x); the } the voidLinkintXinty) {98 makeroot (x); Aboutpre[x]=y; - access (x);101 }102 voidCutintXinty) {103 makeroot (x);104 access (y); the splay (y);106pre[ch[y][0]]=0;107ch[y][0]=0;108 }109 voidInitintN) { the for(intI=0; i<=n;i++)111ch[i][0]=ch[i][1]=pre[i]=0; the }113 voidQueryintXinty) { the intRa=getroot (x); the intrb=getroot (y); the if(Ra==rb&&ra) printf ("yes\n");117 Elseprintf"no\n");118 }119 - intMain ()121 {122Ios::sync_with_stdio (false);123 intn,m;124 while(SCANF ("%d%d", &n,&m)! =EOF) { the init (n);126 intx, y;127 Chara[ the]; - for(intI=0; i<m;i++){129scanf"%s%d%d",a,&x,&y); the if(a[0]=='C') Link (x, y);131 Else if(a[0]=='D') Cut (x, y); the Elsequery (x, y);133 }134 }135 return 0;136}
Bzoj 2049 Cave Cave Survey (LCT)