[LCT]洞穴勘测

哪里都有的题
LCT模板

Code:

#include<bits/stdc++.h>
using namespace std;
inline int read(){
	int res=0,f=1;char ch=getchar();
	while(!isdigit(ch)) {if(ch=='-') f=-f;ch=getchar();}
	while(isdigit(ch)) {res=(res<<1)+(res<<3)+(ch^48);ch=getchar();}
	return res*f;
}
const int N=100005;
char op[10];
bool rev[N];
int fa[N],son[N][2],sta[N],n,m;
inline bool isroot(int x) {return son[fa[x]][0]!=x && son[fa[x]][1]!=x;}
inline void pushdown(int x){
	int l=son[x][0],r=son[x][1];
	if(rev[x]){
		rev[x]^=1,rev[l]^=1;rev[r]^=1;
		swap(son[x][0],son[x][1]);
	}
}
inline void rotate(int x){
	int y=fa[x],z=fa[y];
	int l=(son[y][0]!=x),r=l^1;
	if(!isroot(y)){
		if(son[z][0]==y) son[z][0]=x;
		else son[z][1]=x;
	}
	fa[x]=z,fa[y]=x;fa[son[x][r]]=y;
	son[y][l]=son[x][r],son[x][r]=y;
}
inline void splay(int x){
	int top=0;sta[++top]=x;
	for(int i=x;!isroot(i);i=fa[i]) sta[++top]=fa[i];
	for(int i=top;i;i--) pushdown(sta[i]);
	while(!isroot(x)){
		int y=fa[x],z=fa[y];
		if(!isroot(y)){
			if(son[y][0]==x^son[z][0]==y) rotate(x);
			else rotate(y);
		}
		rotate(x);
	}
}
inline void access(int x) {for(int t=0;x;x=fa[x]) splay(x),son[x][1]=t,t=x;}
inline void make_root(int x){access(x);splay(x);rev[x]^=1;}
inline void link(int x,int y){make_root(x);fa[x]=y;splay(x);}
inline void cut(int x,int y) {make_root(x);access(y);splay(y);son[y][0]=fa[x]=0;}
inline int find(int x){
	access(x);splay(x);
	int y=x;
	while(son[y][0]) y=son[y][0];
	return y;
}

int main(){
	scanf("%d%d",&n,&m);  
	while(m--){  
		scanf("%s",op);  
		int x,y;scanf("%d%d",&x,&y);  
		if(op[0]=='C') link(x,y);  
		if(op[0]=='D') cut(x,y);
		if(op[0]=='Q') puts(find(x)==find(y)?"Yes":"No");  
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值