题目
做法
说实话如果你在看这篇题解的话应该也没有人劝你回去打模板吧
My complete code
#include<cstdio>
#include<cstring>
#include<iostream>
#include<string>
using namespace std;
typedef int LL;
const LL maxn=1e6;
inline LL Read(){
LL x(0),f(1);char c=getchar();
while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9')x=(x<<3)+(x<<1)+c-'0',c=getchar();
return x*f;
}
LL n,m;
LL son[maxn][2],fa[maxn],r[maxn];
inline void Update(LL x){return;}
inline bool Notroot(LL x){
return son[fa[x]][0]==x||son[fa[x]][1]==x;
}
inline void Pushr(LL x){
swap(son[x][0],son[x][1]);r[x]^=1;
}
inline void Pushdown(LL x){
if(r[x]){
if(son[x][0])Pushr(son[x][0]);
if(son[x][1])Pushr(son[x][1]);
r[x]=0;
}
}
inline void Rotate(LL x){
LL y(fa[x]),lz=(son[y][1]==x),z(fa[y]);
if(Notroot(y))
son[z][son[z][1]==y]=x;fa[x]=z;
son[y][lz]=son[x][lz^1];
if(son[y][lz]) fa[son[y][lz]]=y;
son[x][lz^1]=y; fa[y]=x;
Update(y),Update(x);
}
LL sta[maxn];
inline void Splay(LL x){
LL y(x),top(0);
sta[++top]=y;
while(Notroot(y)) sta[++top]=y=fa[y];
while(top) Pushdown(sta[top--]);
while(Notroot(x)){
y=fa[x];
if(Notroot(y)){
LL z(fa[y]);
if(((son[y][1]==x)^(son[z][1]==y))==0) Rotate(y);
else Rotate(x);
}
Rotate(x);
}
}
inline void Access(LL x){
for(LL y=0;x;y=x,x=fa[x]){
Splay(x); son[x][1]=y; Update(x);
}
}
inline void Makeroot(LL x){
Access(x),Splay(x),Pushr(x);
}
inline void Split(LL x,LL y){
Makeroot(x),Access(y),Splay(y);
}
inline void Link(LL x,LL y){
Makeroot(x);
fa[x]=y;
}
inline void Delet(LL x,LL y){
Split(x,y);
fa[x]=son[y][0]=0;
Update(y);
}
inline LL Findroot(LL x){
Access(x),Splay(x);
while(son[x][0])Pushdown(x),x=son[x][0];
Splay(x);
return x;
}
char s[maxn];
int main(){
n=Read(),m=Read();
while(m--){
scanf(" %s",s);
LL x(Read()),y(Read());
if(s[0]=='Q'){
Makeroot(x);
if(Findroot(y)==x)
printf("Yes
");
else
printf("No
");
}else if(s[0]=='D')
Delet(x,y);
else
Link(x,y);
}
}/*
200 5
Query 123 127
Connect 123 127
Query 123 127
Destroy 127 123
Query 123 127
*/