1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int MN=10002;
 4 struct Node {
 5     bool rev;
 6     Node *parent, *child[2];
 7 }_memory[MN],*EMPTY=_memory;
 8 inline void clear(Node* const x) {
 9     if (x==EMPTY) return;
10     if (x->rev) {
11         x->child[0]->rev^=1;
12         x->child[1]->rev^=1;
13         swap(x->child[0],x->child[1]);
14         x->rev=false;
15     }
16 }
17 inline void rotate(Node* const x,const int c) {
18     Node* const y=x->parent;
19     y->child[c^1]=x->child[c];
20     if (x->child[c]!=EMPTY) x->child[c]->parent=y;
21     x->parent=y->parent;
22     if (y->parent->child[0]==y) x->parent->child[0]=x;
23     else if (y->parent->child[1]==y) x->parent->child[1]=x;
24     x->child[c]=y;
25     y->parent=x;
26 }
27 inline bool splayy(Node* const x,Node* (&y)) {
28     return (y=x->parent)!=EMPTY && (y->child[0]==x||y->child[1]==x); 
29 }
30 inline void splay(Node *const x) {
31     clear(x);
32     for (Node *y,*z;splayy(x,y);)
33         if(splayy(y,z)) {
34             clear(z),clear(y),clear(x);
35             const int c=y==z->child[0];
36             if(x==y->child[c]) rotate(x,c^1),rotate(x,c);
37             else rotate(y,c),rotate(x,c);
38         } else {
39             clear(y);clear(x);
40             rotate(x,x==y->child[0]);
41         }
42 }
43 inline Node* access(Node *u) {
44     Node* v=EMPTY;
45     for(;u!=EMPTY;u=u->parent) {
46         splay(u);
47         u->child[1]=v;
48         v=u;
49     }
50     return v;
51 }
52 inline Node* getroot(Node* x) {
53     for (x=access(x);clear(x),x->child[0]!=EMPTY;x=x->child[0]);
54     return x;
55 }
56 inline void makeroot(Node* const x) {
57     access(x)->rev^=1;
58     splay(x);
59 }
60 void link(Node* const x,Node* const y) {
61     makeroot(x);
62     x->parent=y;
63     access(x);
64 }
65 inline void cut(Node* const x,Node* const y) {
66     makeroot(x);
67     access(y),splay(y);
68     y->child[0]->parent=EMPTY;
69     y->child[0]=EMPTY;
70 }
71 int n,m;
72 int main() {
73     scanf("%d%d",&n,&m);
74     for (int i=0;i<=n;++i) {
75         Node* const node=_memory+i;
76         node->child[0]=node->child[1]=node->parent=EMPTY;
77     }
78     for (int i=0,x,y;i<m;++i) {
79         char buf[10];
80         scanf("\n%s%d%d",buf,&x,&y);
81         Node *ra, *rb;
82         switch(buf[0]) {
83             case ‘Q‘:
84                 ra=getroot(_memory+x),rb=getroot(_memory+y);
85                 printf(ra==rb&&ra!=EMPTY?"Yes\n":"No\n");
86                 break;
87             case ‘D‘: cut(_memory+x,_memory+y); break;
88             case ‘C‘: link(_memory+x,_memory+y);
89         }
90     }
91 }