1 #include <bits/stdc++.h>
2 using namespace std;
3 struct LCT
4 {
5 int c[2], fa, rev;
6 int& operator [] (int i)
7 {
8 return c[i];
9 }
10 }a[10005];
11 int top, sta[10005];
12
13 void scanf(int &x)
14 {
15 char ch = getchar();
16 x = 0;
17 while(ch < ‘0‘ || ch > ‘9‘)
18 ch = getchar();
19 while(ch >= ‘0‘ && ch <= ‘9‘)
20 x = x * 10 + ch - 48, ch = getchar();
21 }
22
23 void push_down(int k)
24 {
25 if(a[k].rev)
26 {
27 a[a[k][0]].rev ^= 1, a[a[k][1]].rev ^= 1;
28 swap(a[k][0], a[k][1]), a[k].rev = 0;
29 }
30 }
31
32 bool isroot(int x)
33 {
34 return a[a[x].fa][0] != x && a[a[x].fa][1] != x;
35 }
36
37 void rotate(int x)
38 {
39 int y = a[x].fa, z = a[y].fa;
40 int dy = a[y][1] == x, dz = a[z][1] == y;
41 if(!isroot(y)) a[z][dz] = x;
42 a[y][dy] = a[x][dy ^ 1], a[a[x][dy ^ 1]].fa = y;
43 a[x][dy ^ 1] = y, a[y].fa = x, a[x].fa = z;
44 }
45
46 void splay(int x)
47 {
48 sta[top = 1] = x;
49 for(int i = x; !isroot(i); i = a[i].fa)
50 sta[++top] = a[i].fa;
51 while(top)
52 push_down(sta[top--]);
53 while(!isroot(x))
54 {
55 int y = a[x].fa, z = a[y].fa;
56 if(!isroot(y))
57 if(a[y][1] == x ^ a[z][1] == y) rotate(x);
58 else rotate(y);
59 rotate(x);
60 }
61 }
62
63 void access(int x)
64 {
65 for(int i = 0; x; x = a[x].fa)
66 splay(x), a[x][1] = i, i = x;
67 }
68
69 void make_root(int x)
70 {
71 access(x), splay(x), a[x].rev ^= 1;
72 }
73
74 void link(int x, int y)
75 {
76 make_root(x), a[x].fa = y;
77 }
78
79 void cut(int x, int y)
80 {
81 make_root(x), access(y), splay(y), a[y][0] = a[x].fa = 0;
82 }
83
84 int find_root(int x)
85 {
86 access(x), splay(x);
87 while(a[x][0])
88 x = a[x][0];
89 return x;
90 }
91
92 int main()
93 {
94 int n, m, x, y;
95 char op[10];
96 scanf("%d%d", &n, &m);
97 while(m--)
98 {
99 scanf("%s", op);
100 scanf(x), scanf(y);
101 if(op[0] == ‘C‘) link(x, y);
102 else if(op[0] == ‘D‘) cut(x, y);
103 else puts(find_root(x) == find_root(y) ? "Yes" : "No");
104 }
105 return 0;
106 }