Link-cut Tree是一种支持改变树(森林)的形态(link和cut),同时维护树的路径上节点信息的数据结构。lct通过splay来维护每次的perferred path,说白了就是一个动态的树链剖分。splay的左右儿子分别代表preferred path上深度比它小和深度比它大的节点。

Link-cut Tree需要支持以下的操作:

1.access(x): 将节点x连接到perferred path上,返回值是一个节点,如果这是第一次access,则返回根节点,如果之前还access其他节点,则返回lca(last, x)。
2.makeroot(x): 将x这个节点作为根,也就是换根操作。
3.link(x,y): 连接x和y所在的子树。
4.cut(x,y): 将x和y之间的边删除。

我们先来看看access操作,借用Yang Zhe大神论文里的一张图来对access操作有一个形象的理解:

这一次access过后,从N到根的路径都变成了perferred path。

然后我们再来看看makeroot操作,我们单次makeroot操作就是把需要作为根的节点access,然后splay再到顶端,再通过给这个节点打上rev标记来将左右儿子交换。(不理解的可以画一张图理解一下。)
这下有了makeroot也有了access,就很容易地处理出一条链上的信息了,link就只要makeroot(a),fa[a]=b就好了,cut只要makeroot(a),access(b),splay(a),ch[a][0]=fa[b]=0就能解决。

参考模板:

