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. autoit 简单使用-- 操作windows窗口

    autoit 简单使用 autoit中文交流论坛: http://www.autoitx.com/index.php 1 下载 autoit 中文安装包 : https://yunpan.cn/OcP ...

  2. Windows访问Linux下的共享目录的配置方法

    user安全级别 第一步:安装samba3(如果已经安装就跳过这一步)  [root@rhce2 /]# yum groupinstall "CIFS file server" 第 ...

  3. Valid timeZone Values(转)

    https://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/timezone.html Valid time ...

  4. Easyui Datagrid 如何实现后台交互显示用户数据列表

    转自:https://blog.csdn.net/Tomsheng321/article/details/50722571?utm_source=blogxgwz9 新手初学的时候可能有个疑问:如何在 ...

  5. openGL 纹理05

    纹理(Texture) 为了能够把纹理映射(Map)到三角形上,我们需要指定三角形的每个顶点各自对应纹理的哪个部分. 这样每个顶点就会关联着一个纹理坐标(Texture Coordinate) 用来标 ...

  6. 由ffmpeg中avformat模块中的 URL_SCHEME_CHARS 看 strspn( ) 的妙用

    在ffmpeg的avformat 模块中avio.c 对 URL_SCHEME_CHARS 的定义: #define URL_SCHEME_CHARS \ "abcdefghijklmnop ...

  7. kibana.yml(中文配置详解)

    # Kibana is served by a back end server. This controls which port to use. # server.port: 5601 # The ...

  8. 如何使用ThinkPHP5 ,自动生成目录?

    具体步骤: A.在build.php中按照实际需求修改定义模块的内容: B.修改Public/index.php,在代码中加入: // 读取自动生成定义文件 $build = include '/.. ...

  9. Dubbo管理中心部署

    我们在开发时,需要知道注册中心都注册了哪些服务,以便我们开发和测试.我们可以通过部署一个管理中心来实现.其实管理中心就是一个web应用,部署到tomcat即可. 管理端的部署: 1,首先我们要编译源码 ...

  10. python小程序:备份文件

    设计程序,有以下步骤: 需要备份的文件和目录由一个列表指定. 备份应该保存在主备份目录中. 文件备份成一个zip文件. zip存档的名称是当前的日期和时间. 解决方案: 版本一: #!/usr/bin ...