Link Cut Tree

· · 个人记录

\tt Link \ \ Cut\ \ Tree 详解

1. 性质

对于每一个剖出来的链,让他以**深度**作为权值 变成一个 $\tt Splay$ , 然后把每个轻儿子接在每一个节点。 一个节点 $\tt x$ 的轻儿子不表示在他的儿子数组 $\tt ch[x][0/1]$ 中,而他的轻儿子 $\tt y$ 的 $\tt fa[y]$ 数组则 $\tt = x$ (即 $\tt fa[y] = x$ ) 每个节点仅包含在一个 $\tt Splay$ 中,一个 $\tt Splay$ 中没有深度相同的节点且深度连续。 ## 2. 操作 对于一个 $\tt Splay$ 的操作,在这里由于我太菜就不讲了 ### 1. $\tt access(x)

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

怎么做呢?

  1. 把当前节点转到 \tt Splay 的根
  2. 把左儿子更换为上一个 \tt Splay 的根 (把左子树变成用轻边连接,这时此节点为所在链的最底端,把上一个链变成用重边连接即可,就是把左子树换成上一个 \tt Splay)
  3. 更新数据
  4. 跳到当前点的父亲
\tt Code :
void access(int now) {
    int y = 0;
    while (now) {
        Splay(now);
        ch[now][1] = y;
        upd(now);
        y = now;
        now = fa[now];
    }
}

2.\tt makeroot(x)

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

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

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

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

\tt Code:
int findroot(int now) {
    access(now),Splay(now);
    while(ch[now][0]) {
        pushdown(now);
        now = ch[now][0];
    }
    Splay(now);
    return now;
}

4.\tt split(x,y)

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

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

\tt Code:
void split(int x,int y) {
    makeroot(x),access(y),Splay(y);
}

5.\tt link(x,y)

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

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

\tt Code:
void split(int x,int y) {
    makeroot(x),access(y),Splay(y);
}

6.\tt cut(x,y)

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

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

\tt Code:
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);
    }
}

\tt AC 代码

#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;
}