/* 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的更多相关文章

  1. POJ 1330 Nearest Common Ancestors(Targin求LCA)

    传送门 Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26612   Ac ...

  2. [最近公共祖先] POJ 1330 Nearest Common Ancestors

    Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 27316   Accept ...

  3. POJ 1330 Nearest Common Ancestors

    Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14698   Accept ...

  4. POJ1330 Nearest Common Ancestors

      Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24587   Acce ...

  5. POJ 1470 Closest Common Ancestors

    传送门 Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 17306   Ac ...

  6. poj----(1470)Closest Common Ancestors(LCA)

    Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 15446   Accept ...

  7. poj 1330 Nearest Common Ancestors LCA

    题目链接:http://poj.org/problem?id=1330 A rooted tree is a well-known data structure in computer science ...

  8. POJ 1330 Nearest Common Ancestors(Tree)

    题目:Nearest Common Ancestors 根据输入建立树,然后求2个结点的最近共同祖先. 注意几点: (1)记录每个结点的父亲,比较层级时要用: (2)记录层级: (3)记录每个结点的孩 ...

  9. 【POJ1330】Nearest Common Ancestors(树链剖分求LCA)

    Description A rooted tree is a well-known data structure in computer science and engineering. An exa ...

  10. 【POJ】1330 Nearest Common Ancestors ——最近公共祖先(LCA)

    Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18136   Accept ...

随机推荐

  1. 使用C#登录带验证码的网站

    我在上一篇文章中已经讲解了一般网站的登录原来和C#的登录实现,很多人问到对于使用了验证码的网站该怎么办,这里我就讲讲验证码的原理和对应的登录方法.验证码的由来几年前,大部分网站.论坛之类的是没有验证码 ...

  2. hashmap:cr:csdn

    HashMap相关问题 1.你用过HashMap吗?什么是HashMap?你为什么用到它? 用过,HashMap是基于哈希表的Map接口的非同步实现,它允许null键和null值,且HashMap依托 ...

  3. [CSP-S模拟测试]:antipalindrome(数学)

    题目传送门(内部题58) 输入格式 第一行一个数$T$表示数据组数.接下来每行两个数$n$和$m$. 输出格式 $T$行,每行一个答案,对${10}^9+7$取模. 样例 样例输入: 25 66 5 ...

  4. HTML 3-列表、表单、语义化

    有序列表.无序列表.自定义列表在语义上的区别 1.有序列表ol>li:列表有顺序编号(默认列表前加1.,2.,3.,...编号),适用于有逻辑顺序的排列项目,而且次序不能乱.2.无序列表ul&g ...

  5. <HTTP权威指南>记录 ---- 网络爬虫

    网络爬虫 网络爬虫(web crawler)能够在无需人类干预的情况下自动进行一系列Web事务处理的软件程序.很多爬虫会从一个Web站点逛到另一个Web站点,获取内容,跟踪超链,并对它们找到的数据进行 ...

  6. (转)Docker network命令

    转:https://blog.csdn.net/gezhonglei2007/article/details/51627821 原文地址:https://docs.docker.com/engine/ ...

  7. 51单片机的idata,xdata,pdata,data的详解

    data: 固定指前面0x00-0x7f的128个RAM,可以用acc直接读写的,速度最快,生成的代码也最小. bit :是指0x20-0x2f的可位寻址区idata:固定指前面0x00-0xff的2 ...

  8. 资源-DotNet-站点:DotNet 站点列表

    ylbtech-资源-DotNet-站点:DotNet 站点列表 1.ASP.NET Web返回顶部 1.1.问卷星 https://www.wjx.cn/sample/service.aspx 1. ...

  9. Php邮件发送源码

    好久冒写点东西了.....最近生活压抑的很....为生活而劳累,整理下邮件发送的实例了,网上也有很多,我这个也是提取整理好的,测试Ok,首页邮件类smtp_email_class.php如下:< ...

  10. Html5 学习笔记 【PC固定布局】 实战5 咨询页面 侧栏

    最终效果图: 提出公共页脚和导航部分: 新建infomatino.html (旅游咨询)页面 <!DOCTYPE html> <html lang="zh-cn" ...