[Sdoi2008]Cave 洞穴勘测

再次冒着巨大风险,A了一道裸得不能在裸的题,lct基本操作。
贴板:

#include<cstdio>
#include<algorithm>
#define fo(i,a,b) for (int (i)=(a);(i)<=(b);(i)++)
#define lc c[x][0]
#define rc c[x][1]
using namespace std;
const int N=10000+255;
int c[N][2],f[N],rt,r[N];
int n,m,x,y,k;
char s[20];
void R(int &x){
	int t=0; char ch;
	for (ch=getchar();!('0'<=ch&&ch<='9');ch=getchar());
	for (;('0'<=ch&&ch<='9');ch=getchar()) t=t*10+ch-'0';
	x=t;
}
bool wh(int x){
	return x==c[f[x]][1];
}
bool nroot(int x){
	return x==c[f[x]][0]||x==c[f[x]][1];
}
void rotate(int x){
	int y=f[x],z=f[y],k=wh(x),w=c[x][1^k];
	if (nroot(y)) c[z][wh(y)]=x; f[x]=z;
	c[y][k]=w; if (w) f[w]=y;
	c[x][1^k]=y; f[y]=x;
}
void pushr(int x){
	swap(lc,rc); r[x]^=1;
}
void pushdown(int x){
	if (r[x]){
		if (lc) pushr(lc);
		if (rc) pushr(rc);
		r[x]=0;
	}
}
void pd(int x){
	if (nroot(x)) pd(f[x]);
	pushdown(x);
}
void splay(int x){
	pd(x);
	for (;nroot(x);rotate(x))	
		if (nroot(f[x])) 
			rotate(wh(f[x])==wh(x)?f[x]:x);
}
void access(int x){
	for (int y=0;x;x=f[y=x])
		splay(x),rc=y;
}
void make(int x){
	access(x); splay(x); 
	pushr(x);
}
void link(int x,int y){
	make(x);
	f[x]=y;
}
void cut(int x,int y){
	make(x);
	access(y); splay(y);
	c[y][0]=f[x]=0;
}
int find(int x){
	access(x); splay(x);
	while (lc) pushdown(x),x=lc;
	splay(x);
	return x;
}
bool pd(int x,int y){
	make(x);
	return x==find(y);
}
int main(){
	//freopen("bzoj2049.in","r",stdin);
	//freopen("bzoj2049.out","w",stdout);
	R(n);R(m);
	fo(i,1,m){
		scanf("%s",s+1);R(x);R(y);
		switch(s[1]){
			case 'D':cut(x,y);break;
			case 'C':link(x,y);break;
			case 'Q':if(pd(x,y)) puts("Yes"); else puts("No");
		}
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值