[学习笔记] Link-Cut-Tree

终于学了 link-cut-tree \text{link-cut-tree} link-cut-tree ,我是看FlashHu 的博客学的。

概念简述

lct \text{lct} lct 的思想主要是把连向某一个儿子的边划分为实边,其它的边划分成虚边。
区别在于虚实是可以动态变化的,因此要使用更高级、更灵活的Splay来维护每一条由若干实边连接而成的实链。

lct \text{lct} lct的主要性质如下:

  • 每一个 splay \text{splay} splay维护的是一条从上到下按在原树中深度严格递增的路径,且中序遍历 splay \text{splay} splay得到的每个点的深度序列严格递增。
  • 每个节点包含且仅包含于一个 splay \text{splay} splay中。
  • 边分为实边和虚边,实边包含在 splay \text{splay} splay中,而虚边总是由一棵 splay \text{splay} splay指向另一个节点。因为性质2,当某点在原树中有多个儿子时,只能向其中一个儿子拉一条实链,而其它儿子是不能在这个 splay \text{splay} splay中的。那么为了保持树的形状,我们要让到其它儿子的边变为虚边,由对应儿子所属的 splay \text{splay} splay的根节点的父亲指向该点,而从该点并不能直接访问该儿子。

具体操作

结合这一道模板题:【模板】Link Cut Tree,讲一下具体操作

access(x)

lct \text{lct} lct的核心操作,定义是把 x x x到整棵树的根的路径全部改成实边,且 x x x要成为这条路径上最深的点。

说白的实现步骤就四个,原谅蒟蒻搞不出图来

  • 把当前所指向的父亲转到根
  • 修改儿子
  • 更新信息
  • 切换当前点所指的父亲
void access(int x)
{
	for(int y=0;x;x=par[y=x])
		splay(x),ch[x][1]=y,push_up(x); 
}
makeroot(x)

该操作的定义是把 x x x换成大 splay \text{splay} splay的根,我们把 x x x到根的路径打通( a c c e s s access access),然后把 splay(x) \text{splay(x)} splay(x)到根,在 x x x上面打上一个区间翻转标记( f l i p flip flip),就把深度最大的变成深度最小的,已达到换根的效果。

void flip(int x)
{
	if(!x) return ;
	swap(ch[x][0],ch[x][1]);
	fl[x]^=1;
}
void makeroot(int x)
{
	access(x);splay(x);
	flip(x);
}
findroot(x)

找到 x x x在原树上的根,主要用来判断两点之间的连通性,先把 x x x到根的路径打通,然后把 splay(x) \text{splay(x)} splay(x)到根,找到 x x x最左边的儿子,就是根,注意要把根 splay \text{splay} splay回去,不要改变树的形态,代码如下:

int findroot(int x)
{
	access(x);splay(x);
	while(ch[x][0]) push_down(x),x=ch[x][0];
	splay(x);
	return x;
}
split(x,y)

这个操作就是把 ( x , y ) (x,y) (x,y)的路径拆出来,我们先把 x x x换成根,然后在把 y y y x x x的路径打通,把 y y y转到根,那么 y y y上面存的信息就是 ( x , y ) (x,y) (x,y)这一条路径上的,代码如下:

void split(int x,int y)
{
	makeroot(x);
	access(y);splay(y);
}
link(x,y)

连一条 ( x , y ) (x,y) (x,y)的边,要判断 x , y x,y x,y的连通性,我们先把 x x x给换成根,然后找 y y y r o o t root root即可。

void link(int x,int y)
{
	makeroot(x);
	if(findroot(y)!=x) par[x]=y;
}
cut(x,y)

断一条 ( x , y ) (x,y) (x,y)的边,要判断 x , y x,y x,y的连通性,用 link \text{link} link类似的方式,这时候要判断 ( x , y ) (x,y) (x,y)直接相连,先判断 y y y的父亲是不是 x x x,在判断 y y y是否有左儿子(因为 y y y x x x的右儿子, y y y如果有左儿子说明不直接相连)。

