link-cut tree
Link-Cut Tree
P3690 【模板】动态树(LCT)
LCT 的大致思想:
将森林实链剖分,对每条链建一棵splay,splay中根遍历的节点深度依次递增,每个点维护其所在splay的子树的信息(无需维护链外信息),splay间认父不认子,splay的根的父亲为原树中链顶的父亲.
下面是一个可能的LCT
access(x)(核心函数)将x 到根的链提取出来.
下面为
其大致流程就是不断往上跳,若该节点与父亲不在同一条实链上,则断开原有的连接,并将该节点与父亲连接.
那我们如何快速断开与连接呢?其实,将
void access(int x)
{
for(int y=0;x;y=x,x=f[x])
splay(x),c[x][1]=y,up(x);//别忘记更新x节点的信息
}
makeroot(x)将x 设为树根
先 access(x),然后 splay(x),然后再翻转
void makeroot(int x)
{
access(x);splay(x);tag(x);
}
split(x,y)将x 到y 的路径提取出来
先将 splay(y)(这样可以方便查询).
void split(int x,int y)
{
makeroot(x);access(y);splay(y);
}
findroot(x)找到x 所在树的根
先 access(x),splay(x),然后不断地往左子树找即可. 最后要 splay(x) 以保证时间复杂度.
int findroot(int x)
{
access(x);splay(x);
while(c[x][0]) down(x),x=c[x][0];//记得翻转子树并下传标记
splay(x);
return x;
}
link(x,y)连接x,y
先 makeroot(x),若
void link(int x,int y)
{
makeroot(x);
if(findroot(y)!=x) f[x]=y;//注意findroot(y)之后y为所在splay的根
}
cut(x,y)切断边x,y
先 split(x,y),此时
void cut(int x,int y)
{
split(x,y);
if(c[y][0]==x&&!c[x][1]) f[x]=c[y][0]=0,up(y);
}
完整代码
#include<bits/stdc++.h>
using namespace std;
const int N=100007;
int n,m,r[N],f[N],c[N][2],v[N],s[N],opt,x,y;
void tag(int x){r[x]^=1;swap(c[x][0],c[x][1]);}
void down(int x){if(r[x])r[x]=0,tag(c[x][0]),tag(c[x][1]);}
void up(int x){s[x]=v[x]^s[c[x][0]]^s[c[x][1]];}
int nroot(int x){return c[f[x]][0]==x||c[f[x]][1]==x;}
void rotate(int x)
{
int y=f[x],z=f[y],k=c[y][1]==x,w=c[x][!k];
c[x][!k]=y;c[y][k]=w;if(nroot(y)) c[z][c[z][1]==y]=x;
if(w) f[w]=y;f[y]=x;f[x]=z;
up(y);
}
void pushall(int x)
{
if(nroot(x)) pushall(f[x]);
down(x);
}
void splay(int x)
{
pushall(x);//splay前记得推平懒标记
for(int y=f[x];nroot(x);rotate(x),y=f[x])
if(nroot(y)) rotate((c[y][1]==x)^(c[f[y]][1]==y)?x:y);
up(x);
}
void access(int x)
{
for(int y=0;x;y=x,x=f[x])
splay(x),c[x][1]=y,up(x);
}
int findroot(int x)
{
access(x);splay(x);
while(c[x][0]) down(x),x=c[x][0];
splay(x);
return x;
}
void makeroot(int x)
{
access(x);splay(x);tag(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) f[x]=y;
}
void cut(int x,int y)
{
split(x,y);
if(c[y][0]==x&&!c[x][1]) f[x]=c[y][0]=0,up(y);
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;++i) scanf("%d",&v[i]),s[i]=v[i];
while(m--)
{
scanf("%d%d%d",&opt,&x,&y);
if(opt==0) split(x,y),printf("%d\n",s[y]);
if(opt==1) link(x,y);
if(opt==2) cut(x,y);
if(opt==3) splay(x),v[x]=y,up(x);
}
}
LCT 相关技巧
- 维护树链信息
直接 split 一下就可以了. 比树剖少一个
P5354 由乃的 OJ
按位处理. 直接树剖
- 维护子树信息
P4219 [BJOI2014]大融合
我们再多维护虚子树信息即可.
我们只需在切换边虚实状态的时候修改虚子树信息. 如 access,link.
#include<cstdio>
const int N=100007;
int n,m,x,y;char op[3];
int f[N],c[N][2],r[N],s[N],v[N],si[N];
void tag(int x){r[x]^=1;int t=c[x][0];c[x][0]=c[x][1];c[x][1]=t;}
void down(int x){if(r[x])r[x]=0,tag(c[x][0]),tag(c[x][1]);}
void up(int x){s[x]=s[c[x][0]]+s[c[x][1]]+1+si[x];}
int nroot(int x){return c[f[x]][0]==x||c[f[x]][1]==x;}
void rotate(int x)
{
int y=f[x],z=f[y],k=c[y][1]==x,w=c[x][!k];
if(nroot(y)) c[z][c[z][1]==y]=x;c[x][!k]=y;c[y][k]=w;
if(w) f[w]=y;f[y]=x;f[x]=z;
up(y);
}
void pushall(int x)
{
if(nroot(x)) pushall(f[x]);
down(x);
}
void splay(int x)
{
pushall(x);
for(int y=f[x];nroot(x);rotate(x),y=f[x])
if(nroot(y)) rotate((c[y][1]==x)^(c[f[y]][1]==y)?x:y);
up(x);
}
void access(int x)
{
for(int y=0;x;y=x,x=f[y])
splay(x),si[x]+=s[c[x][1]],si[x]-=s[c[x][1]=y],up(x);
}
void makeroot(int x)
{
access(x);splay(x);tag(x);
}
int findroot(int x)
{
access(x);splay(x);
while(c[x][0]) down(x),x=c[x][0];
splay(x);
return x;
}
void split(int x,int y)
{
makeroot(x);access(y);splay(y);
}
void link(int x,int y)
{
split(x,y);//这里不是指提取路径,而是偷懒
f[x]=y;si[y]+=s[x];up(y);
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;++i) s[i]=1;
while(m--)
{
scanf("%s%d%d",op,&x,&y);
if(op[0]=='A') link(x,y);
else split(x,y),printf("%d\n",(si[x]+1)*(si[y]+1));
}
}
P4299 首都
注意到新重心一定在原两个重心的路径上,直接splay上二分即可
- 维护生成树
P4172 [WC2006]水管局长
若不连通则连上,否则,若原来路径的最大值大于新加入的边权,则将原来路径上边权最大的边替换掉.
P2387 [NOI2014] 魔法森林
按
P4234 最小差值生成树
从小到大加边,维护最大生成树.
P5385 [Cnoi2019] 须臾幻境
首先有个结论:连通块数 = 点数 - 生成树边数.
令边的权值为其编号,则区间
从左到右扫描右端点,用 LCT 维护最大生成树,同时用主席树维护前缀信息. 时间复杂度
- 维护双连通分量
P2542 [AHOI2005] 航线规划
遇到环就缩,查询的时候查询路径长度即可.
动态图连通性问题
- 「离线可过」动态图连通性
方法一:线段树分治+可撤销并查集
方法二:将每条边的删除时间作为权值,加入过程中维护最大生成树,若两点路径的最小权值大于当前时间,则连通.
- 「强制在线」动态图连通性
给每条边赋予一个等级,设
设
我们再规定,
加入:将该边的等级设为
查询:直接在
删除:
设该边的等级为
若该边在
从
设该边两边的点集分别为
不妨假设枚举的边的其中一端在
若该边两端均在
否则找到答案,在
用
P11408 [RMI 2020] 树咖 / Arboras
维护长链剖分. 一次修改后,
证:重剖,设势能为有多少长边为轻边,每次修改势能至多增加
复杂度
P10359 [PA 2024] Kolorowy las
时光倒流,操作 4 变为插入点,操作 3 变为取出距离
现在问题变为快速查询距离
使用 LCT,对每个点维护其子树中距离
复杂度