HDU 5266 pog loves szh III(区间LCA)
题目链接 pog loves szh III
题意就是 求一个区间所有点的$LCA$。
我们把$1$到$n$的$DFS$序全部求出来……然后设$i$的$DFS$序为$c[i]$,$pc[i]$为$c[i]$的反函数。
区间的$LCA$其实就是,$DFS$序最大和最小的两个点的$LCA$。
(话说$2017$女生赛里面有一题要用的结论和这题的差不多)
然后求出区间的$DFS$序最大值$x$和最小值$y$。
然后求一下$LCA(pc[x],pc[y])$即可。
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i(a); i <= (b); ++i)
#define dec(i, a, b) for (int i(a); i >= (b); --i) typedef long long LL; const int N = 300010;
const int A = 21; int c[N];
int deep[N];
int f[N][A], g[N][A], st[N][A];
vector <int> v[N];
int ti;
int n, q;
int pc[N]; void ST(){
rep(i, 1, n) f[i][0] = c[i];
rep(j, 1, 20) rep(i, 1, n)
if ((i + (1 << j) - 1) <= n) f[i][j] = min(f[i][j - 1], f[i + (1 << (j - 1))][j - 1]); rep(i, 1, n) g[i][0] = c[i];
rep(j, 1, 20) rep(i, 1, n)
if ((i + (1 << j) - 1) <= n) g[i][j] = max(g[i][j - 1], g[i + (1 << (j - 1))][j - 1]);
} inline int solvemin(int l, int r){
int k = (int)log2((double)(r - l + 1));
return min(f[l][k], f[r - (1 << k) + 1][k]); } inline int solvemax(int l, int r){
int k = (int)log2((double)(r - l + 1));
return max(g[l][k], g[r - (1 << k) + 1][k]);
} void dfs(int x, int fa, int dep){
c[x] = ++ti; pc[ti] = x;
deep[x] = dep;
if (fa){
st[x][0] = fa;
for (int i = 0; st[st[x][i]][i]; ++i) st[x][i + 1] = st[st[x][i]][i];
} for (auto u : v[x]){
if (u == fa) continue;
dfs(u, x, dep + 1);
}
} int LCA(int a, int b){
if (deep[a] < deep[b]) swap(a, b);
for (int i = 0, delta = deep[a] - deep[b]; delta; delta >>= 1, ++i) if (delta & 1) a = st[a][i];
if (a == b) return a;
dec(i, 19, 0) if (st[a][i] != st[b][i]) a = st[a][i], b = st[b][i];
return st[a][0];
} int main(){ while (~scanf("%d", &n)){
rep(i, 0, n + 1) v[i].clear();
memset(c, 0, sizeof c);
ti = 0;
rep(i, 2, n){
int x, y;
scanf("%d%d", &x, &y);
v[x].push_back(y);
v[y].push_back(x);
} memset(st, 0, sizeof st);
memset(f, 0, sizeof f);
memset(g, 0, sizeof g);
dfs(1, 0, 0);
ST();
for (scanf("%d", &q); q--; ){
int x, y;
scanf("%d%d", &x, &y);
printf("%d\n", LCA(pc[solvemax(x, y)], pc[solvemin(x, y)])); }
} return 0;
}
HDU 5266 pog loves szh III(区间LCA)的更多相关文章
- hdu 5266 pog loves szh III(lca + 线段树)
I - pog loves szh III Time Limit:6000MS Memory Limit:131072KB 64bit IO Format:%I64d & %I ...
- HDU 5266 pog loves szh III ( LCA + SegTree||RMQ )
pog loves szh III Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Oth ...
- HDU 5266 pog loves szh III (线段树+在线LCA转RMQ)
题目地址:HDU 5266 这题用转RMQ求LCA的方法来做的很easy,仅仅须要找到l-r区间内的dfs序最大的和最小的就能够.那么用线段树或者RMQ维护一下区间最值就能够了.然后就是找dfs序最大 ...
- HDU 5266 pog loves szh III 线段树,lca
Pog and Szh are playing games. Firstly Pog draw a tree on the paper. Here we define 1 as the root of ...
- HDU 5266 pog loves szh III (LCA)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5266 题目就是让你求LCA,模版题.注意dfs会栈溢出,所以要扩栈,或者用bfs写. #pragma ...
- HDU 5266 pog loves szh III
题意:给出一棵树,1为根节点,求一段区间内所有点的最近公共祖先. 解法:用一棵线段树维护区间LCA.LCA是dp做法.dp[i][j]表示点i的第2^j个祖先是谁,转移方程为dp[i][j] = dp ...
- hdu5266 pog loves szh III 【LCA】【倍增】
Pog and Szh are playing games. Firstly Pog draw a tree on the paper. Here we define 1 as the root of ...
- hdu 5265 pog loves szh II
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5265 pog loves szh II Description Pog and Szh are pla ...
- hdu 5264 pog loves szh I
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5264 pog loves szh I Description Pog has lots of stri ...
随机推荐
- supervisor 安装使用
简介 Supervisor是用Python开发的一套通用的进程管理程序,能将一个普通的命令行进程变为后台daemon,并监控进程状态,异常退出时能自动重启.它是通过fork/exec的方式把这些被管 ...
- mongodb v2.4.9 快速操作记录
参考链接:http://www.runoob.com/mongodb/mongodb-tutorial.html oschina链接:https://gitee.com/dhclly/icedog.s ...
- 程序员必需知道的Mac OS使用技巧
macos sierra正式版发布了,于是我把我沉寂了一年没有用过了的macbook拿出来玩玩,顺便把一些常用技巧mark. 1.apple store下载软件无响应(经常出现的问题) 解决方法:更改 ...
- Selenium WebDriver-操作键盘事件
# 注意: !!!操作操作系统的按键,需要先装pywin32,然后通过交互模式import win32api和import win32con判断是否安装成功,需要重启下cmd进入交互模式# 下载链接: ...
- python 时间、日期、时间戳的转换
在实际开发中经常遇到时间格式的转换,例如: 前端传递的时间格式是字符串格式,我们需要将其转换为时间戳,或者前台传递的时间格式和我们数据库中的格式不对应,我们需要对其进行转换才能与数据库的时间进行匹配等 ...
- 【LeetCode】Balanced Binary Tree(平衡二叉树)
这道题是LeetCode里的第110道题. 题目要求: 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. ...
- [整理]tar压缩下来为什么格式是.tar.gz
前段时间打包,直接用tar命令压缩,压缩好的文件取名rar.同事用winrar打开发现一直报错. 经过查询发现,tar -cvzf压缩下来的格式其实应该是.tar.gz 但是格式怎么会这么奇怪呢?是压 ...
- sql语句执行时算术运算导致溢出。
执行sql语句时报错: 用户代码未处理 System.OverflowException HResult=-2146233066 Message=算术运算导致溢出. 文章:https://bbs.cs ...
- DS博客作业——树
DS博客作业--树 1.本周学习总结 1.思维导图 2.谈谈你对树结构的认识及学习体会. 在树这一章节,我们学习的是二叉树的算法. 树的构建:一种是直接给树的顺序存储结构的字符串,一种是通过先序遍历和 ...
- Windows杂技
WINDOWS下 ,在某目录下按住shift加鼠标右键,可以直接有当前目录的dos窗口 win10不能用 解决办法是在当前文件夹的地址栏输入cmd然后回车 发现Powershell可以当作dos执行相 ...