跳到正文

个人记录

Link Cut Tree

zhoukangyang约 9 分钟
原文
文章信息与版本

文章信息

发布
2020年7月13日
收录
2025年12月12日
更新
2026年11月2日
篇幅
约 9 分钟

版本

  1. v1当前

1. 性质

Link  Cut  Tree\tt Link \ \ Cut\ \ Tree 维护一个森林,对于每一棵树的非叶子节点都是将某一个儿子的连边划为实边(该儿子为重儿子) ,而连向其他子树的边划分为虚边(该儿子为轻儿子)

对于每一个剖出来的链,让他以深度作为权值 变成一个 Splay\tt Splay , 然后把每个轻儿子接在每一个节点。

一个节点 x\tt x 的轻儿子不表示在他的儿子数组 ch[x][0/1]\tt ch[x][0/1] 中,而他的轻儿子 y\tt y 的 fa[y]\tt fa[y] 数组则 =x\tt = x (即 fa[y]=x\tt fa[y] = x )

每个节点仅包含在一个 Splay\tt Splay 中,一个 Splay\tt Splay 中没有深度相同的节点且深度连续。

2. 操作

对于一个 Splay\tt Splay 的操作,在这里由于我太菜就不讲了

1. access(x)\tt access(x)

该操作就是把节点 x\tt x 与这棵树的根用重边连接

怎么做呢?

  1. 把当前节点转到 Splay\tt Splay 的根
  2. 把左儿子更换为上一个 Splay\tt Splay 的根 (把左子树变成用轻边连接,这时此节点为所在链的最底端,把上一个链变成用重边连接即可,就是把左子树换成上一个 Splay\tt Splay)
  3. 更新数据
  4. 跳到当前点的父亲

Code:\tt Code :

C++
void access(int now) {
    int y = 0;
    while (now) {
        Splay(now);
        ch[now][1] = y;
        upd(now);
        y = now;
        now = fa[now];
    }
}

2.makeroot(x)\tt makeroot(x)

该操作就是把节点 x\tt x 换为所在树的根

  1. 把这个节点到根变成重边(此时这个节点所在的 Splay\tt Splay 只剩下了该节点到根的节点)
  2. 把此节点转到当前 SplaySplay 的根(方便打标记)
  3. 在这个节点上打翻转标记(为了让该节点变成根节点,让链的深度翻转,就是让SplaySplay的所有节点的左右子树翻转)

Code:\tt Code:

C++
void filp(int now) { //翻转操作
    if(now!=0) swap(ch[now][0],ch[now][1]),mer_[now]^=1;
}
void pushdown(int now) { //标记下传
    if(!mer_[now]) return;
    mer_[now]^=1,filp(ch[now][0]),filp(ch[now][1]);
}
void makeroot(int now) {
    access(now),Splay(now),filp(now);
}

3.findroot(x)\tt findroot(x)

该操作就是找到当前树的根

找到树根就是找到深度最小的节点,只要从 Spaly\tt Spaly 根节点不停地往左走就行了。

Code:\tt Code:

C++
int findroot(int now) {
    access(now),Splay(now);
    while(ch[now][0]) {
		pushdown(now);
        now = ch[now][0];
    }
    Splay(now);
    return now;
}

4.split(x,y)\tt split(x,y)

就是把 x\tt x 与 y\tt y 所经过的路径提取出来成为以 y\tt y 为根的 Spaly\tt Spaly

只要把 x\tt x 作为树根,然后让 y\tt y 和 根(x\tt x) 联通,然后把 y\tt y 传到 Splay\tt Splay 的根即可。

Code:\tt Code:

C++
void split(int x,int y) {
    makeroot(x),access(y),Splay(y);
}

5.link(x,y)\tt link(x,y)

在 x\tt x 节点和 y\tt y 节点连边

判断联通,然后把 x\tt x 作为树根 , 把 y\tt y 的父节点换为 x\tt x 即可(连虚边)

Code:\tt Code:

C++
void split(int x,int y) {
    makeroot(x),access(y),Splay(y);
}

6.cut(x,y)\tt cut(x,y)

在 x\tt x 节点和 y\tt y 节点断边

判断联通,然后把 x\tt x 作为树根,然后取消连边即可

Code:\tt Code:

C++
void cut(int x,int y){//断边
	makeroot(x);
	if(findroot(y)==x&&fa[y]==x&&!ch[y][0]){
		fa[y]=ch[x][1]=0;
		upd(x);
	}
}

AC\tt AC 代码

C++
#include<bits/stdc++.h>
#define N 410000
using namespace std;
int s[N],v[N],ch[N][2],fa[N],mer_[N];
bool get(int now) {
    return ch[fa[now]][0]==now||ch[fa[now]][1]==now;
}
void filp(int now) {
    if(now!=0) swap(ch[now][0],ch[now][1]),mer_[now]^=1;
}
void pushdown(int now) {
    if(!mer_[now]) return;
    mer_[now]^=1,filp(ch[now][0]),filp(ch[now][1]);
}
void upd(int now) {
    s[now]=s[ch[now][0]]^s[ch[now][1]]^v[now];
}
void rotate(int now) {
    int fath=fa[now],ffa=fa[fath],flag=(ch[fath][1]==now),ano=ch[now][1^flag];
    if(get(fath)) ch[ffa][ch[ffa][1]==fath] = now;
    if(ano) fa[ano]=fath;
    ch[now][1^flag] = fath , ch [fath][flag] = ano , fa[fath] = now , fa[now] = ffa;
    upd(fath),upd(now);
}
int tot,lnow,stac[N];
void Splay(int now) {
    lnow = now, tot = 1, stac[1] = now;
    while(get(lnow)) ++tot,stac[tot]=lnow=fa[lnow];
    while(tot != 0) pushdown(stac[tot]),--tot; 
    while(get(now)) {
        lnow=fa[now],tot=fa[lnow];
        if(get(lnow)) rotate((ch[lnow][0]==now)^(ch[tot][0]==lnow)?now:lnow);
        rotate(now); 
    }
    upd(now);
}
void access(int now) {
    int y = 0;
    while (now) {
        Splay(now);
        ch[now][1] = y;
        upd(now);
        y = now;
        now = fa[now];
    }
}
void makeroot(int now) {
    access(now),Splay(now),filp(now);
}
int findroot(int now) {
    access(now),Splay(now);
    while(ch[now][0]) {
		pushdown(now);
        now = ch[now][0];
    }
    Splay(now);
    return now;
}
void split(int x,int y) {
    makeroot(x),access(y),Splay(y);
}
void link(int x,int y) {
    makeroot(x);
    if(findroot(y) != x) fa[x]=y;
}
void cut(int x,int y){//断边
	makeroot(x);
	if(findroot(y)==x&&fa[y]==x&&!ch[y][0]){
		fa[y]=ch[x][1]=0;
		upd(x);
	}
}
int n,m,xx,yy,opt;
int main() {
    scanf("%d%d",&n,&m);
    for(int i = 1; i <= n ; i++) scanf("%d",&v[i]);
    while(m--) {
        scanf("%d%d%d",&opt,&xx,&yy);
        if(opt==0) split(xx,yy),printf("%d\n",s[yy]);
        if(opt==1) link(xx,yy);
        if(opt==2) cut(xx,yy);
        if(opt==3) Splay(xx),v[xx]=yy,upd(xx);
    }
    return 0;
}
原文
存档评论1 条抓取于