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 ...
随机推荐
- [CSP-S模拟测试]:antipalindrome(数学)
题目传送门(内部题58) 输入格式 第一行一个数$T$表示数据组数.接下来每行两个数$n$和$m$. 输出格式 $T$行,每行一个答案,对${10}^9+7$取模. 样例 样例输入: 25 66 5 ...
- U-Boot是什么
U-Boot U-Boot,全称 Universal Boot Loader,是遵循GPL条款的开放源码项目.U-Boot的作用是系统引导.U-Boot从FADSROM.8xxROM.PPCBOOT逐 ...
- Where should I put <script> tags in HTML markup?
Where should I put <script> tags in HTML markup? When embedding JavaScript in an HTML document ...
- linux: 如何查看端口占用?
查看端口占用 $: netstat -anp | grep 8888 tcp 0 0 127.0.0.1:8888 0.0.0.0:* LISTEN 13404/python3 tcp 0 1 172 ...
- note《JavaScript 秘密花园》
点我跳转 (一)JavaScript-Garden-Object (二)JavaScript-Garden-Function (三)JavaScript-Garden-Array (四)JavaScr ...
- Html5 学习笔记 【PC固定布局】 实战3 热门旅游展示区
最终效果图: html 代码: <!DOCTYPE html> <html lang="zh-cn"> <head> <meta char ...
- Spring Boot & Redis 用起来真简单!
作者:java_老男孩 https://blog.51cto.com/14230003/2368721 Redis 是目前业界使用最广泛的内存数据存储.相比 Memcached,Redis 支持更丰 ...
- java 多线程间通信(一)
synchronized同步 package com.test7; public class Run { public class MyObject { private int a; public M ...
- for循环(C语言型)流程
- send, sendto, sendmsg - 从套接字发送消息
概述 #include <sys/types.h> #include <sys/socket.h> int send(int s, const void *msg, size_ ...