Link-Cut Tree(在补了)

· · 算法·理论

0. 前言

Link-Cut Tree 是一种支持

的高级数据结构

在此之前,你应学会树链剖分和Splay维护区间的原理。

Splay维护区间的原理是旋转操作不会改变二叉树的中序遍历。相当于用平衡树维护了区间中的下标。

树剖在 这里

回顾一下树剖的原理:

将一棵树上的特定边作为实边(例:重边),重边会连成若干条链,用维护区间的数据结构来维护。

然而 Link-Cut Tree 的特色就在于 Link-Cut ,这导致的链剖分方式会改变,所以要用更灵活的数据结构来维护这些区间。

比如说Splay就很好

Link-Cut Tree 就是一种用Splay辅助维护实链的数据结构。

1. 基本原理

树链剖分的一些基本结论:

由于线段树的形态不如 Splay 灵活,所以我们用同样能维护区间信息的 Splay 来维护每一条树链上的区间信息,其中序遍历是实链上由浅到深的顺序。

在重链剖分里,我们以 topfa 的不同作为区分两条重链的方式,并以 fa[topfa[x]] 的方式存储了重链间的父子关系。在引入较为独立的 Splay 之后,事实上树链链头即是 Splay 的最左边的结点。

注意到链头并不具有性质上的特殊性,因此我们可以直接用辅助树的父子关系代表树链的父子关系。又因为子树内的旋转并不影响子树间的父子关系,因此我们创造性地相出一种表示方法

LCT通过以下一些基本操作来实现的主要功能

2. 基本操作

以下对 Splay 的基本操作只解释一些细节。

Rotate(x)

Splay 基本操作,将 x 向上旋。

这里有一个小问题,就是遇到虚边就相当于遇到了不存在的边,不能给虚边记录儿子。

void Rotate(int x){
    if(isroot(x)) return;
    int y=fa[x],fy=fa[y],p=getp(x),z=son[x][p^1];
    if(!isroot(y)) son[fy][getp(y)]=x;//先判虚边 
    fa[x]=fy;
    son[y][p]=z;
    if(z) fa[z]=y;
    son[x][p^1]=y;
    fa[y]=x;
    Pushup(y);
    return;
}

Pushdown(x)

Link-Cut Tree会用到区间反转功能,所以需要下传标记。

void Pushdown(int x){
    if(tag[x]){
        tag[x]=0;
        if(son[x][0]) tag[son[x][0]]^=1;
        if(son[x][1]) tag[son[x][1]]^=1;
        swap(son[x][0],son[x][1]);
    }
    return;
}

Splay(x)

Splay基本操作,将x旋到辅助树中的根。

首先要将从辅助树的根到x上的所有点Pushdown

int Splay(int x){
    int r=x;stktop=0;
    while(!isroot(r)){
        stk[++stktop]=r;
        r=fa[r];
    }
    Pushdown(r);//记得pushdown(r) 
    while(stktop) Pushdown(stk[stktop--]);
    while(!isroot(x)){
        if(!isroot(fa[x]) && getp(fa[x])==getp(x)) Rotate(fa[x]);
        Rotate(x);
    }
    Pushup(x);
    return x;
}

Access(x)

Access(x) 是指将从 x 到原树根结点间的所有结点归入一条实链,即一棵辅助树内。这是LCT最关键的操作。这会暂时破坏树链与叶子节点对应的性质

将 x 到根节点间的路径修改成一条实链。x 在该实链的底端,在原树上需要将 x 到它的所有儿子之间的边置为虚边。

首先将 x 旋转到当前辅助树的根节点。由于辅助树的中序遍历就是重链的由浅到深的顺序,所以此时断开 x 在辅助树上的右子树是 x 在原树中的子树里的一条树链。

设从起始结点y向上处理,当前结点为x

在到x~y的路径上,x 的儿子树链所在辅助树的根为 r

主要步骤如下。

int Access(int x){
    int p=0;
    while(x){
        Splay(x);
        son[x][1]=p;
        Pushup(x);
        p=x;
        x=fa[x];
    }
    return p;
}

3.2. Makeroot(x)

将点x换为原树的根。

进行Access(x)之后,x在它所在重链的底端。直接将辅助树翻转不就让x在顶端了?

妙啊~妙啊~

记得要打标记,而不是直接交换左右儿子。

void Makeroot(int x){
    Access(x);
    Splay(x);
    tag[x]^=1;
    return;
}

