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

最新文章

  1. SOAPUI使用教程-MockService脚本概述
  2. Java构造函数
  3. 在eclipse中把之前的Tomcat 6删掉,不能再建
  4. mvc remote的验证
  5. jsoup Cookbook(中文版)--爬虫(java)
  6. (笔记)angular选项卡变色
  7. python实现树莓派生成并识别二维码
  8. NoSQL数据库有哪些
  9. IIS启用.net2.0
  10. 《Genesis-3D开源游戏引擎完整实例教程-跑酷游戏篇04:如何实现触控操作》
  11. CentOS7使用VPN上网
  12. DataTable与Linq相互转换
  13. 如何在Android Studio上使用Github
  14. 团队作业8——Beta 阶段冲刺4th day
  15. 如何程序化的构造Hibernate配置 // How to initialize Hibernate programmably
  16. 「PKUSC2018」神仙的游戏
  17. kepware http接口 php
  18. c语言:复合文字
  19. linux 初始设置
  20. 彻底弄懂jQuery事件原理二

热门文章

  1. ugui点击穿透判断
  2. 惠普IPMI登陆不上
  3. JavaScript Return Object.Type
  4. 指定pom文件jdk版本
  5. SQL Server之索引解析(二)
  6. vue axios应用
  7. spring boot 整合activemq
  8. [多校联考2019(Round 4 T2)][51nod 1288]汽油补给(ST表+单调栈)
  9. PyCharm控制台python shell 和 IPython shell的切换
  10. 01分数规划问题(二分法与Dinkelbach算法)