/* 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. 【2019 Multi-University Training Contest 4】

    01:https://www.cnblogs.com/myx12345/p/11644076.html 02: 03:https://www.cnblogs.com/myx12345/p/116478 ...

  2. paper 160:python 知识点概要 更新ing

    1.python json  http://www.runoob.com/python/python-json.html Python的json模块提供了一种很简单的方式来编码和解码JSON数据. 其 ...

  3. StatefulWidget 有状态组件

    在 Flutter 中自定义组件其实就是一个类,这个类需要继承 StatelessWidget/StatefulWidget: StatelessWidget 是无状态组件,状态不可变的 widget ...

  4. Sumdiv

    题目链接 题意:求a^b的所有约数之和mod9901. 思路:因为一个数A能够表示成多个素数的幂相乘的形式.即A=(a1^n1)*(a2^n2)*(a3^n3)...(am^nm).所以这个题就是要求 ...

  5. Spring-Boot"原生态"的logback

    前言 Logback是由 log4j创始人设计的又一个开源日志组件: logback当前分成三个模块: logback-core logbackclassic logback-access logba ...

  6. 强哥新周报SQL

    因为数据口径的更改,所以.强哥的SQL 比较好用.不会出麻烦. 总共有四个 日常记录下,好好看. -- 2019年4月核销新客 SELECT yzm2.consignee_phone AS `会员手机 ...

  7. 大数据学习笔记之Hadoop(二):HDFS文件系统

    文章目录 一 HDFS概念 1.1 概念 1.2 组成 1.3 HDFS 文件块大小 二 HFDS命令行操作 三 HDFS客户端操作 3.1 eclipse环境准备 3.1.1 jar包准备 3.2 ...

  8. nginx 配置代理 session 丢失

    最开始一直用的ip访问的项目是没问题的  , 后来改成用了域名访问就发现一直获取不了session ,最开始以为是跨域问题 , 但项目中已经配置了跨域 , 因为第一次用 nginx  ,果断入坑了 , ...

  9. PAT_A1073#Scientific Notation

    Source: PAT A1073 Scientific Notation (20 分) Description: Scientific notation is the way that scient ...

  10. java.lang -> Boolean

    java.lang -> Boolean 是什么 Boolean 类是将 boolean 基本类型进行包装.类型为 Boolean 的对象包含一个单一属性 value,其类型为 boolean. ...