3.3. Findroot(x)

找到x所在原树的根

Access(x)之后,利用搜索树的性质可以找到根。

int Findroot(int x){
    Access(x);
    Splay(x);
    Pushdown(x);
    while(son[x][0]){
        x=son[x][0];
        Pushdown(x);
    }
    Splay(x);
    return x;
}

3.4. Split(x,y)

提取路径。

将x到y的路径放到一棵辅助树中

int Split(int x,int y){
    Makeroot(x);
    Access(y);
    Splay(y);
    return y;
}

4.操作

4.1. Link(x,y)

void Link(int x,int y){
    Makeroot(x);
    int r=Findroot(y);
    if(r!=x) fa[x]=y;
    return;
}

4.2. Cut(x,y)

令x成为根

在Findroot(y)过程中包括了access(y)

这时可以顺便判x和y间是否有边。

void Cut(int x,int y){
    Makeroot(x);
    int r=Findroot(y);
    if(r==x && fa[y]==x && !son[y][0]){
        fa[y]=son[x][1]=0;
        Pushup(x);
    }
    return;
}

【模板】

背模板背模板背模板!!!!!

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

const int MAXN=100005;
int N,M,A[MAXN];
int tag[MAXN],d[MAXN];
int val[MAXN],siz[MAXN],fa[MAXN],son[MAXN][2];

bool isroot(int x) {return (x!=son[fa[x]][0] && x!=son[fa[x]][1]);}
bool getp(int x) {return (x==son[fa[x]][1]);}

void Pushup(int x){
    siz[x]=siz[son[x][0]]+siz[son[x][1]]+1;
    d[x]=d[son[x][0]]^d[son[x][1]]^A[x];
    return;
}

void Pushdown(int x){
    if(tag[x]){
        swap(son[x][0],son[x][1]);
        if(son[x][0]) tag[son[x][0]]^=1;
        if(son[x][1]) tag[son[x][1]]^=1;
        tag[x]=0;
    }
    return;
}

void Rotate(int x){
    if(isroot(x)) return;
    int y=fa[x],fy=fa[y],p=getp(x),z=son[x][p^1];
    if(!isroot(y)) son[fy][getp(y)]=x;//先判虚边 
    fa[x]=fy;
    son[y][p]=z;
    if(z) fa[z]=y;
    son[x][p^1]=y;
    fa[y]=x;
    Pushup(y);
    return;
}

int stk[MAXN],stktop;
int Splay(int x){
    int r=x;stktop=0;
    while(!isroot(r)){//不要缩写
        stk[++stktop]=r;
        r=fa[r];
    }
    Pushdown(r);//记得pushdown(r) 
    while(stktop) Pushdown(stk[stktop--]);
    while(!isroot(x)){
        if(!isroot(fa[x]) && getp(fa[x])==getp(x)) Rotate(fa[x]);
        Rotate(x);
    }
    Pushup(x);
    return x;
}

int Access(int x){
    int p=0;
    while(x){
        Splay(x);
        son[x][1]=p;
        Pushup(x);
        p=x;
        x=fa[x];
    }
    return p;
}

void Makeroot(int x){
    Access(x);
    Splay(x);
    tag[x]^=1;
    return;
}

int Findroot(int x){
    Access(x);
    Splay(x);
    Pushdown(x);
    while(son[x][0]){
        x=son[x][0];
        Pushdown(x);
    }
    Splay(x);
    return x;
}

int Split(int x,int y){
    Makeroot(x);
    Access(y);
    Splay(y);
    return y;
}

void Link(int x,int y){
    Makeroot(x);
    int r=Findroot(y);
    if(r!=x) fa[x]=y;
    return;
}

void Cut(int x,int y){
    Makeroot(x);
    int r=Findroot(y);
    if(r==x && fa[y]==x && !son[y][0]){
        fa[y]=son[x][1]=0;
        Pushup(x);
    }
    return;
}

void Input(){
    scanf("%d%d",&N,&M);
    for(int i=1;i<=N;i++)
        scanf("%d",&A[i]);
    return;
}

int main(){
    Input();
    while(M--){
        int t,x,y;
        scanf("%d%d%d",&t,&x,&y);
        if(t==0){
            int root=Split(x,y);
            printf("%d\n",d[root]);
        }
        if(t==1) Link(x,y);
        if(t==2) Cut(x,y);
        if(t==3){
            Splay(x);
            A[x]=y;
        }
    }
    return 0;
}