1 #include<cstdio>
2 #include<cstdlib>
3 #include<cmath>
4 #include<cstring>
5 #include<algorithm>
6 #include<iostream>
7 #include<vector>
8 #include<map>
9 #include<set>
10 #include<queue>
11 #include<string>
12 #define inf 1000000000
13 #define maxn 20000+1000
14 #define maxm 500+100
15 #define eps 1e-10
16 #define ll long long
17 #define pa pair<int,int>
18 using namespace std;
19 inline int read()
20 {
21 int x=0,f=1;char ch=getchar();
22 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
23 while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();}
24 return x*f;
25 }
26 int fa[maxn],c[maxn][2],sta[maxn],n,m;
27 bool rev[maxn];
28 inline bool isroot(int x)
29 {
30 return c[fa[x]][0]!=x&&c[fa[x]][1]!=x;
31 }
32 void rotate(int x)
33 {
34 int y=fa[x],z=fa[y],l=(c[y][1]==x),r=l^1;
35 if(!isroot(y))c[z][c[z][1]==y]=x;
36 fa[x]=z;fa[y]=x;fa[c[x][r]]=y;
37 c[y][l]=c[x][r];c[x][r]=y;
38 }
39 void pushdown(int x)
40 {
41 if(!rev[x])return;
42 rev[x]^=1;rev[c[x][0]]^=1;rev[c[x][1]]^=1;
43 swap(c[x][0],c[x][1]);
44 }
45 void splay(int x)
46 {
47 int top=0;sta[++top]=x;
48 for(int y=x;!isroot(y);y=fa[y])sta[++top]=fa[y];
49 for(;top;)pushdown(sta[top--]);
50 while(!isroot(x))
51 {
52 int y=fa[x],z=fa[y];
53 if(!isroot(y))
54 {
55 if(c[z][0]==y^c[y][0]==x)rotate(x);else rotate(y);
56 }
57 rotate(x);
58 }
59
60 }
61 void access(int x)
62 {
63 for(int y=0;x;x=fa[x])
64 {
65 splay(x);c[x][1]=y;y=x;
66 }
67 }
68 void makeroot(int x)
69 {
70 access(x);splay(x);rev[x]^=1;
71 }
72 int find(int x)
73 {
74 access(x);splay(x);
75 while(c[x][0])x=c[x][0];
76 return x;
77 }
78 void link(int x,int y)
79 {
80 makeroot(x);fa[x]=y;splay(x);
81 }
82 void cut(int x,int y)
83 {
84 makeroot(x);access(y);splay(y);c[y][0]=fa[x]=0;
85 }
86 int main()
87 {
88 freopen("input.txt","r",stdin);
89 freopen("output.txt","w",stdout);
90 n=read();m=read();
91 char ch[10];int x,y;
92 while(m--)
93 {
94 scanf("%s",ch);x=read();y=read();
95 switch(ch[0])
96 {
97 case ‘C‘:link(x,y);break;
98 case ‘D‘:cut(x,y);break;
99 case ‘Q‘:{if(find(x)==find(y))printf("Yes\n");else printf("No\n");}break;
100 }
101 }
102 return 0;
103 }