动态树是个好玩的东西

LCT题集

预备知识

Splay

树链剖分(好像关系并不大)

动态树(Link-cut tree)

先搬dalao博客

  • 什么是LCT?

    动态树是一类要求维护森林的连通性的题的总称,这类问题要求维护某个点到根的某些数据,支持树的切分,合并,以及对子树的某些操作。其中解决这一问题的某些简化版(不包括对子树的操作)的基础数据结构就是LCT(link-cut tree)。

    LCT的大体思想类似于树链剖分中的轻重链剖分,轻重链剖分是处理出重链来,由于重链的定义和树链剖分是处理静态树所限,重链不会变化,变化的只是重链上的边或点的权值,由于这个性质,我们用线段树来维护树链剖分中的重链,但是LCT解决的是动态树问题(包含静态树),所以需要用更灵活的Splay来维护这里的“重链”

讲了这么多,其实就是维护森林的树链剖分,每一条链用Splay维护

LCT的主要性质

  1. 每一个Splay维护的是一条从上到下按在原树中深度严格递增的路径,且中序遍历Splay得到的每个点的深度序列严格递增。

    比如有一棵树,根节点为1(深度1),有两个儿子2,3(深度2),那么Splay有3种构成方式:

    {1−2},{3}

    {1−3},{2}

    {1},{2},{3}(每个集合表示一个Splay)

    而不能把1,2,3同放在一个Splay中(存在深度相同的点)

  2. 每个节点包含且仅包含于一个Splay中

  3. 边分为实边和虚边,实边包含在Splay中,而虚边总是由一棵Splay指向另一个节点(指向该Splay中中序遍历最靠前的点在原树中的父亲)。

    因为性质2,当某点在原树中有多个儿子时,只能向其中一个儿子拉一条实链(只认一个儿子),而其它儿子是不能在这个Splay中的。

    那么为了保持树的形状,我们要让到其它儿子的边变为虚边,由对应儿子所属的Splay的根节点的父亲指向该点,而从该点并不能直接访问该儿子(认父不认子)。

各种操作

access(x)

打通\(x\)到动态树的根的路径(使路径上的所有点在同一个Splay中)

void access(int x) {
for (int y = 0; x; y = x, x = t[x].fa)
splay(x), t[x].ch[1] = y, pushup(x);
return ;
}

makeroot(x)

使\(x\)变成动态树的根

void makeroot(int x) {
access(x); splay(x);putrev(x);
return ;
}

findroot(x)

用来判断连通性(类似并查集)(findroot(x)==findroot(y)表明x,y在同一棵树中)

inline int findroot(int x) {
access(x); splay(x);
while (t[x].ch[0]) pushdown(x), x = t[x].ch[0];
return x;
}

split(x,y)

打通\(x\)到\(y\)的路径(类似access)

void split(int x, int y) {
makeroot(x), access(y), splay(y);
return ;
}

link(x,y)

连接\(x-y\)

void link(int x, int y) {
makeroot(x);
if (findroot(y) == x) return ;//已经连通
t[x].fa = y;//如果写成t[y].fa = x, y与原先的树就断开,就不会与其连通
return ;
}

cut(x, y)

断开\(x-y\)

void cut(int x, int y) {
makeroot(x);
if (findroot(y) == x && t[x].fa == y && !t[x].ch[1])//满足它们直接连接才断开
t[x].fa = t[y].ch[0] = 0, pushup(y);
return ;
}

例题

洛谷P3690 【模板】Link Cut Tree (动态树)

Code

#include<bits/stdc++.h>

#define LL long long
#define RG register
const int N = 300010;
using namespace std; inline int gi() {
RG int x = 0; RG char c = getchar(); bool f = 0;
while (c != '-' && (c < '0' || c > '9')) c = getchar();
if (c == '-') c = getchar(), f = 1;
while (c >= '0' && c <= '9') x = x*10+c-'0', c = getchar();
return f ? -x : x;
} struct node {
int v, s, fa, ch[2];
bool rev;
}t[N];
int S[N], top;
void putrev(int x) {
swap(t[x].ch[0], t[x].ch[1]);
t[x].rev ^= 1;
return ;
}
#define pushup(x) (t[x].s = (t[x].v^t[t[x].ch[0]].s^t[t[x].ch[1]].s))
void pushdown(int x) {
if (t[x].rev) {
if (t[x].ch[0]) putrev(t[x].ch[0]);
if (t[x].ch[1]) putrev(t[x].ch[1]);
t[x].rev = 0;
}
return ;
}
#define get(x) (t[t[x].fa].ch[1]==x)
bool isroot(int x) {
return (t[t[x].fa].ch[0] != x) && (t[t[x].fa].ch[1] != x);
}
void rotate(int x) {
int k = get(x), y = t[x].fa, z = t[y].fa;
if (!isroot(y)) t[z].ch[get(y)] = x;
t[x].fa = z;
t[t[x].ch[k^1]].fa = y, t[y].ch[k] = t[x].ch[k^1];
t[y].fa = x, t[x].ch[k^1] = y;
pushup(y);
return ;
}
void splay(int x) {
S[top = 1] = x;
for (RG int i = x; !isroot(i); i = t[i].fa) S[++top] = t[i].fa;
for (RG int i = top; i; i--) pushdown(S[i]);
while (!isroot(x)) {
int y = t[x].fa;
if (!isroot(y))
(get(x) ^ get(y)) ? rotate(x) : rotate(y);
rotate(x);
}
pushup(x);
return ;
}
void access(int x) {
for (int y = 0; x; y = x, x = t[x].fa)
splay(x), t[x].ch[1] = y, pushup(x);
return ;
}
void makeroot(int x) {
access(x); splay(x);putrev(x);
return ;
}
inline int findroot(int x) {
access(x); splay(x);
while (t[x].ch[0]) pushdown(x), x = t[x].ch[0];
return x;
}
void link(int x, int y) {
makeroot(x);
if (findroot(y) == x) return ;
t[x].fa = y;
return ;
}
void split(int x, int y) {
makeroot(x), access(y), splay(y);
return ;
}
void cut(int x, int y) {
makeroot(x);
if (findroot(y) == x && t[x].fa == y && !t[x].ch[1])
t[x].fa = t[y].ch[0] = 0, pushup(y);
return ;
} int main() {
int n = gi(), T = gi();
for (RG int i = 1; i <= n; i++) t[i].v = gi();
while (T--) {
int op = gi(), x = gi(), y = gi();
if (!op) {
split(x, y);
printf("%d\n", t[y].s);
}
else if (op == 1) link(x, y);
else if (op == 2) cut(x, y);
else {
access(x); splay(x); t[x].v = y; pushup(x);
}
}
return 0;
}

