LCT-动态树

LCT-动态树

学习资料

https://www.cnblogs.com/flashhu/p/8324551.html
(具体的操作解析参见该dalao的博客,已经非常详尽)

例题

T1 洞穴勘测
题解

LCT板题,与基本操作没有差别
对于拆边,直接 c u t cut cut
对于连边,直接 l i n k link link
对于询问,直接通过 f i n d r o o t findroot findroot判断连通性

代码
#include<bits/stdc++.h>
#define M 1000009
using namespace std;
int read(){
	int f=1,re=0;char ch;
	for(ch=getchar();!isdigit(ch)&&ch!='-';ch=getchar());
	if(ch=='-'){f=-1,ch=getchar();}
	for(;isdigit(ch);ch=getchar()) re=(re<<3)+(re<<1)+ch-'0';
	return re*f;
}
int n,rev[M],c[M][2],f[M],q[M],m;
char s[20];
bool root(int x){return c[f[x]][0]!=x&&c[f[x]][1]!=x;} 
void pushdown(int x){
	if(rev[x]){
		rev[c[x][0]]^=1,rev[c[x][1]]^=1;
		swap(c[x][0],c[x][1]);
		rev[x]^=1;
	}
}
void rotate(int x){
	int fa=f[x],gfa=f[fa];
	int l=c[fa][1]==x, r=l^1;
    if(!root(fa))
        c[gfa][c[gfa][1]==fa]=x;
    f[x]=gfa,f[c[x][r]]=fa,f[fa]=x;
    c[fa][l]=c[x][r];c[x][r]=fa; 
}
void splay(int x){
	int top=0;
	q[++top]=x;
	for(int i=x;!root(i);i=f[i]) q[++top]=f[i];
	while(top) pushdown(q[top--]);
	while(!root(x)){
		int fa=f[x],gfa=f[fa];
		if(!root(fa)){
			if(c[fa][0]==x^c[gfa][0]==fa) rotate(x);
			else rotate(fa);
		}rotate(x);
	}
}
void access(int x){
	for(int y=0;x;y=x,x=f[x])
		splay(x),c[x][1]=y;
}
void makeroot(int x){access(x),splay(x),rev[x]^=1;}
void link(int x,int y){makeroot(x),f[x]=y,splay(x);}
void cut(int x,int y){makeroot(x),access(y),splay(y),c[y][0]=f[x]=0;}
int findroot(int x){
	access(x),splay(x);
	while(c[x][0]) x=c[x][0];
	return x;
}
int main(){
	n=read(),m=read();
	for(int i=1;i<=m;i++){
		scanf("%s",s);
		int x=read(),y=read();
		if(s[0]=='D') cut(x,y);
		if(s[0]=='C') link(x,y);
		if(s[0]=='Q'){
			if(findroot(x)==findroot(y)) printf("Yes\n");
			else printf("No\n"); 
		}
	}return 0;
}
T2 P3690
题解

多了 p u s h u p pushup pushup操作
同时连边的时候注意是否已经连过
拆边的时候,要判断是否存在改变
均通过 f i n d r o o t findroot findroot实现

代码
#include<bits/stdc++.h>//P3690 
#define M 1000009
using namespace std;
int read(){
	int f=1,re=0;char ch;
	for(ch=getchar();!isdigit(ch)&&ch!='-';ch=getchar());
	if(ch=='-'){f=-1,ch=getchar();}
	for(;isdigit(ch);ch=getchar()) re=(re<<3)+(re<<1)+ch-'0';
	return re*f;
}
int n,rev[M],c[M][2],f[M],q[M],m,v[M],sum[M];
char s[20];
bool root(int x){return c[f[x]][0]!=x&&c[f[x]][1]!=x;} 
void pushup(int x){sum[x]=sum[c[x][1]]^sum[c[x][0]]^v[x];}
void pushdown(int x){
	if(rev[x]){
		rev[c[x][0]]^=1,rev[c[x][1]]^=1;
		swap(c[x][0],c[x][1]);
		rev[x]^=1;
	}
}
void rotate(int x){
	int fa=f[x],gfa=f[fa];
	int l=c[fa][1]==x, r=l^1;
    if(!root(fa)) c[gfa][c[gfa][1]==fa]=x;
    f[x]=gfa,f[c[x][r]]=fa,f[fa]=x;
    c[fa][l]=c[x][r];c[x][r]=fa; 
    pushup(fa);
}
void splay(int x){
	int top=0;
	q[++top]=x;
	for(int i=x;!root(i);i=f[i]) q[++top]=f[i];
	while(top) pushdown(q[top--]);
	while(!root(x)){
		int fa=f[x],gfa=f[fa];
		if(!root(fa)){
			if(c[fa][0]==x^c[gfa][0]==fa) rotate(x);
			else rotate(fa);
		}rotate(x);
	}pushup(x);
}
void access(int x){
	for(int y=0;x;y=x,x=f[x])
		splay(x),c[x][1]=y,pushup(x);
}
void makeroot(int x){access(x),splay(x),rev[x]^=1;}
void split(int x,int y){makeroot(x),access(y),splay(y);}
int findroot(int x){
	access(x),splay(x);
	while(c[x][0]) x=c[x][0];
	splay(x);return x;//此处splay又把根节点还原回去了
}
void link(int x,int y){
	makeroot(x);
	if(findroot(y)!=x) f[x]=y;
}
void cut(int x,int y){
	makeroot(x);
	if(findroot(y)==x&&!c[y][0]&&f[y]==x) c[x][1]=f[y]=0;
	pushup(x);
}
int main(){
	n=read(),m=read();
	for(int i=1;i<=n;i++) v[i]=read();
	for(int i=1;i<=m;i++){
		int opt=read(),x=read(),y=read();
		if(opt==0) split(x,y),printf("%d\n",sum[y]);
		if(opt==1) link(x,y);
		if(opt==2) cut(x,y);
		if(opt==3) splay(x),v[x]=y;//一定要先splay,再修改值
	}return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值