//
// Title : LCT(change root)
// Date : 03.05.2016
// Test : BZOJ-2049
// Complexity : O(mlogn)
//
/*
对于有link和cut操作维护树上的信息等问题——
解决办法:link-cut tree
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath> #ifdef WIN32
#define LL "%I64d"
#else
#define LL "%lld"
#endif #ifdef CT
#define debug(...) printf(__VA_ARGS__)
#define setfile()
#else
#define debug(...)
#define filename ""
#define setfile() freopen(filename".in", "r", stdin); freopen(filename".out", "w", stdout);
#endif #define R register
#define getc() (S == T && (T = (S = B) + fread(B, 1, 1 << 15, stdin), S == T) ? EOF : *S++)
#define dmax(_a, _b) ((_a) > (_b) ? (_a) : (_b))
#define dmin(_a, _b) ((_a) < (_b) ? (_a) : (_b))
#define cmax(_a, _b) (_a < (_b) ? _a = (_b) : 0)
#define cmin(_a, _b) (_a > (_b) ? _a = (_b) : 0)
char B[1 << 15], *S = B, *T = B;
inline int FastIn()
{
R char ch; R int cnt = 0; R bool minus = 0;
while (ch = getc(), (ch < '0' || ch > '9') && ch != '-') ;
ch == '-' ? minus = 1 : cnt = ch - '0';
while (ch = getc(), ch >= '0' && ch <= '9') cnt = cnt * 10 + ch - '0';
return minus ? -cnt : cnt;
}
#define maxn 200010
int n, m;
struct Node *null;
struct Node
{
bool rev;
Node *ch[2], *fa;
inline bool type()
{
return fa -> ch[1] == this;
}
inline bool check()
{
return this == fa -> ch[type()];
}
inline void set_rev()
{
std::swap(ch[0], ch[1]);
rev ^= 1;
}
inline void pushdown()
{
if (rev)
{
ch[0] -> set_rev();
ch[1] -> set_rev();
rev = 0;
}
}
void pushdownall()
{
if (check())
fa -> pushdownall();
pushdown();
}
inline void rotate()
{
R Node *f = fa;
R bool d = type();
(fa = f -> fa), f -> check() ? fa -> ch[f -> type()] = this : 0;
(f -> ch[d] = ch[!d]) != null ? ch[!d] -> fa = f : 0;
(ch[!d] = f) -> fa = this;
}
inline void splay(R bool need = 1)
{
if (need) pushdownall();
for (; check(); rotate())
if (fa -> check())
(type() != fa -> type() ? this : fa) -> rotate();
}
inline Node *access()
{
R Node *i = this, *j = null;
for (; i != null; i = (j = i) -> fa)
{
i -> splay();
i -> ch[1] = j;
}
return j;
}
inline void make_root()
{
access();
splay(0);
set_rev();
}
inline void link(R Node *that)
{
make_root();
fa = that;
}
inline void cut(R Node *that)
{
make_root();
that -> access();
splay(0);
that -> fa = ch[1] = null;
}
inline bool find(R Node *that)
{
access();
splay();
while (that -> fa != null)
that = that -> fa;
return that == this;
}
}mem[maxn];
int main()
{
// setfile();
n = FastIn(), m = FastIn();
null = mem;
null -> fa = null -> ch[0] = null -> ch[1] = null;
for (R int i = 1; i <= n; ++i) mem[i] = (Node) {0, {null, null}, null};
for (R int i = 1; i <= m; ++i)
{
R char opt;
while (opt = getc(), opt < 'A' || opt > 'Z');
R int a = FastIn(), b = FastIn();
if (opt == 'C')
{
(mem + a) -> link(mem + b);
}
else if (opt == 'D')
{
(mem + a) -> cut(mem + b);
}
else
{
puts((mem + a) -> find(mem + b) ? "Yes" : "No");
}
}
return 0;
}

练习建议:
BZOJ2002 [Hnoi2010]Bounce 弹飞绵羊
BZOJ2049 [Sdoi2008]Cave 洞穴勘测
BZOJ3282 Tree

【学习心得】Link-cut Tree的更多相关文章

  1. Link Cut Tree学习笔记

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

  2. 学习笔记:Link Cut Tree

    模板题 原理 类似树链剖分对重儿子/长儿子剖分,Link Cut Tree 也做的是类似的链剖分. 每个节点选出 \(0 / 1\) 个儿子作为实儿子,剩下是虚儿子.对应的边是实边/虚边,虚实时可以进 ...

  3. link cut tree 入门

    鉴于最近写bzoj还有51nod都出现写不动的现象,决定学习一波厉害的算法/数据结构. link cut tree:研究popoqqq那个神ppt. bzoj1036:维护access操作就可以了. ...

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

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

  5. Codeforces Round #339 (Div. 2) A. Link/Cut Tree 水题

    A. Link/Cut Tree 题目连接: http://www.codeforces.com/contest/614/problem/A Description Programmer Rostis ...

  6. Link/cut Tree

    Link/cut Tree 一棵link/cut tree是一种用以表示一个森林,一个有根树集合的数据结构.它提供以下操作: 向森林中加入一棵只有一个点的树. 将一个点及其子树从其所在的树上断开. 将 ...

  7. 洛谷P3690 Link Cut Tree (模板)

    Link Cut Tree 刚开始写了个指针版..调了一天然后放弃了.. 最后还是学了黄学长的板子!! #include <bits/stdc++.h> #define INF 0x3f3 ...

  8. bzoj2049 [Sdoi2008]Cave 洞穴勘测 link cut tree入门

    link cut tree入门题 首先说明本人只会写自底向上的数组版(都说了不写指针.不写自顶向下QAQ……) 突然发现link cut tree不难写... 说一下各个函数作用: bool isro ...

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

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

  10. [CodeForces - 614A] A - Link/Cut Tree

    A - Link/Cut Tree Programmer Rostislav got seriously interested in the Link/Cut Tree data structure, ...

随机推荐

  1. codeforces 1186C Vus the Cossack and Strings

    题目链接:https://codeforc.es/contest/1186/problem/C 题目大意:xxxxx(自认为讲不清.for instance) 例如:a="01100010& ...

  2. mysql先分组,然后取每个分组中的第2大的记录

    文章参考http://www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-group-in-sql/ 首先建表: ...

  3. /etc/syscofig/network 修改主机名

    [root@mysql ~]# cat /etc/sysconfig/network NETWORKING=yes HOSTNAME=mysql

  4. 第九周总结&第七次实验报告

    实验7 实验任务详情: 完成火车站售票程序的模拟. 要求: (1)总票数1000张: (2)10个窗口同时开始卖票: (3)卖票过程延时1秒钟: (4)不能出现一票多卖或卖出负数号票的情况. 实验过程 ...

  5. [Web 前端] 031 bootstrap 的使用和全局 css 样式

    目录 0. 前言 1. 基本模板 2. 布局容器 2.1 container 2.2 container-fluid 3. 栅格系统 3.1 简介 3.2 栅格参数 3.3 实例:从堆叠到水平排列 2 ...

  6. Nginx 的root和 alias

    nginx是通过alias设置虚拟目录,在nginx的配置中,alias目录和root目录是有区别的:1)alias指定的目录是准确的,即location匹配访问的path目录下的文件直接是在alia ...

  7. Thinkphp3.2 Redis缓存session

    Thinkphpsession缓存没有redis类库 Redis.class.php放在Library/Think/Session/Driver/下: <?php /** * +-------- ...

  8. python day1-requests

    一.什么是requests Requests是用python语言基于urllib编写的,采用的是Apache2 Licensed开源协议的HTTP库. 相对于urllib库(自带,无需手动安装)而言, ...

  9. Python 余弦相似度与皮尔逊相关系数 计算

    夹角余弦(Cosine) 也可以叫余弦相似度. 几何中夹角余弦可用来衡量两个向量方向的差异,机器学习中借用这一概念来衡量样本向量之间的差异. (1)在二维空间中向量A(x1,y1)与向量B(x2,y2 ...

  10. Mysql逻辑架构介绍

    总体概览: 和其它数据库相比,MySQL有点与众不同,它的架构可以在多种不同场景中应用并发挥良好作用.主要体现在存储引擎的架构上,插件式的存储引擎架构将查询处理和其它的系统任务以及数据的存储提取相分离 ...