再次冒着巨大风险,A了一道裸得不能在裸的题,lct基本操作。
贴板:
#include<cstdio>
#include<algorithm>
#define fo(i,a,b) for (int (i)=(a);(i)<=(b);(i)++)
#define lc c[x][0]
#define rc c[x][1]
using namespace std;
const int N=10000+255;
int c[N][2],f[N],rt,r[N];
int n,m,x,y,k;
char s[20];
void R(int &x){
int t=0; char ch;
for (ch=getchar();!('0'<=ch&&ch<='9');ch=getchar());
for (;('0'<=ch&&ch<='9');ch=getchar()) t=t*10+ch-'0';
x=t;
}
bool wh(int x){
return x==c[f[x]][1];
}
bool nroot(int x){
return x==c[f[x]][0]||x==c[f[x]][1];
}
void rotate(int x){
int y=f[x],z=f[y],k=wh(x),w=c[x][1^k];
if (nroot(y)) c[z][wh(y)]=x; f[x]=z;
c[y][k]=w; if (w) f[w]=y;
c[x][1^k]=y; f[y]=x;
}
void pushr(int x){
swap(lc,rc); r[x]^=1;
}
void pushdown(int x){
if (r[x]){
if (lc) pushr(lc);
if (rc) pushr(rc);
r[x]=0;
}
}
void pd(int x){
if (nroot(x)) pd(f[x]);
pushdown(x);
}
void splay(int x){
pd(x);
for (;nroot(x);rotate(x))
if (nroot(f[x]))
rotate(wh(f[x])==wh(x)?f[x]:x);
}
void access(int x){
for (int y=0;x;x=f[y=x])
splay(x),rc=y;
}
void make(int x){
access(x); splay(x);
pushr(x);
}
void link(int x,int y){
make(x);
f[x]=y;
}
void cut(int x,int y){
make(x);
access(y); splay(y);
c[y][0]=f[x]=0;
}
int find(int x){
access(x); splay(x);
while (lc) pushdown(x),x=lc;
splay(x);
return x;
}
bool pd(int x,int y){
make(x);
return x==find(y);
}
int main(){
//freopen("bzoj2049.in","r",stdin);
//freopen("bzoj2049.out","w",stdout);
R(n);R(m);
fo(i,1,m){
scanf("%s",s+1);R(x);R(y);
switch(s[1]){
case 'D':cut(x,y);break;
case 'C':link(x,y);break;
case 'Q':if(pd(x,y)) puts("Yes"); else puts("No");
}
}
return 0;
}