[SDOI2008]洞穴勘测

II.[SDOI2008]洞穴勘测

也是近似的模板题,甚至比模板还要简单,连维护\(pushup\)都不需要。

主要是为了多敲几遍熟悉代码。

代码:

#include<bits/stdc++.h>
using namespace std;
#define lson t[x].ch[0]
#define rson t[x].ch[1]
int n,m;
struct LCT{
	int ch[2],fa,rev;
}t[100100];
int identify(int x){
	if(t[t[x].fa].ch[0]==x)return 0;
	if(t[t[x].fa].ch[1]==x)return 1;
	return -1;
}
void REV(int x){
	swap(lson,rson),t[x].rev^=1;
}
void pushdown(int x){
	if(!t[x].rev)return;
	if(lson)REV(lson);
	if(rson)REV(rson);
	t[x].rev=0;
}
void rotate(int x){
	int y=t[x].fa;
	int z=t[y].fa;
	int dirx=identify(x);
	int diry=identify(y);
	int b=t[x].ch[!dirx];
	if(diry!=-1)t[z].ch[diry]=x;t[x].fa=z;
	if(b)t[b].fa=y;t[y].ch[dirx]=b;
	t[x].ch[!dirx]=y,t[y].fa=x;
}
void pushall(int x){
	if(identify(x)!=-1)pushall(t[x].fa);
	pushdown(x);
}
void splay(int x){
	pushall(x);
	while(identify(x)!=-1){
		int fa=t[x].fa;
		if(identify(fa)==-1)rotate(x);
		else if(identify(x)==identify(fa))rotate(fa),rotate(x);
		else rotate(x),rotate(x);
	}
}
void access(int x){
	for(int y=0;x;x=t[y=x].fa)splay(x),rson=y;
}
void makeroot(int x){
	access(x),splay(x),REV(x);
}
int findroot(int x){
	access(x),splay(x);
	while(lson)pushdown(x),x=lson;
	splay(x);
	return x;
}
void split(int x,int y){
	makeroot(x),access(y),splay(y);
}
void link(int x,int y){
	makeroot(x);
	t[x].fa=y;
}
void cut(int x,int y){
	split(x,y);
	t[x].fa=t[y].ch[0]=0;
}
bool connected(int x,int y){
	return findroot(x)==findroot(y);
}
int main(){
	scanf("%d%d",&n,&m);
	for(int i=1,x,y;i<=m;i++){
		char s[10];
		scanf("%s%d%d",s,&x,&y);
		if(s[0]=='Q')puts(connected(x,y)?"Yes":"No");
		if(s[0]=='C')link(x,y);
		if(s[0]=='D')cut(x,y);
	}
	return 0;
}

原文地址:https://www.cnblogs.com/Troverld/p/14601864.html