# 洞穴勘测（bzoj 2049）

200 5
Query 123 127
Connect 123 127
Query 123 127
Destroy 127 123
Query 123 127

3 5
Connect 1 2
Connect 3 1
Query 2 3
Destroy 1 3
Query 2 3

No
Yes
No

Yes
No

## HINT

/*LCT裸题*/
#include<cstdio>
#include<iostream>
#define N 10010
using namespace std;
int n,m;
int fa[N],c[N][2],st[N],rev[N];
bool isroot(int x){
return c[fa[x]][0]!=x&&c[fa[x]][1]!=x;
}
void pushdown(int x){
int l=c[x][0],r=c[x][1];
if(rev[x]){
rev[l]^=1;rev[r]^=1;rev[x]^=1;
swap(c[x][0],c[x][1]);
}
}
void rotate(int x){
int y=fa[x],z=fa[y],l,r;
if(c[y][0]==x) l=0;else l=1;r=l^1;
if(!isroot(y)){
if(c[z][0]==y) c[z][0]=x;else c[z][1]=x;
}
fa[x]=z;fa[y]=x;fa[c[x][r]]=y;
c[y][l]=c[x][r];c[x][r]=y;
}
void splay(int x){
int top=0;st[++top]=x;
for(int i=x;!isroot(i);i=fa[i])
st[++top]=fa[i];
for(int i=top;i;i--) pushdown(st[i]);
while(!isroot(x)){
int y=fa[x],z=fa[y];
if(!isroot(y)){
if(c[y][0]==x^c[z][0]==y) rotate(x);
else rotate(y);
}
rotate(x);
}
}
void access(int x){
int t=0;
while(x){
splay(x);
c[x][1]=t;
t=x;x=fa[x];
}
}
void makeroot(int x){
access(x);
splay(x);
rev[x]^=1;
}
void join(int x,int y){
makeroot(x);
fa[x]=y;
splay(x);
}
void cut(int x,int y){
makeroot(x);
access(y);
splay(y);
c[y][0]=fa[x]=0;
}
int find(int x){
access(x);
splay(x);
int y=x;
while(c[y][0]) y=c[y][0];
return y;
}
int main(){
char ch[10];int x,y;
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++){
scanf("%s%d%d",ch,&x,&y);
if(ch[0]=='C') join(x,y);
else if(ch[0]=='D') cut(x,y);
else {
if(find(x)==find(y)) printf("Yes\n");
else printf("No\n");
}
}
return 0;
}

posted @ 2017-01-16 18:01  karles~  阅读(239)  评论(0编辑  收藏