动态树Link-cut tree(LCT)总结的更多相关文章

  1. 动态树(Link Cut Tree) :SPOJ 375 Query on a tree

    QTREE - Query on a tree #number-theory You are given a tree (an acyclic undirected connected graph) ...

  2. 洛谷P3690 [模板] Link Cut Tree [LCT]

    题目传送门 Link Cut Tree 题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代 ...

  3. BZOJ 3282 Link Cut Tree (LCT)

    题目大意:维护一个森林,支持边的断,连,修改某个点的权值,求树链所有点点权的异或和 洛谷P3690传送门 搞了一个下午终于明白了LCT的原理 #include <cstdio> #incl ...

  4. Luogu 3690 Link Cut Tree

    Luogu 3690 Link Cut Tree \(LCT\) 模板题.可以参考讲解和这份码风(个人认为)良好的代码. 注意用 \(set\) 来维护实际图中两点是否有直接连边,否则无脑 \(Lin ...

  5. LCT总结——概念篇+洛谷P3690[模板]Link Cut Tree(动态树)(LCT,Splay)

    为了优化体验(其实是强迫症),蒟蒻把总结拆成了两篇,方便不同学习阶段的Dalao们切换. LCT总结--应用篇戳这里 概念.性质简述 首先介绍一下链剖分的概念(感谢laofu的讲课) 链剖分,是指一类 ...

  6. LuoguP3690 【模板】Link Cut Tree (动态树) LCT模板

    P3690 [模板]Link Cut Tree (动态树) 题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两 ...

  7. P3690 【模板】Link Cut Tree (动态树)

    P3690 [模板]Link Cut Tree (动态树) 认父不认子的lct 注意:不 要 把 $fa[x]$和$nrt(x)$ 混 在 一 起 ! #include<cstdio> v ...

  8. 【刷题】洛谷 P3690 【模板】Link Cut Tree (动态树)

    题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor ...

  9. LG3690 【模板】Link Cut Tree (动态树)

    题意 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和.保证x到y是联通的 ...

  10. Link Cut Tree学习笔记

    从这里开始 动态树问题和Link Cut Tree 一些定义 access操作 换根操作 link和cut操作 时间复杂度证明 Link Cut Tree维护链上信息 Link Cut Tree维护子 ...

随机推荐

  1. 安装CentOS 6.4 64 位操作系统

    1.安装 CentOS 6.4 64位操作系统的一些困境: 1.1 CentOS 6.4 64位操作系统的ISO文件有4G多,通过U盘安装的方式已经不可取(FAT32 只支持最大4G文件); 1.2 ...

  2. Go 笔记和疑问?

    前言: 本文是学习<<go语言程序设计>> -- 清华大学出版社(王鹏 编著) 的2014年1月第一版 做的一些笔记 , 如有侵权, 请告知笔者, 将在24小时内删除, 转载请 ...

  3. 【转载】Redis优化经验

    转载地址:http://blog.sina.com.cn/s/blog_4be888450100z2ze.html 内存管理优化 Redis Hash是value内部为一个HashMap,如果该Map ...

  4. zrender源码分析2--初始化Storage

    接上次分析到初始化ZRender的源码,这次关注内容仓库Storage的初始化 入口1:new Storage(); // zrender.js /** * ZRender接口类,对外可用的所有接口都 ...

  5. Cocos2d-x-2.2.2开发环境配置

    1.安装各种软件: Android SDK Android NDK Apache Ant Python Eclipse(adt) Cygwin(可选) Java Cocos2d-x 2.系统环境变量配 ...

  6. 【小梅哥FPGA进阶教程】MC8051软核在FPGA上的使用

    十.MC8051软核在FPGA上的使用 本教程内容力求以详细的步骤和讲解让读者以最快的方式学会 MC8051 IP core 的应用以及相关设计软件的使用,并激起读者对 SOPC 技术的兴趣.本实验重 ...

  7. CodeForces 376F Tree and Queries(假·树上莫队)

    You have a rooted tree consisting of n vertices. Each vertex of the tree has some color. We will ass ...

  8. GitHub操作总结

    GitHub操作总结 : 总结看不明白就看下面的详细讲解. . 作者:万境绝尘 转载请注明出处:http://blog.csdn.net/shulianghan/article/details/188 ...

  9. 微软编程一小时 题目2: Longest Repeated Sequence

    题目2 : Longest Repeated Sequence 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 You are given a sequence of i ...

  10. Ubuntu 安装 Memcached

    直接使用命令 sudo apt-get install Memcached 进行安装 安装完, 默认只能本地连接,需要修改一下配制文件,打开 /etc/Memcached.conf 文件, 找到一个 ...