这才是真的。。LCT入门题把。。
Connect就是link操作,Destroy就是cut操作,Query就是查询是否在同一个树中。
【代码】
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stack>
#define N 10005
#define M 2001005
#define INF 1e9
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pa;
int read()
{
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-') f=-1;ch=getchar();}
while(isdigit(ch)){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return x*f;
}
int n,m,top,cnt;
int son[N][2],st[N],fa[N];
bool rev[N];
void Input_Init(){
n=read();m=read();
}
bool isroot(int x){
return son[fa[x]][0]!=x&&son[fa[x]][1]!=x;
}
void pushdown(int k)
{
if(!rev[k]) return;
int l=son[k][0],r=son[k][1];
rev[k]=0;rev[l]^=1;rev[r]^=1;
swap(son[k][0],son[k][1]);
}
void Rotate(int x)
{
int y=fa[x],z=fa[y],l,r;
l=son[y][1]==x;r=l^1;
if(!isroot(y)) son[z][son[z][1]==y]=x;
fa[x]=z;fa[y]=x;fa[son[x][r]]=y;
son[y][l]=son[x][r];son[x][r]=y;
}
void Splay(int x)
{
top=0;st[++top]=x;
for(int i=x;!isroot(i);i=fa[i]) st[++top]=fa[i];
while(top) pushdown(st[top--]);
while(!isroot(x))
{
int y=fa[x],z=fa[y];
if(!isroot(y)) {
if(son[z][0]==y^son[y][0]==x) Rotate(x);
else Rotate(y);
}
Rotate(x);
}
}
void Access(int x)
{
for(int t=0;x;t=x,x=fa[x])
Splay(x),son[x][1]=t;
}
void Make_Root(int x){
Access(x);Splay(x);rev[x]^=1;
}
int Find(int x){
Access(x);Splay(x);
while(son[x][0]) x=son[x][0];
return x;
}
void Solve_Query(int x,int y){
if(Find(x)!=Find(y)) printf("No\n");
else printf("Yes\n");
}
void Solve_Link(int x,int y){
Make_Root(x);fa[x]=y;
}
void Solve_Cut(int x,int y){
Make_Root(x);Access(y),Splay(y);
son[y][0]=fa[x]=0;
}
void Solve()
{
while(m--)
{
char ch[10];static int x,y;
scanf("%s",ch);x=read(),y=read();
if(ch[0]=='Q') Solve_Query(x,y);
else if(ch[0]=='C') Solve_Link(x,y);
else Solve_Cut(x,y);
}
}
int main()
{
Input_Init();
Solve();
return 0;
}