Least Common Ancestors
/* Least Common Ancestors
* Au: Small_Ash
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 500005, M = 1000005, MM = 20;
int n, m, s, d[N], l[N], f[M], fn; // d 是深度,l 是最左边对应位置,f 是 dfs 序
bool v[N]; // dfs 标记
int head[N], nex[M], to[M], en; // 邻接表
int r[M][MM]; // RMQ 用
inline void add(int x, int y) {
nex[++en] = head[x], head[x] = en, to[en] = y;
}
inline void push(int x) {
f[fn] = x; fn++;
}
void rmq() {
for (int i = 0; i < fn; i++) r[i][0] = f[i];
for (int j = 1; (1 << j) <= fn; j++)
for (int i = 0; i + (1 << j) - 1 < fn; i++) {
if (d[r[i][j - 1]] <= d[r[i + (1 << (j - 1))][j - 1]]) r[i][j] = r[i][j - 1];
else r[i][j] = r[i + (1 << (j - 1))][j - 1];
}
}
void dfs(int x, int t) {
v[x] = true; d[x] = t;
l[x] = fn, push(x);
for (int k = head[x]; k; k = nex[k]) {
if (v[to[k]]) continue;
dfs(to[k], t + 1);
push(x);
}
}
int lca(int x, int y) {
int k = 0; if (x > y) swap(x, y);
int temp = y - x + 1;
while ((1 << (k + 1)) <= temp) k++;
if (d[r[x][k]] <= d[r[y - (1 << k) + 1][k]]) return r[x][k];
else return r[y - (1 << k) + 1][k];
}
int main() {
scanf("%d%d%d", &n, &m, &s);
for (int i = 1, a, b; i < n; i++) {
scanf("%d%d", &a, &b);
add(a, b), add(b, a);
}
fn = 0;
dfs(s, 0);
rmq();
for (int i = 1, a, b; i <= m; i++) {
scanf("%d%d", &a, &b);
printf("%d\n", lca(l[a], l[b]));
}
return 0;
}
/**
* Least Common Ancestors
* std: [Luogu](https://www.luogu.org/problem/show?pid=3379)
**/
#include <bits/stdc++.h>
using namespace std;
const int N = 1000003;
int n, rmq[N][23], t, x, y;
int query(int l, int r) {
int k = int(log(r - l + 1) / log(2));
return max(rmq[l][k], rmq[r + 1 - (1 << k)][k]);
}
void st() {
for (int i = 1; i <= n; i++)
scanf("%d", &rmq[i][0]);
for (int j = 1; j <= int(log(n) / log(2)); j++)
for (int i = 1; i + (1 << j) - 1 <= n; i++)
rmq[i][j] = max(rmq[i][j - 1], rmq[i + (1 << (j - 1))][j - 1]);
}
int main() {
scanf("%d%d", &n, &t);
st();
for (int i = 1; i <= t; i++) {
scanf("%d%d", &x, &y);
printf("%d\n", query(x, y));
}
return 0;
}
Least Common Ancestors的更多相关文章
- POJ 1330 Nearest Common Ancestors(Targin求LCA)
传送门 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26612 Ac ...
- [最近公共祖先] POJ 1330 Nearest Common Ancestors
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 27316 Accept ...
- POJ 1330 Nearest Common Ancestors
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14698 Accept ...
- POJ1330 Nearest Common Ancestors
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24587 Acce ...
- POJ 1470 Closest Common Ancestors
传送门 Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 17306 Ac ...
- poj----(1470)Closest Common Ancestors(LCA)
Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 15446 Accept ...
- poj 1330 Nearest Common Ancestors LCA
题目链接:http://poj.org/problem?id=1330 A rooted tree is a well-known data structure in computer science ...
- POJ 1330 Nearest Common Ancestors(Tree)
题目:Nearest Common Ancestors 根据输入建立树,然后求2个结点的最近共同祖先. 注意几点: (1)记录每个结点的父亲,比较层级时要用: (2)记录层级: (3)记录每个结点的孩 ...
- 【POJ1330】Nearest Common Ancestors(树链剖分求LCA)
Description A rooted tree is a well-known data structure in computer science and engineering. An exa ...
- 【POJ】1330 Nearest Common Ancestors ——最近公共祖先(LCA)
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18136 Accept ...
随机推荐
- 5 August
P1016 旅行家的预算 单调队列. 再看看单调队列怎么用的. #include <cstdio> int n, l, r; double D, dd, d[9], C, p[9], an ...
- HTMLTestRunner优化:带截图、饼图、失败重跑
github地址:https://github.com/yoyoketang/sele_project_th9
- Html5 学习笔记 【PC固定布局】 实战3 热门旅游展示区
最终效果图: html 代码: <!DOCTYPE html> <html lang="zh-cn"> <head> <meta char ...
- selenium和phantomjs,完成豆瓣音乐排行榜的内容爬取
代码要多敲 注释要清晰 哪怕再简单 #使用selenium和phantomjs,完成豆瓣音乐排行榜的内容爬取 #地址:https://music.douban.com/chart #导入需要的模块 f ...
- C语言|博客作业12
一.我学到的内容(整理本课程所学,[用思维导图的方式] 二.我的收获(包括我完成的所有作业的链接+收获)不能只有作业链接,没有收获 作业链接 收获 https://edu.cnblogs.com/ca ...
- Java GC算法
转自:http://blog.csdn.net/heyutao007/article/details/38151581 1.JVM内存组成结构 JVM内存结构由堆.栈.本地方法栈.方法区等部分组成,结 ...
- shell脚本对代码执行时间的计时
时间秒: $(date +%s) 算数表达式: $(($cost_time/60)) $(($cost_time%60)) #!/bin/bash # ccache is from epel mirr ...
- javaIO流(一)--File类的基本使用
一.File文件操作类 在java语言中提供有对于文件操作系统的支持,这个支持就在java.io.File类中进行了定义,也就是说在整个java.io包中File类是唯一一个与文件本身操作有关的类(创 ...
- C++中的面向对象(一)
1,本节课开始进入 C++ 中的面向对象,面向对象是 C++ 中最核心也是体现 C++ 价 值的一个部分: 2,日常生活当中我们都习惯对事物进行分类,那么这种分类的思想是否可以引入到 程序设计中? ...
- Regular Expression 範例
Regular expression 被實作於各種語言中,可以用來對字串做 比對 擷取 分隔 這幾類的處理.以下是 JavaScript的處理範例. 各位看官,可以按F12開啟 brower 的 de ...