# bzoj 2049: [Sdoi2008]Cave 洞穴勘测

Time Limit: 10 Sec  Memory Limit: 259 MB
Submit: 5550  Solved: 2510
[Submit][Status][Discuss]

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

 1 #include<iostream>
2 #include<cstdio>
3 #include<cstdlib>
4 #include<cmath>
5 #include<cstring>
6 #include<algorithm>
7 #include<vector>
8 #include<queue>
9 using namespace std;
10 const int maxn=20000;
11 int N,M;
12 char s[50];
13 int c[maxn][2],fa[maxn],st[maxn];
14 bool rev[maxn];
15 bool isroot(int x){
16     return x!=c[fa[x]][0]&&x!=c[fa[x]][1];
17 }
18 void pushdown(int x){
19     int l=c[x][0],r=c[x][1];
20     if(rev[x]){
21         rev[x]^=1; rev[l]^=1; rev[r]^=1;
22         swap(c[x][0],c[x][1]);
23     }
24 }
25 void rotate(int x){
26     int y=fa[x],z=fa[y],l,r;
27     if(x==c[y][0]) l=0;else l=1; r=l^1;
28     if(!isroot(y)){
29         if(y==c[z][0]) c[z][0]=x;
30         else c[z][1]=x;
31     }
32     fa[x]=z; fa[y]=x; fa[c[x][r]]=y;
33     c[y][l]=c[x][r]; c[x][r]=y;
34 }
35 void splay(int x){
36     int top=0; st[++top]=x;
37     for(int i=x;isroot(i)==false;i=fa[i]){
38         st[++top]=fa[i];
39     }
40     for(int i=top;i;i--) pushdown(st[i]);
41     while(!isroot(x)){
42         int y=fa[x],z=fa[y];
43         if(!isroot(y)){
44             if((x==c[y][0])&&(y==c[z][0])||(x==c[y][1])&&(y==c[z][1])){
45                 rotate(y),rotate(x);
46             }
47             else rotate(x),rotate(x);
48         }
49         else rotate(x);
50     }
51 }
52 void access(int x){
53     int t=0;
54     while(x!=0){
55         splay(x);
56         c[x][1]=t;
57         t=x; x=fa[x];
58     }
59 }
60 void rever(int x){
61     access(x); splay(x); rev[x]^=1;
62 }
63 void cut(int x,int y){
64     rever(x); access(y); splay(y); c[y][0]=fa[x]=0;
65 }
67     rever(x); fa[x]=y; splay(x);
68 }
69 int find(int x){
70     access(x); splay(x);
71     while(c[x][0]) x=c[x][0];
72     return x;
73 }
74 int main(){
75     scanf("%d%d",&N,&M);
76     for(int i=1,u,v;i<=M;i++){
77         scanf("%s%d%d",s,&u,&v);
78         if(s[0]=='Q'){
79             if(find(u)==find(v)) puts("Yes");
80             else puts("No");
81         }
86 }