# 【BZOJ2049】 [Sdoi2008]Cave 洞穴勘测

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