Link Cut Tree (动态树)【P3690】

LCT模板题解析
本文详细解析了一道关于链剖树(LCT)的算法题目,介绍了LCT的基础函数及其作用,如isroot(), pushup(), pushr(), Rotate(), splay(), access(), makeroot(), split(), link(), cut()等,并通过具体代码实现展示了如何处理点权值的XOR求和、连接、删除及修改操作。

题目链接

给定n个点以及每个点的权值,要你处理接下来的m个操作。操作有4种。操作从0到3编号。点从1到n编号。

0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和。保证x到y是联通的。

1:后接两个整数(x,y),代表连接x到y,若x到y已经联通则无需连接。

2:后接两个整数(x,y),代表删除边(x,y),不保证边(x,y)存在。

3:后接两个整数(x,y),代表将点x上的权值变成y。


  一道证明LCT的模版题,可以在这道题上证明一些自己对于LCT的一些理解。

首先就是几个基础的函数,有isroot(x)、pushup(x)、pushr(x)、pushdown(x)、Rotate(x)、splay(x)、access(x)、makeroot(x)、split(x, y)、link(x, y)、cut(x, y)……

  介绍一下这几个函数的分别作用,首先,我们知道LCT是一颗动态树,而其中最灵活的就是splay操作,所以有时候我们去寻找splay的根,我们不能判断是不是fa[]到头,因为这里还有一个叫做虚边的东西。那么这里我们就需要用到了isroot()这个函数,找的就是在这颗splay()中的根节点。

  然后再说pushup()还有pushdown()这个就没有说的必要了吧,感觉基本上大家都会理解。

  pushr()是一个很特殊的函数,我们要翻转一段区间的时候就是需要用到这个函数,或者更新某一个点的时候如果还要传递信息的时候也是需要这样的节点的。

  Rotate()操作中,有一些与splay()中的Rotate()不一样的地方,就是这里的根不一样了,有时候要更新的时候不一样的了,尤其是从某个几点推到根节点的时候就容易发生这样的事情。

  Splay()的操作不是那么的有区别 ,还是一样的了。

  access()操作就是把这个点x到原树的根结点的这一条链上的所有的点的边都拉成一条重链,放进一个splay中。

  makeroot()是把x变成整棵树的根节点。

  split(x, y)就是将x-y这条链给提取出来。

  link(x, y)连接x-y这两个块的边,如果他们本身不在一个联通块上的话。

  cut(x, y)将x-y这条边删除嘛,如果这条边没有的话,就不用删了。


#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
using namespace std;
const int maxN = 3e5 + 7;
int N, M, v[maxN];
int fa[maxN], c[maxN][2], s[maxN], r[maxN], st[maxN];   //st[]是模拟栈
inline bool isroot(int x) { return c[fa[x]][0] != x && c[fa[x]][1] != x; }
inline void pushup(int x) { s[x] = s[c[x][0]] ^ s[c[x][1]] ^ v[x]; }
inline void pushr(int x) { swap(c[x][0], c[x][1]); r[x] ^= 1; } //翻转操作
inline void pushdown(int x)
{
    if(r[x])
    {
        if(c[x][0]) pushr(c[x][0]);
        if(c[x][1]) pushr(c[x][1]);
        r[x] = 0;
    }
}
inline void Rotate(int x)   //splay的一次旋转操作
{
    int y = fa[x], z = fa[y], k = c[y][1] == x;
    if(!isroot(y)) c[z][c[z][1] == y] = x;  //***一定要加的判断,不然会死循环,在判断isroot的时候会出现问题
    fa[x] = z;
    c[y][k] = c[x][k^1];
    fa[c[x][k^1]] = y;
    c[x][k^1] = y;
    fa[y] = x;
    pushup(y); pushup(x);
}
inline void splay(int x)    //这里就不需要goal了,因为都是要把这个点移动到根上去的
{
    int y = x, z = 0;
    st[++z] = y;    //这个splay一定先把整条链上的都更新完了
    while(!isroot(y)) st[++z] = y = fa[y];
    while(z) pushdown(st[z--]);
    while(!isroot(x))
    {
        y = fa[x]; z = fa[y];
        if(!isroot(y)) (c[z][0] == y) ^ (c[y][0] == x) ? Rotate(x) : Rotate(y);
        Rotate(x);
    }
}
inline void access(int x)
{
    int y = 0;
    while(x)
    {
        splay(x); c[x][1] = y;
        pushup(x);
        y = x; x = fa[x];
    }
}
inline void makeroot(int x)
{
    access(x); splay(x);
    pushr(x);
}
int findroot(int x)
{
    access(x); splay(x);
    while(c[x][0]) { pushdown(x); x = c[x][0]; }
    splay(x);   //保证时间复杂度
    return x;
}
inline void split(int x, int y) //把x-y的这条边提取出来
{
    makeroot(x);
    access(y); splay(y);
}
inline void link(int x, int y)  //连边
{
    makeroot(x);
    if(findroot(y) != x) fa[x] = y;
}
inline void cut(int x, int y)
{
    makeroot(x);
    if(findroot(y) != x || fa[y] != x || c[y][0]) return;
    fa[y] = c[x][1] = 0;
    pushup(x);
}
int main()
{
    scanf("%d%d", &N, &M);
    for(int i=1; i<=N; i++) scanf("%d", &v[i]);
    int op, x, y;
    while(M--)
    {
        scanf("%d%d%d", &op, &x, &y);
        switch (op)
        {
            case 0: split(x, y); printf("%d\n", s[y]); break;
            case 1: link(x, y); break;
            case 2: cut(x, y); break;
            case 3: splay(x); v[x] = y; break;  //先讲x弄上去,不然会影响到splay前面的元素
            default: break;
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wuliwuliii

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值