【BZOJ】【P2049】【SDOI2008】【洞穴勘测】【题解】【LCT】

传送门:www.lydsy.com/JudgeOnline/problem.php?id=2049

刚说BZOJ挂了就好了= =、

又是一道模板题:

Code:

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=10010;
struct node{
	int rev;
	node *c[2],*p;
	void pushdown(){
		if(rev){
			c[0]->rev^=1;
			c[1]->rev^=1;
			swap(c[1],c[0]);
			rev^=1;
		}
	}
	void sets(node *x,int d){
		pushdown();
		(c[d]=x)->p=this;
	}
	bool d(){
		return p->c[1]==this;
	}
	bool root(){
		return p->c[0]!=this&&p->c[1]!=this;
	}
}nil[maxn];
int tot=0;
void newnode(node *x){
	x=nil+(++tot);
	x->rev=0;
	x->c[0]=x->c[1]=x->p=nil;
}
void rot(node *x){
	node *p=x->p;
	if(!p->root())p->p->pushdown();
	p->pushdown();x->pushdown();
	int d=x->d();
	p->sets(x->c[!d],d);
	if(p->root())
		x->p=p->p;
	else
		p->p->sets(x,p->d());
	x->sets(p,!d);
}
void splay(node *x){
	while(!x->root())
		if(x->p->root())
			rot(x);
		else if(x->d()==x->p->d())
			rot(x->p),rot(x);
		else 
			rot(x),rot(x);
}
node* access(node *x){
	node *y=nil;
	for(;x!=nil;y=x,x=x->p){
		splay(x);
		x->sets(y,1);
	}return y;
}
void makert(node *x){
	access(x)->rev^=1;
	splay(x);
}
node *findrt(node *x){
	for(x=access(x);x->c[0]!=nil;x=x->c[0]);return x;
}
void link(node *x,node *y){
	makert(x);
	x->p=y;
	access(x);
}
void cut(node *x,node *y){
	makert(x);
	access(y);
	splay(y);
	y->c[0]=x->p=nil;
}
int n,m;
int getint(){
	int res=0,ok=0;char ch;
	while(1){
		ch=getchar();
		if(ch<='9'&&ch>='0'){
			res*=10;res+=ch-'0';ok=1;
		}else if(ok)break;
	}return res;
}
int main(){
	n=getint();m=getint();
	node *x;
	for(int i=1;i<=n;i++)
		newnode(x);
	nil->c[0]=nil->c[1]=nil->p=nil;
	while(m--){
		char opt[55];
		scanf("%s",opt);
		node *u=nil+getint(),*v=nil+getint();
		if(opt[0]=='Q'){
			if(findrt(u)==findrt(v)){
				puts("Yes");
			}else
				puts("No");
		}else
		if(opt[0]=='C'){
			link(u,v);
		}else{
			cut(u,v);
		}
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值