Lowest Common Ancestor (LCA)
In a rooted tree, the lowest common ancestor (or LCA for short) of two vertices u and v is defined as the lowest vertex that is ancestor of both that two vertices.
Given a tree of N vertices, you need to answer the question of the form "r u v" which means if the root of the tree is at r then what is LCA of u and v.
Input
The first line contains a single integer N. Each line in the next N - 1 lines contains a pair of integer u andv representing a edge between this two vertices.
The next line contains a single integer Q which is the number of the queries. Each line in the next Q lines contains three integers r, u, v representing a query.
Output
For each query, write out the answer on a single line.
Constraints
20 points:
- 1 ≤ N, Q ≤ 100
40 points:
- 1 ≤ N, Q ≤ 105
- There is less than 10 unique value of r in all queries
40 points:
- 1 ≤ N, Q ≤ 2 × 105
Example
Input:
4
1 2
2 3
1 4
2
1 4 2
2 4 2 Output:
1
2Explanation
- "1 4 2": if 1 is the root, it is parent of both 2 and 4 so LCA of 2 and 4 is 1.
- "2 4 2": the root of the tree is at 2, according to the definition, LCA of any vertex with 2 is 2.
题意:给出一棵N个结点的树,有Q次询问,每次询问给出三个数r,x,y。
求当以r作为树根时,x和y的lca
解决本题,有两个关键的地方:
1. 每次询问的答案只可能是: x, y, r, lca(x, y), lca(x, r), lca(y, r),这里的lca都是以1为树根时的lca
2. 如果 x = lca(u, v), 那么dist(x, root) + dist(x, u) + dist(x, v)的值是最小的。
Accepted Code:
/*************************************************************************
> File Name: TALCA.cpp
> Author: Stomach_ache
> Mail: sudaweitong@gmail.com
> Created Time: 2014年09月24日 星期三 17时39分16秒
> Propose:
************************************************************************/
#include <cmath>
#include <string>
#include <cstdio>
#include <vector>
#include <fstream>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
/*Let's fight!!!*/ const int MAX_N = ;
const int MAX_LOG = ;
typedef pair<int, int> pii;
int N, Q;
int p[MAX_N][MAX_LOG], depth[MAX_N];
vector<int> G[MAX_N]; void dfs(int u, int fa, int d) {
p[u][] = fa;
depth[u] = d;
for (int i = ; i < G[u].size(); i++) {
int v = G[u][i];
if (v != fa) dfs(v, u, d + );
}
} void init() {
dfs(, -, );
for (int k = ; k + < MAX_LOG; k++) {
for (int v = ; v <= N; v++) {
if (p[v][k] < ) p[v][k + ] = -;
else p[v][k + ] = p[p[v][k]][k];
}
}
} int lca(int u, int v) {
if (depth[u] > depth[v]) swap(u, v);
for (int k = ; k < MAX_LOG; k++) {
if ((depth[v] - depth[u]) >> k & ) {
v = p[v][k];
}
}
if (u == v) return u;
for (int k = MAX_LOG - ; k >= ; k--) {
if (p[u][k] != p[v][k]) {
u = p[u][k];
v = p[v][k];
}
}
return p[u][];
} int dist(int u, int v) {
int x = lca(u, v);
return depth[u] + depth[v] - * depth[x];
} int main(void) {
ios::sync_with_stdio(false);
while (cin >> N) {
for (int i = ; i <= N; i++) G[i].clear();
for (int i = ; i < N; i++) {
int u, v;
cin >> u >> v;
G[u].push_back(v);
G[v].push_back(u);
} init();
cin >> Q;
pii s[];
while (Q--) {
int r, u, v;
cin >> r >> u >> v;
s[].second = r;
s[].second = u;
s[].second = v;
s[].second = lca(r, u);
s[].second = lca(r, v);
s[].second = lca(u, v);
for (int i = ; i < ; i++) {
int x = s[i].second;
s[i].first = dist(u, x) + dist(v, x) + dist(r, x);
}
sort(s, s + );
cout << s[].second << endl;
}
}
return ;
}
Lowest Common Ancestor (LCA)的更多相关文章
- PAT A1143 Lowest Common Ancestor (30 分)——二叉搜索树,lca
The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U ...
- 235. Lowest Common Ancestor of a Binary Search Tree(LCA最低公共祖先)
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the ...
- 洛谷 SP14932 LCA - Lowest Common Ancestor
洛谷 SP14932 LCA - Lowest Common Ancestor 洛谷评测传送门 题目描述 A tree is an undirected graph in which any two ...
- PAT Advanced 1143 Lowest Common Ancestor (30) [二叉查找树 LCA]
题目 The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both ...
- [LeetCode] Lowest Common Ancestor of a Binary Tree 二叉树的最小共同父节点
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...
- [LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- [LeetCode]Lowest Common Ancestor of a Binary Search Tree
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- 数据结构与算法(1)支线任务4——Lowest Common Ancestor of a Binary Tree
题目如下:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ Given a binary tree, fin ...
- Lowest Common Ancestor of a Binary Search Tree
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
随机推荐
- 如何快速合并多个TXT文本内容
工作中有时候需要合并很多文本内容,例如一些推送清单之类,一个一个打开去复制粘贴的话,少量还行,如果txt文本数据量大(10+M以上)且文件数量多(成百上千),这种方式就显得很低效了.具体要求如下: ...
- 使用Cookie实现用户商品历史浏览记录
该功能分为四个模块: 1. 获取所有商品并以链接的形式显示 out.write("网站商品: <br/>"); Map<String, Book> book ...
- jmeter做bbs作业时提示404错误
在用jemter做bbs作业时候,注册成功后会跳转到主页,在写主页的脚本的时候,将fiddler抓到的url复制到路径下方“/bbs/forum.php”,但是第一次复制的时候在/bbs/forum. ...
- PostgreSQL:COALESCE函数
COALESCE函数是返回参数中的第一个非null的值,它要求参数中至少有一个是非null的,如果参数都是null会报错. select COALESCE(null,null); //报错 selec ...
- OCCT 7.4.0 beta version is available
OpenCASCADE 7.4.0测试版本发布 OCC在9月16号发布了opencascade740 beta测试版本,新版本里面做了如下一些重点修改如下: 造型算法部分主要对网格化算法BRepMes ...
- java-学习网站推荐
技术博客: http://c.biancheng.net/view/1390.html (设计模式等等应有尽有,最全教程,强烈推荐!!!) hutool:http://hutool.mydoc.io/ ...
- <每日一题>题目8:文件备份V1.0
import os #备份文件的路径 file_address = input("输入需要备份文件所在的路径:") os.chdir(file_address) #备份文件命名 f ...
- C# 读写 Photoshop PSD文件 操作类
分析了PSD的文件....才发现PSD的RGB色彩保存也是 先红 蓝 绿 这样保存的 ....麻烦的.. 另外8BIM好象没什么用..可以直接跳过..直接获取最后的图形信息就可以了.. 我只对一些PS ...
- PostgreSQL DISTINCT ON
https://stackoverflow.com/questions/3800551/select-first-row-in-each-group-by-group select DISTINCT ...
- Luogu P1850 换教室(期望dp)
P1850 换教室 题意 题目描述 对于刚上大学的牛牛来说,他面临的第一个问题是如何根据实际情况申请合适的课程. 在可以选择的课程中,有\(2n\)节课程安排在\(n\)个时间段上.在第\(i(1\l ...