题意:每次在两个点间连边或删边,询问两点是否连通。
(lbn187大神不说完全没意识到是lct啊啊啊啊啊!!!)
连边就是link,删边cut,判断联通就是看是不是在一棵辅助树上(就是两个点所在辅助树的总根是否相同。。find即可)
第一次写lct,完全不会,写一个过程看会模板,没救了。。
(不要问我up干什么吃的,其实我也不知道)
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>
#define N 10009
#define M 200009
#define ll long long
#define gc getchar()
using namespace std;
int n,m;
struct LCT
{
int son[N][2],father[N];
int rev[N];
void down(int x)
{
if (!x||!rev[x]) return;
rev[son[x][0]]^=1,rev[son[x][1]]^=1;
swap(son[x][0],son[x][1]);
rev[x]=0;
}
void pushdown(int x)
{
if (!isroot(x)) pushdown(father[x]);
down(x);
}
void up(int x)
{
return;
}
int isroot(int x)
{
return son[father[x]][0]!=x&&son[father[x]][1]!=x;
}
void rotate(int x)
{
int y=father[x],z=father[y],l=(son[y][0]!=x),r=l^1;
if (!isroot(y)) son[z][son[z][1]==y]=x;
father[x]=z;
father[y]=x;
father[son[x][r]]=y;
son[y][l]=son[x][r];
son[x][r]=y;
up(y);
up(x);
}
void splay(int x)
{
for (pushdown(x);!isroot(x);rotate(x))
{
int y=father[x],z=father[y];
if (!isroot(y))
rotate((x==son[y][0])==(y==son[z][0])?y:x);
}
}
void access(int x)
{
for (int t=0;x;t=x,x=father[x])
splay(x),son[x][1]=t,up(x);
}
int find(int x)
{
access(x);
splay(x);
while (son[x][0]) x=son[x][0];
return x;
}
void makeroot(int x)
{
access(x);
splay(x);
rev[x]^=1;
}
void link(int x,int y)
{
makeroot(x);
father[x]=y;
}
void cut(int x,int y)
{
makeroot(x);
access(y);
splay(y);
son[y][0]=father[x]=0;
up(y);
}
}T;
int read()
{
int x=0,f=1;
char c=gc;
for (;c<'0'||c>'9';c=gc)
if(c=='-') f=-1;
for (;c>='0'&&c<='9';c=gc)
x=x*10+c-'0';
return x*f;
}
int main()
{
n=read(),m=read();
for (int i=1;i<=m;i++)
{
char ch[10];
int x,y;
scanf("%s%d%d",ch,&x,&y);
if (ch[0]=='C')
T.link(x,y);
if (ch[0]=='D')
T.cut(x,y);
if (ch[0]=='Q')
printf("%s\n",T.find(x)==T.find(y)?"Yes":"No");
}
return 0;
}
抄袭miaom(miao_zc)的lct模板。。。