LCT与生成树

· · 个人记录

绪言

前置知识:splay,树链剖分,最小生成树

>$LCT$说白了就是用$splay$动态维护树链剖分的东西。 ——$zyb

在树链剖分中,我们采用“轻重链剖分”的方式,即每个节点与它子树最大的儿子节点在同一条重链上(另外还有一种“长短链剖分”,本蒟蒻并不常用)。但是,在LCT中,我们使用的是“偏爱链剖分”——就是想怎么剖就怎么剖。

现在,假设我们已经有了一种划分虚实链的方法。对于每一个用实边连成的连通块,我们用一棵splay来维护。此时splay的排序关键字为节点的深度,即将splay中序遍历后,节点的深度按从小到大的顺序排序。

然后我们就可以开始操作了。

(注:本文用fa[x]代表x的父节点,son[0][x]代表左儿子,son[1][x]代表右儿子)

基本操作

如何维护虚实关系?

假如有一条实链,它的splay的根节点为x,x的父节点为f。就像这样:

这时我们令fa[x]=f,但是son[0][f]并不等于x。

换句话说,x认f这个爹,但f并不认x这个儿子。

所以我们可以利用这个特性来判断一个节点是否为根。

bool isroot(int x)
{
    return (son[0][fa[x]]!=x)&&(son[1][fa[x]]!=x);
}
```cpp void rvers(int x) { r[x]^=1; swap(son[0][x],son[1][x]); } void pushdown(int x) { if(!r[x])return ; if(son[0][x])rvers(son[0][x]); if(son[1][x])rvers(son[1][x]); r[x]=0; } void rotate(int x) { int y=fa[x],z=fa[y]; bool t=(son[1][y]==x); if(!isroot(y))son[son[1][z]==y][z]=x; fa[x]=z; fa[y]=x; son[t][y]=son[!t][x]; son[!t][x]=y; if(son[t][y])fa[son[t][y]]=y; update(y); update(x); } void pathdown(int x) { if(!isroot(x))pathdown(fa[x]); pushdown(x); } void splay(int x) { pathdown(x); for( ;!isroot(x);rotate(x)) if(!isroot(fa[x]))((son[0][fa[x]]==x)^(son[0][fa[fa[x]]]==fa[x]))?rotate(x):rotate(fa[x]); } ``` 然后是$LCT$的重点——$access$操作。 这个操作的效果是“一链通天”,即将$x$到整棵树的根的路径上的所有边都变成实边,并断开所有原来连在这些节点上的实边。 假设现在我们需要将节点$q$到根的路径全部变成实边。我们从$q$开始,每次将当前节点旋转至对应$splay$的根,然后断开该节点与右子树的实边(即实边变虚边)。这样,因为我们$splay$的排序关键字是节点的深度,我们就将这棵$splay$中所有深度更大的点全部切掉了。切掉后,再将该节点与前一棵$splay$的根连一条实边。然后跳到更靠上的一棵$splay$上继续操作,直到跳到整棵$LCT$的根为止。 具体操作看图 假设我们要将图中的$q$点$access$一遍。 ![](https://cdn.luogu.com.cn/upload/image_hosting/hrepscdo.png) 首先将$q$旋转到对应$splay$的根。 ![](https://cdn.luogu.com.cn/upload/image_hosting/n1yhoe3u.png) 此时$q$的右子树为空,所以直接跳过。 跳到上面一棵$splay$(即$fa[q]$)。此时$fa[q]$已经是对应$splay$的根了。 ![](https://cdn.luogu.com.cn/upload/image_hosting/h3tr1v5o.png) 切断它与右子树的实边。 ![](https://cdn.luogu.com.cn/upload/image_hosting/9u7edz27.png) 将它与上一棵splay的根(即$q$)连一条实边。 ![](https://cdn.luogu.com.cn/upload/image_hosting/smig6y8m.png) 再跳到上面。 ![](https://cdn.luogu.com.cn/upload/image_hosting/bepc2cym.png) 再旋转。 ![](https://cdn.luogu.com.cn/upload/image_hosting/tni5hxa3.png) 再断边、连边。 ![](https://cdn.luogu.com.cn/upload/image_hosting/catz66og.png) 完成。 代码如下: ```cpp void access(int x) { int y=0; while(x) { splay(x); son[1][x]=y; update(x); y=x; x=fa[x]; } } ``` 然后是换根操作。 我们想要将$x$节点转到根上去,由前两步可知,只需先$access(x)$,然后再$splay(x)$即可。 但是我们注意到,此时$x$是这棵splay中深度最大的点,所以我们还需要将这条链翻转,使其符合splay的性质。 ```cpp void makeroot(int x) { access(x); splay(x); rvers(x); } ``` 现在我们已经能维护一些最简单的信息了。 现在有一个问题:如何查询一条链$(x,y)$的信息? 我们需要将$x$到$y$的路径单拎出来。由前面的操作,我们先$makeroot(x)$,再$access(y)$,此时$x$和$y$就在一条实链里了。为了处理问题方便,习惯上我们将$y$作为splay的根。 ```cpp void split(int x,int y) { makeroot(x); access(y); splay(y); } ``` 讲到这里,我们提出一个应该在最初提出的问题:为什么这种数据结构叫做$LCT$? 因为它是$Link-Cut-Tree$的缩写。 顾名思义,它还能对树进行动态的加边($link$)和删边($cut$)。 加边时,我们只需先$split(x,y)$,然后令$fa[x]=y$即可。 ```cpp void link(int x,int y) { split(x,y); if(fa[x])return ; fa[x]=y; } ``` (if语句用来判断连通性,请读者自行思考) 删边时,我们也只需$splay(x,y)$,然后令$fa[x]=son[0][y]=0$即可。 ```cpp void cut(int x,int y) { split(x,y); if(son[0][y]!=x||fa[x]!=y||son[0][x]||son[1][x]||son[1][y])return ; fa[x]=son[0][y]=0; update(y); } ``` (if语句同上) 至此,$LCT$的基本操作基本完成。 ------------ ## 一道例题 传送门:[P3690](https://www.luogu.org/problem/P3690) 看题目的名称就知道这是一道模板题。 只需要再$update$函数中维护路径异或和,每次查询$split$一下即可。 剩下的操作看上面的板子即可。 贴代码 ```cpp #include<bits/stdc++.h> #define ll long long #define ull unsigned long long #define ff first #define ss second #define rep(i,a,b) for(int i=(a);i<=(b);i++) #define per(i,a,b) for(int i=(a);i>=(b);i--) using namespace std; const int INF=2147483647; inline int read() { int x=0,k=1;char c=getchar(); while(c<'0'||c>'9'){if(c=='-')k=-1;c=getchar();} while(c>='0'&&c<='9'){x=(x<<3)+(x<<1)+(c^48);c=getchar();} return x*k; } int fa[300086],son[2][300086],w[300086],s[300086]; bool r[300086]; int n,m,x,y,opt; bool isroot(int x) { return (son[0][fa[x]]!=x)&&(son[1][fa[x]]!=x); } void rvers(int x) { r[x]^=1; swap(son[0][x],son[1][x]); } void pushdown(int x) { if(!r[x])return ; if(son[0][x])rvers(son[0][x]); if(son[1][x])rvers(son[1][x]); r[x]=0; } void update(int x) { s[x]=s[son[0][x]]^s[son[1][x]]^w[x]; } void rotate(int x) { int y=fa[x],z=fa[y]; bool t=(son[1][y]==x); if(!isroot(y))son[son[1][z]==y][z]=x; fa[x]=z; fa[y]=x; son[t][y]=son[!t][x]; son[!t][x]=y; if(son[t][y])fa[son[t][y]]=y; update(y); update(x); } void pathdown(int x) { if(!isroot(x))pathdown(fa[x]); pushdown(x); } void splay(int x) { pathdown(x); for( ;!isroot(x);rotate(x)) if(!isroot(fa[x]))(son[0][fa[x]]==x)^(son[0][fa[fa[x]]]==fa[x])?rotate(x):rotate(fa[x]); } void access(int x) { int y=0; while(x) { splay(x); son[1][x]=y; update(x); y=x; x=fa[x]; } } void makeroot(int x) { access(x); splay(x); rvers(x); } void split(int x,int y) { makeroot(x); access(y); splay(y); } void link(int x,int y) { split(x,y); if(fa[x])return ; fa[x]=y; } void cut(int x,int y) { split(x,y); if(son[0][y]!=x||fa[x]!=y||son[0][x]||son[1][x]||son[1][y])return ; fa[x]=son[0][y]=0; update(y); } int main() { n=read(),m=read(); rep(i,1,n)w[i]=s[i]=read(); while(m--) { opt=read(),x=read(),y=read(); 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) { access(x); splay(x); w[x]=y; update(x); } } return 0; } ``` ------------ ## 关于复杂度 这玩意看起来体积巨大,它的复杂度是多少? 本蒟蒻不会证明,它大概是均摊$O(nlogn)$。但是常数巨大(大约有$26$左右的样子)。 与$O(nlog^2 n)$的树链剖分相比,$LCT$渐进复杂度更低,常数更大。在数据范围较小时,树链剖分更为优秀;当数据范围较大(如$5e5$左右)时,$LCT$优势更加明显。 当然,如果题目涉及动态加边、删边的话,用$LCT$要强于树链剖分。 ------------ ## LCT与生成树 $LCT$的一个经典应用就是动态维护生成树。 这里需要注意一点,生成树问题大都与边权有关,但$LCT$只能维护点权,不能维护边权,怎么办? 只需要在两个点之间塞一个点代表这条边即可(没错就是这么暴力)。 当然注意此时要$link$或$cut$两次。 例如,我们需要动态维护图的最小生成树权值,支持加边、删边,强制在线。 这时我们的$LCT$维护每条路径的最大边权。每次加入一条新边时,我们查看两个端点的连通性(可以使用并查集)。若不连通,直接$link$。若已经连通,则查询这两点路径上的边权最大的边,比较两条边的权值,保留小的那一条,另一条直接扔掉即可。 下面有几道例题。 ------------ ## 几道例题 1、[P4234](https://www.luogu.org/problem/P4234)最小极差生成树。 题目大意:给一张连通图,求极差最小的生成树的极差(最大边权与最小边权的差值)。 做法:将所有边按边权从小到大排序,一条一条地加入。$LCT$用来维护最大生成树(方法与前面类似),每次加边时求出边权最小的边的权值,然后用这个差值更新答案即可。 代码: ```cpp #include<bits/stdc++.h> #define ll long long #define ull unsigned long long #define ff first #define ss second #define rep(i,a,b) for(int i=(a);i<=(b);i++) #define per(i,a,b) for(int i=(a);i>=(b);i--) using namespace std; const int INF=2147483647; inline int read() { int x=0,k=1;char c=getchar(); while(c<'0'||c>'9'){if(c=='-')k=-1;c=getchar();} while(c>='0'&&c<='9'){x=(x<<3)+(x<<1)+(c^48);c=getchar();} return x*k; } int fa[250086],son[2][250086],mn[250086],v[250086],f[250086],n,m,x,y,k,num,cnt,now,ans; bool r[250086],vis[200086]; struct edge{ int u,v,w; bool operator<(edge x)const { return w<x.w; } }e[200086]; bool isroot(int x) { return (son[0][fa[x]]!=x)&&(son[1][fa[x]]!=x); } void rvers(int x) { r[x]^=1; swap(son[0][x],son[1][x]); } void pushdown(int x) { if(!r[x])return ; if(son[0][x])rvers(son[0][x]); if(son[1][x])rvers(son[1][x]); r[x]=0; } void update(int x) { mn[x]=x; if(son[0][x]&&v[mn[son[0][x]]]<v[mn[x]])mn[x]=mn[son[0][x]]; if(son[1][x]&&v[mn[son[1][x]]]<v[mn[x]])mn[x]=mn[son[1][x]]; } void rotate(int x) { int y=fa[x],z=fa[y]; bool t=(son[1][y]==x); if(!isroot(y))son[son[1][z]==y][z]=x; fa[x]=z; fa[y]=x; son[t][y]=son[!t][x]; son[!t][x]=y; if(son[t][y])fa[son[t][y]]=y; update(y); update(x); } void pathdown(int x) { if(!isroot(x))pathdown(fa[x]); pushdown(x); } void splay(int x) { pathdown(x); for( ;!isroot(x);rotate(x)) if(!isroot(fa[x]))(son[0][fa[x]]==x)^(son[0][fa[fa[x]]]==fa[x])?rotate(x):rotate(fa[x]); } void access(int x) { int y=0; while(x) { splay(x); son[1][x]=y; update(x); y=x; x=fa[x]; } } void makeroot(int x) { access(x); splay(x); rvers(x); } void split(int x,int y) { makeroot(x); access(y); splay(y); } void link(int x,int y) { split(x,y); fa[x]=y; } void cut(int x,int y) { split(x,y); fa[x]=son[0][y]=0; update(y); } int query(int x,int y) { split(x,y); return mn[y]; } int find(int x) { if(x!=f[x])return f[x]=find(f[x]); return f[x]; } void merge(int x,int y) { int fx=find(x),fy=find(y); if(fx==fy)return ; f[fx]=fy; } int main() { n=read(),m=read(); rep(i,1,n)f[i]=i,v[i]=2333333; ans=6666666; rep(i,1,m) { x=read(),y=read(),k=read(); if(x>y)swap(x,y); if(x==y)continue; e[++cnt].u=x,e[cnt].v=y,e[cnt].w=k; } sort(e+1,e+1+cnt); now=1; rep(i,1,cnt) { x=e[i].u,y=e[i].v,k=e[i].w; int fx=find(x),fy=find(y); if(fx!=fy) { merge(x,y); v[i+n]=k; link(x,i+n),link(i+n,y); vis[i]=true; num++; } else { int w=query(x,y); cut(e[w-n].u,w),cut(w,e[w-n].v); vis[w-n]=false; v[i+n]=k; link(x,i+n),link(i+n,y); vis[i]=true; } if(num==n-1) { while(!vis[now])now++; ans=min(ans,e[i].w-e[now].w); } } printf("%d\n",ans); return 0; } ``` 2、[P2387](https://www.luogu.org/problem/P2387)魔法森林 题目大意:一张图,每条边有$a,b$两种权值,求$1$到$n$的路径中,$\max a+\max b$的最小值。 做法:将所有边按权值$a$从小到大排序,用$LCT$维护$b$的最小生成树,每次连边时,若$1$和$n$已经连通,则更新答案即可。 代码: ```cpp #include<bits/stdc++.h> #define ll long long #define ull unsigned long long #define ff first #define ss second #define rep(i,a,b) for(int i=(a);i<=(b);i++) #define per(i,a,b) for(int i=(a);i>=(b);i--) using namespace std; const int INF=2147483647; inline int read() { int x=0,k=1;char c=getchar(); while(c<'0'||c>'9'){if(c=='-')k=-1;c=getchar();} while(c>='0'&&c<='9'){x=(x<<3)+(x<<1)+(c^48);c=getchar();} return x*k; } struct edge{ int u,v,a,b; }e[200086]; int n,m,x; int fa[150086],son[2][150086],f[150086],mx[150086],v[150086],ans; bool r[150086]; int cmp(edge c,edge d) { return c.a<d.a; } int find(int x) { if(f[x]!=x)return f[x]=find(f[x]); return f[x]; } void merge(int x,int y) { int fx=find(x),fy=find(y); if(fx!=fy)f[fx]=fy; } bool isroot(int x) { return (son[0][fa[x]]!=x)&&(son[1][fa[x]]!=x); } void rvers(int x) { r[x]^=1; swap(son[0][x],son[1][x]); } void pushdown(int x) { if(!r[x])return ; if(son[0][x])rvers(son[0][x]); if(son[1][x])rvers(son[1][x]); r[x]=0; } void update(int x) { mx[x]=x; if(son[0][x]&&v[mx[son[0][x]]]>v[mx[x]])mx[x]=mx[son[0][x]]; if(son[1][x]&&v[mx[son[1][x]]]>v[mx[x]])mx[x]=mx[son[1][x]]; } void rotate(int x) { int y=fa[x],z=fa[y]; bool t=(son[1][y]==x); if(!isroot(y))son[son[1][z]==y][z]=x; fa[x]=z; fa[y]=x; son[t][y]=son[!t][x]; son[!t][x]=y; if(son[t][y])fa[son[t][y]]=y; update(y); update(x); } void pathdown(int x) { if(!isroot(x))pathdown(fa[x]); pushdown(x); } void splay(int x) { pathdown(x); for( ;!isroot(x);rotate(x)) if(!isroot(fa[x]))(son[0][fa[x]]==x)^(son[0][fa[fa[x]]]==fa[x])?rotate(x):rotate(fa[x]); } void access(int x) { int y=0; while(x) { splay(x); son[1][x]=y; update(x); y=x; x=fa[x]; } } void makeroot(int x) { access(x); splay(x); rvers(x); } void split(int x,int y) { makeroot(x); access(y); splay(y); } void link(int x,int y) { split(x,y); fa[x]=y; } void cut(int x,int y) { split(x,y); fa[x]=son[0][y]=0; update(y); } int query(int x,int y) { split(x,y); return mx[y]; } int main() { n=read(),m=read(); ans=INF-10; rep(i,1,m) { e[i].u=read(),e[i].v=read(),e[i].a=read(),e[i].b=read(); } sort(e+1,e+1+m,cmp); rep(i,1,n+m)f[i]=i; rep(i,n+1,n+m)v[i]=e[i-n].b; rep(i,1,m) { int uu=e[i].u,vv=e[i].v; bool mark=true; if(find(uu)==find(vv)) { int w=query(uu,vv); if(v[w]>e[i].b)cut(e[w-n].u,w),cut(w,e[w-n].v); else mark=false; } else merge(uu,vv); if(mark)link(uu,i+n),link(i+n,vv); if(find(1)==find(n))ans=min(ans,e[i].a+v[query(1,n)]); } if(ans<2e9)printf("%d",ans); else printf("-1"); return 0; } ``` ------------ ## 总结 $LCT$是一种强力的数据结构,一般题目的思维难度不大,但对于代码能力的要求较高。虽然不在$NOIP$范围内,但有些题目还是能够用得上的。 ------------ ## 练习 基本操作:[P2147](https://www.luogu.org/problem/P2147),[P3203](https://www.luogu.org/problem/P3203),[P1501](https://www.luogu.org/problem/P1501) 维护生成树:[P4172](https://www.luogu.org/problem/P4172),[P4180](https://www.luogu.org/problem/P4180) 最后祝大家身体健康(雾)。