传送门biu~
没人说模板题就不能做好久。。
#include<bits/stdc++.h>
using namespace std;
struct Node{
Node *ch[2],*fa;
bool rev_mark;
int dir(){
if(fa->ch[0]==this) return 0;
if(fa->ch[1]==this) return 1;
return -1;
}
Node();
void pushdown();
void rev();
}*null=new Node(),tree[100005];
Node :: Node(){
ch[0]=ch[1]=fa=null;
rev_mark=false;
}
void Node :: pushdown(){
if(this==null) return;
if(rev_mark){
ch[0]->rev();
ch[1]->rev();
rev_mark=false;
}
}
void Node :: rev(){
if(this==null) return;
rev_mark=!rev_mark;
swap(ch[0],ch[1]);
}
void To_pushdown(Node *o){
if(~(o->dir())) To_pushdown(o->fa);
o->pushdown();
}
void Rotate(Node *o,int d){
Node *k=o->ch[d^1];
int d2;
o->ch[d^1]=k->ch[d]; k->ch[d]->fa=o;
k->ch[d]=o;
if(~o->dir()) o->fa->ch[o->dir()]=k;
k->fa=o->fa;o->fa=k;
}
void Splay(Node *o){
int d; To_pushdown(o);
while(~o->dir()){
if(o->dir()==o->fa->dir()) Rotate(o->fa->fa,o->dir()^1);
Rotate(o->fa,o->dir()^1);
}
}
void Access(Node *o){
Node *p=null;
while(o!=null){
Splay(o);
o->ch[1]=p;
p=o;
o=o->fa;
}
}
void Move_to_root(Node *o){
Access(o); Splay(o);
o->rev();
}
void Link(Node *x,Node *y){
Move_to_root(x);
x->fa=y;
}
void Cut(Node *x,Node *y){
Move_to_root(x);
Access(y);Splay(y);
y->ch[0]=null;
x->fa=null;
}
inline void Query(Node *x,Node *y){
Access(x);
Splay(x);
while(y->fa!=null) y=y->fa;
if(y==x) printf("Yes\n");
else printf("No\n");
}
int main(){
null->ch[0]=null->ch[1]=null->fa=null;
char opt[105];
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=m;++i){
int x,y;
scanf("%s%d%d",opt,&x,&y);
if(opt[0]=='Q') Query(&tree[x],&tree[y]);
else if(opt[0]=='C') Link(&tree[x],&tree[y]);
else Cut(&tree[x],&tree[y]);
}
return 0;
}