https://www.luogu.org/problemnew/show/P3379

求 a 和 b 的 LCA

考虑先 access(a),此时 a 和 root 在一条链上,再 access(b) 记录最后一个被 access 遇到的点,即为 LCA

因为LCT常数太大所以要开O2才能过

#include <bits/stdc++.h>
using namespace std; const int N = 500000 + 10; int fa[N], ch[N][2], val[N], rev[N], st[N], n, m, root, len; int isroot(int u) {
return ch[fa[u]][0] != u && ch[fa[u]][1] != u;
} int get(int u) {
return ch[fa[u]][1] == u;
} void pushdown(int u) {
if(rev[u]) {
swap(ch[u][0], ch[u][1]);
rev[ch[u][0]] ^= 1;
rev[ch[u][1]] ^= 1;
rev[u] ^= 1;
}
} void rotate(int u) {
int old = fa[u], oldd = fa[old], k = get(u);
if(!isroot(old)) ch[oldd][get(old)] = u; fa[u] = oldd;
fa[ch[u][k ^ 1]] = old; ch[old][k] = ch[u][k ^ 1];
fa[old] = u; ch[u][k ^ 1] = old;
} void splay(int u) {
st[len = 1] = u;
for(int i = u; !isroot(i); i = fa[i]) st[++len] = fa[i];
for(int i = len; i >= 1; i--) pushdown(st[i]);
for(; !isroot(u); rotate(u)) if(!isroot(fa[u])) rotate(get(u) == get(fa[u]) ? fa[u] : u);
} int access(int u) {
int tmp;
for(int i = 0; u; i = u, u = fa[u]) {
splay(u);
ch[u][1] = i;
tmp = u;
}
return tmp;
} void makeroot(int u) {
access(u);
splay(u);
rev[u] ^= 1;
} void link(int x, int y) {
makeroot(x);
fa[x] = y;
} int main() {
scanf("%d %d %d", &n, &m, &root);
for(int i = 1; i < n; i++) {
int a, b;
scanf("%d %d", &a, &b);
link(a, b);
}
makeroot(root);
for(int i = 1; i <= m; i++) {
int a, b;
scanf("%d %d", &a, &b);
access(a); printf("%d\n", access(b));
}
return 0;
}

Link cut tree 实现不高效的 LCA的更多相关文章

  1. link cut tree 入门

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

  2. Link Cut Tree 总结

    Link-Cut-Tree Tags:数据结构 ##更好阅读体验:https://www.zybuluo.com/xzyxzy/note/1027479 一.概述 \(LCT\),动态树的一种,又可以 ...

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

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

  4. Link/cut Tree

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

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

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

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

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

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

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

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

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

  9. Link Cut Tree学习笔记

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

随机推荐

  1. Halcon学习(八)文本操作

    标签: 学习 杂谈 分类: halcon学习 1.open_file( : : FileName, FileType : FileHandle) FileType: 'output':创建文本 ‘ap ...

  2. delphi 四舍五入

    trunc取整 四舍五入 formatfloat('0.00', 2.1850) 看第二位,然后对后面的数字处理,偶数的话舍去,奇数四舍五入 System.Math.RoundTo(tempval,- ...

  3. OK6410&nbsp;linux系统遇到的BUG总结

    经过一段时间使用OK6410 256M RAM 2G nand Flash碰见了不少问题. 所以特意开本贴一起交流.大家有什么BUG解决的可以跟上本帖.求助的请另开贴.勿跟本帖.谢谢.请谅解!!! 希 ...

  4. S3C6410移植u-boot

    1.首先下载u-boot(ftp://ftp.denx.de/pub/u-boot) wget ftp://ftp.denx.de/pub/u-boot/u-boot-latest.tar.bz2 2 ...

  5. Bootstrap 简介(Web前端CSS框架)

    目录1.简介2.特点3.组件4.Javascript插件5.定制自己的框架代码6.Bootstrap Less 1.简介Bootstrap是Twitter推出的一个开源的用于前端开发的工具包.它由Tw ...

  6. java基础篇之HashMap

    HashMap和C#中的Dictionary基本一样 键是唯一值 值可以是对象 循环HashMap的方式:一: 1,通过Set<T> st = hs.keySet()找到所有的key值集合 ...

  7. Professional C# 6 and .NET Core 1.0 - Chapter 38 Entity Framework Core

    本文内容为转载,重新排版以供学习研究.如有侵权,请联系作者删除. 转载请注明本文出处:Professional C# 6 and .NET Core 1.0 - Chapter 38 Entity F ...

  8. ARCGIS中怎么去除重复的面?(转)

    ARCGIS中怎么去除重复的面? https://blog.csdn.net/gswwldp/article/details/66974522   第一种: 1.用polygon to line将面转 ...

  9. 重命名Docker容器

    重命名Docker容器: Docker rename [Old container name]  [New container name]

  10. Ubuntu 源使用帮助

    地址 https://mirrors.ustc.edu.cn/ubuntu/ 说明 Ubuntu 软件源 收录架构 AMD64 (x86_64), Intel x86 其他架构请参考 Ubuntu P ...