# 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 //It is made by wfj_2048~
2 #include <algorithm>
3 #include <iostream>
4 #include <cstring>
5 #include <cstdlib>
6 #include <cstdio>
7 #include <vector>
8 #include <cmath>
9 #include <queue>
10 #include <stack>
11 #include <map>
12 #include <set>
13 #define inf (1<<30)
14 #define il inline
15 #define RG register
16 #define ll long long
17
18 using namespace std;
19
20 int ch[10010][2],fa[10010],st[10010],lazy[10010],n,m;
21 char s[10];
22
23 il int gi(){
24     RG int x=0,q=1; RG char ch=getchar(); while ((ch<'0' || ch>'9') && ch!='-') ch=getchar();
25     if (ch=='-') q=-1,ch=getchar(); while (ch>='0' && ch<='9') x=x*10+ch-48,ch=getchar(); return q*x;
26 }
27
28 il void pushdown(RG int x){ lazy[x]=0,lazy[ch[x][0]]^=1,lazy[ch[x][1]]^=1,swap(ch[x][0],ch[x][1]); return; }
29
30 il int isroot(RG int x){ return ch[fa[x]][0]!=x && ch[fa[x]][1]!=x; }
31
32 il void rotate(RG int x){
33     RG int y=fa[x],z=fa[y],k=(ch[y][0]==x);
34     if (!isroot(y)) ch[z][ch[z][1]==y]=x;
35     fa[x]=z,fa[ch[x][k]]=y,ch[y][k^1]=ch[x][k];
36     fa[y]=x,ch[x][k]=y; return;
37 }
38
39 il void splay(RG int x){
40     RG int top=0; st[++top]=x;
41     for (RG int i=x;!isroot(i);i=fa[i]) st[++top]=fa[i];
42     for (RG int i=top;i;--i) if (lazy[st[i]]) pushdown(st[i]);
43     while (!isroot(x)){
44     RG int y=fa[x],z=fa[y];
45     if (!isroot(y)){
46         if ((ch[y][0]==x)^(ch[z][0]==y)) rotate(x); else rotate(y);
47     }
48     rotate(x);
49     }
50     return;
51 }
52
53 il void access(RG int x){ RG int t=0; while (x) splay(x),ch[x][1]=t,t=x,x=fa[x]; return; }
54
55 il void makeroot(RG int x){ access(x),splay(x),lazy[x]^=1; return; }
56
57 il void link(RG int x,RG int y){ makeroot(x),fa[x]=y; return; }
58
59 il void cut(RG int x,RG int y){ makeroot(x),access(y),splay(y),ch[y][0]=fa[x]=0; return; }
60
61 il int find(RG int x){ access(x),splay(x); while (ch[x][0]) x=ch[x][0]; return x; }
62
63 il void work(){
64     n=gi(),m=gi(); RG int u,v;
65     for (RG int i=1;i<=m;++i){
66     scanf("%s",s); u=gi(),v=gi();
67     if (s[0]=='C') link(u,v); if (s[0]=='D') cut(u,v);
68     if (s[0]=='Q') printf("%s\n",(find(u)==find(v)) ? "Yes" : "No");
69     }
70     return;
71 }
72
73 int main(){
74     work();
75     return 0;
76 }

posted @ 2017-02-19 09:38  wfj_2048  阅读(97)  评论(0编辑  收藏