void cut(int x,int y)
{
	makeroot(x);
	if(findroot(y)==x && par[y]==x && !ch[y][0])
	{
		par[y]=ch[x][1]=0;
		push_up(x); 
	}
}

所有的操作至此就讲完了,下面附上完整代码。

#include <cstdio>
#include <iostream>
using namespace std;
const int M = 100005;
int read()
{
	int x=0,flag=1;char c;
	while((c=getchar())<'0' || c>'9') if(c=='-') flag=-1;
	while(c>='0' && c<='9') x=(x<<3)+(x<<1)+(c^48),c=getchar();
	return x*flag;
}
int n,m,par[M],ch[M][2],val[M],sum[M],fl[M],st[M];
int nrt(int x)
{
	return ch[par[x]][0]==x || ch[par[x]][1]==x;
}
int chk(int x)
{
	return ch[par[x]][1]==x;
}
void push_up(int x)
{
	if(!x) return ;
	sum[x]=sum[ch[x][0]]^sum[ch[x][1]]^val[x];
}
void flip(int x)
{
	if(!x) return ;
	swap(ch[x][0],ch[x][1]);
	fl[x]^=1;
}
void push_down(int x)
{
	if(!x) return ;
	if(fl[x])
	{
		flip(ch[x][0]);flip(ch[x][1]);
		fl[x]=0;
	}
}
void rotate(int x)
{
	int y=par[x],z=par[y],k=chk(x),w=ch[x][k^1];
	ch[y][k]=w;par[w]=y;
	if(nrt(y)) ch[z][chk(y)]=x;par[x]=z;
	ch[x][k^1]=y;par[y]=x;
	push_up(y);push_up(x);
}
void splay(int x)
{
	int y=x,z=0;
	st[++z]=y;
	while(nrt(y)) st[++z]=y=par[y];
	while(z) push_down(st[z--]);
	while(nrt(x))
	{
		int y=par[x],z=par[y];
		if(nrt(y))
		{
			if(chk(y)==chk(x)) rotate(y);
			else rotate(x);
		}
		rotate(x);
	}
}
void access(int x)
{
	for(int y=0;x;x=par[y=x])
		splay(x),ch[x][1]=y,push_up(x); 
}
void makeroot(int x)
{
	access(x);splay(x);
	flip(x);
}
int findroot(int x)
{
	access(x);splay(x);
	while(ch[x][0]) push_down(x),x=ch[x][0];
	splay(x);
	return x;
}
void split(int x,int y)
{
	makeroot(x);
	access(y);splay(y);
}
void link(int x,int y)
{
	makeroot(x);
	if(findroot(y)!=x) par[x]=y;
}
void cut(int x,int y)
{
	makeroot(x);
	if(findroot(y)==x && par[y]==x && !ch[y][0])
	{
		par[y]=ch[x][1]=0;
		push_up(x); 
	}
}
int main()
{
	n=read();m=read();
	for(int i=1;i<=n;i++) val[i]=read();
	while(m--)
	{
		int op=read(),x=read(),y=read();
		if(op==0)
		{
			split(x,y);
			printf("%d\n",sum[y]);
		}
		if(op==1)
			link(x,y);
		if(op==2)
			cut(x,y);
		if(op==3)
			splay(x),val[x]=y;
	}
}

例题

  • 例1:OTOCI,(版题,给luogu传送门)
  • 例2:弹飞绵羊(有点意思,给题解)
  • 例3:洞穴探测(版题,给luogu传送门)
  • 例4:tree(打标记好题)
  • 例5:城市旅行(需要推式子的标记题)
  • 例6:水管局长 l c t lct lct维护最小生成树)

日后会慢慢补充。
T o      b e      c o n t i n u e d . . . . . . . To\space\space\space\space be\space\space\space\space continued....... To    be    continued.......

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值