1 #include <bits/stdc++.h>
2 using namespace std;
3 const int maxn = 20010;
4 struct LCT {
5 int fa[maxn],ch[maxn][2],parent[maxn];
6 bool flip[maxn];
7 inline void pushdown(int x) {
8 if(flip[x]) {
9 if(ch[x][0]) flip[ch[x][0]] ^= 1;
10 if(ch[x][1]) flip[ch[x][1]] ^= 1;
11 swap(ch[x][0],ch[x][1]);
12 flip[x] = false;
13 }
14 }
15 inline void pushup(int x) {
16
17 }
18 void init(int n) {
19 for(int i = 0; i <= n; ++i)
20 parent[i] = ch[i][0] = ch[i][1] = fa[i] = flip[i] = 0;
21 }
22 void rotate(int x,int k) {
23 int y = fa[x];
24 pushdown(y);
25 pushdown(x);
26 ch[y][!k] = ch[x][k];
27 fa[ch[x][k]] = y;
28 fa[x] = fa[y];
29 if(fa[x]) ch[fa[x]][y == ch[fa[x]][1]] = x;
30 ch[x][k] = y;
31 fa[y] = x;
32 pushup(y);
33 }
34 void splay(int x) {
35 pushdown(x);
36 int y = x;
37 while(fa[y]) y = fa[y];
38 if(y != x) {
39 parent[x] = parent[y];
40 parent[y] = 0;
41 while(fa[x]) {
42 pushdown(fa[fa[x]]);
43 pushdown(fa[x]);
44 if(!fa[fa[x]]) rotate(x,x == ch[fa[x]][0]);
45 else {
46 int y = fa[x],z = fa[y],s = (y == ch[z][0]);
47 if(x == ch[y][s]) {
48 rotate(x,s^1);
49 rotate(x,s);
50 } else {
51 rotate(y,s);
52 rotate(x,s);
53 }
54 }
55 }
56 }
57 pushup(x);
58 }
59 void access(int x) {
60 for(int y = 0; x; x = parent[x]) {
61 splay(x);
62 parent[ch[x][1]] = x;
63 parent[y] = 0;
64 fa[ch[x][1]] = 0;
65 ch[x][1] = y;
66 fa[y] = x;
67 y = x;
68 pushup(x);
69 }
70 }
71 int findroot(int x) {
72 access(x);
73 splay(x);
74 while(ch[x][0]) x = ch[x][0];
75 return x;
76 }
77 void makeroot(int x) {
78 access(x);
79 splay(x);
80 flip[x] ^= 1;
81 }
82 void cut(int x,int y) {
83 makeroot(x);
84 access(y);
85 splay(y);
86 fa[ch[y][0]] = 0;
87 ch[y][0] = 0;
88 }
89 void link(int x,int y) {
90 makeroot(y);
91 parent[y] = x;
92 access(y);
93 }
94 bool query(int x,int y) {
95 return findroot(x) == findroot(y);
96 }
97 } lct;
98 int main() {
99 int n,m,u,v;
100 char op[50];
101 while(~scanf("%d%d",&n,&m)) {
102 lct.init(n);
103 while(m--) {
104 scanf("%s%d%d",op,&u,&v);
105 if(op[0] == ‘Q‘) puts(lct.query(u,v)?"Yes":"No");
106 else if(op[0] == ‘C‘) {
107 if(u == v || lct.query(u,v)) continue;
108 lct.link(u,v);
109 } else if(op[0] == ‘D‘) {
110 if(u == v || !lct.query(u,v)) continue;
111 lct.cut(u,v);
112 }
113 }
114 }
115 return 0;
116 }
117 /*
118 200 5
119 Query 123 127
120 Connect 123 127
121 Query 123 127
122 Destroy 127 123
123 Query 123 127
124 3 5
125 Connect 1 2
126 Connect 3 1
127 Query 2 3
128 Destroy 1 3
129 Query 2 3
130 */