# 【SDOI2008】【BZOJ2049】Cave 洞穴勘測

Description

Input

Output

Sample Input

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

Sample Output

No

Yes

No

Yes

No

HINT

Source

(请叫我写LCT必挂星人QAQ)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 10010
using namespace std;
int n,m,u,v;
char ch[20];
struct splay
{
int fa,ch[2];
bool rev;
}tree[MAXN];
int sta[MAXN],top;
void in(int &x)
{
char ch=getchar();int flag=1;x=0;
while (!(ch>='0'&&ch<='9')) flag=ch=='-'?-1:1,ch=getchar();
while (ch>='0'&&ch<='9')    x=x*10+ch-'0',ch=getchar();
}
inline bool is_root(int x)
{
return tree[tree[x].fa].ch[0]!=x&&tree[tree[x].fa].ch[1]!=x;
}
inline void push_down(int x)
{
if (tree[x].rev)
{
tree[x].rev^=1;tree[tree[x].ch[0]].rev^=1;tree[tree[x].ch[1]].rev^=1;
swap(tree[x].ch[0],tree[x].ch[1]);
}
}
inline void rot(int x)
{
int y=tree[x].fa,z=tree[y].fa,l,r;
l=(tree[y].ch[1]==x);r=l^1;
if (!is_root(y))    tree[z].ch[tree[z].ch[1]==y]=x;
tree[tree[x].ch[r]].fa=y;tree[y].fa=x;tree[x].fa=z;
tree[y].ch[l]=tree[x].ch[r];tree[x].ch[r]=y;
}
inline void Splay(int x)
{
sta[++top]=x;
for (int i=x;!is_root(i);i=tree[i].fa)  sta[++top]=tree[i].fa;
while (top) push_down(sta[top--]);
while (!is_root(x))
{
int y=tree[x].fa,z=tree[y].fa;
if (!is_root(y))
{
if (tree[y].ch[0]==x^tree[z].ch[0]==y)  rot(x);
else    rot(y);
}
rot(x);
}
}
inline void access(int x)
{
for (int t=0;x;t=x,x=tree[x].fa)    Splay(x),tree[x].ch[1]=t;
}
inline void make_root(int x)
{
access(x);Splay(x);tree[x].rev^=1;
}
{
make_root(x);tree[x].fa=y;
}
inline void cut(int x,int y)
{
make_root(x);access(y);Splay(y);tree[y].ch[0]=tree[x].fa=0;
}
inline int find(int x)
{
access(x);Splay(x);
int ret=x;
while (tree[ret].ch[0]) ret=tree[ret].ch[0];
return ret;
}
int main()
{
in(n);in(m);
while (m--)
{
scanf("%s",ch);in(u);in(v);
if (ch[0]=='Q') find(u)==find(v)?puts("Yes"):puts("No");
}