【BZOJ2049】[Sdoi2008]Cave洞穴勘测

2 篇文章 0 订阅

题意:每次在两个点间连边或删边,询问两点是否连通。

(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模板。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值