BZOJ 2049: [Sdoi2008]Cave 洞穴勘测(LCT裸题)
题目链接:BZOJ 2049: [Sdoi2008]Cave 洞穴勘测
题意:
三个操作.
1 链接x y
2 断开x y
3 询问x y是否连通
题解:
LCT裸题
1 #include<bits/stdc++.h> 2 #define F(i,a,b) for(int i=a;i<=b;i++) 3 #define mst(a,b) memset(a,b,sizeof(a)) 4 using namespace std; 5 6 namespace LCT 7 { 8 const int N=1e4+7; 9 int f[N],son[N][2],val[N],sum[N],tmp[N],lazy[N]; 10 int g[N],v[N*2],nxt[N*2],ed;bool rev[N]; 11 void clear(int n) 12 { 13 F(i,1,n)f[i]=son[i][0]=son[i][1]=0; 14 F(i,1,n)rev[i]=lazy[i]=g[i]=0;ed=0; 15 } 16 void adg(int x,int y){v[++ed]=y,nxt[ed]=g[x],g[x]=ed;} 17 void build(int x=1){ 18 sum[x]=val[x]; 19 for(int i=g[x];i;i=nxt[i]) 20 if(!f[v[i]]&&v[i]!=1)f[v[i]]=x,build(v[i]); 21 } 22 bool isroot(int x){return !f[x]||son[f[x]][0]!=x&&son[f[x]][1]!=x;} 23 void rev1(int x){if(!x)return;swap(son[x][0],son[x][1]);rev[x]^=1;} 24 void add(int x,int c){if(!x)return;sum[x]+=c,val[x]+=c,lazy[x]+=c;} 25 void pb(int x){ 26 if(rev[x])rev1(son[x][0]),rev1(son[x][1]),rev[x]=0; 27 if(lazy[x])add(son[x][0],lazy[x]),add(son[x][1],lazy[x]),lazy[x]=0; 28 } 29 void up(int x){ 30 sum[x]=val[x]; 31 if(son[x][0])sum[x]=max(sum[son[x][0]],sum[x]); 32 if(son[x][1])sum[x]=max(sum[son[x][1]],sum[x]); 33 } 34 void rotate(int x){ 35 int y=f[x],w=son[y][1]==x; 36 son[y][w]=son[x][w^1]; 37 if(son[x][w^1])f[son[x][w^1]]=y; 38 if(f[y]){ 39 int z=f[y]; 40 if(son[z][0]==y)son[z][0]=x;else if(son[z][1]==y)son[z][1]=x; 41 } 42 f[x]=f[y];f[y]=x;son[x][w^1]=y;up(y); 43 } 44 void splay(int x){ 45 int s=1,i=x,y;tmp[1]=i; 46 while(!isroot(i))tmp[++s]=i=f[i]; 47 while(s)pb(tmp[s--]); 48 while(!isroot(x)){ 49 y=f[x]; 50 if(!isroot(y)){if((son[f[y]][0]==y)^(son[y][0]==x))rotate(x);else rotate(y);} 51 rotate(x); 52 } 53 up(x); 54 } 55 void access(int x){for(int y=0;x;y=x,x=f[x])splay(x),son[x][1]=y,up(x);} 56 int root(int x){access(x);splay(x);while(son[x][0])x=son[x][0];return x;} 57 void makeroot(int x){access(x);splay(x);rev1(x);} 58 void link(int x,int y){makeroot(x);f[x]=y;access(x);} 59 void cutf(int x){access(x);splay(x);f[son[x][0]]=0;son[x][0]=0;up(x);} 60 void cut(int x,int y){makeroot(x);cutf(y);} 61 void update(int x,int y,int c){makeroot(x),access(y),splay(y),add(y,c);} 62 int ask(int x,int y){makeroot(x);access(y);splay(y);return sum[y];} 63 } 64 65 int n,m,x,y; 66 char op[20]; 67 using namespace LCT; 68 int main() 69 { 70 scanf("%d%d",&n,&m); 71 F(i,1,m) 72 { 73 scanf("%s%d%d",op,&x,&y); 74 if(*op=='C'&&root(x)!=root(y))link(x,y); 75 else if(*op=='D'&&root(x)==root(y))cut(x,y); 76 else if(*op=='Q')puts(root(x)==root(y)?"Yes":"No"); 77 } 78 return 0; 79 }