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 ...
随机推荐
- java.net.UnknownHostException 异常处理(转)
在linux系统下部署Java产品的集群环境时,后台报出如下异常,系统报找不到名为“QATest242”的主机: ERROR - Get local host name failed -com.tr ...
- CentOS7服务器中apache、php7以及mysql5.7的安装配置代码
CentOS7服务器中apache.php7以及mysql5.7的配置代码如下所示: yum upgradeyum install net-tools 安装apache (http://m.86822 ...
- Django之深入了解视图层
目录 视图层三板斧 HttpResponse render redirect JsonResponse FBV CBV CBV源码 如何给FBV和CBV加装饰器 视图层三板斧 规定视图函数必须有一个返 ...
- Python3.6爬虫+Djiago2.0+Mysql --运行djiago环境
1.安装djiago 模块 pip install Django --默认安装最新的 安装完成以后可以python -m pip list 查看模块是否安装 2.创建项目及app 及生成目录 备注 ...
- 莫烦pytorch学习笔记(二)——variable
.简介 torch.autograd.Variable是Autograd的核心类,它封装了Tensor,并整合了反向传播的相关实现 Variable和tensor的区别和联系 Variable是篮子, ...
- SEO-----网站不被收录的原因
1. 新站的收录较慢 2. 文章质量不高 文章难以阅读 排版乱 内容是别的网站采集来的 很难被收录 3. 网站被降权中 4. 蜘蛛不访问[网站配置] 检查网站是否屏蔽了蜘蛛的爬取[ robots ...
- 使用 data-* 属性来嵌入自定义数据:
<!DOCTYPE html> <html> <head> <script> function showDetails(animal) { var an ...
- OpenCASCADE 平面求交
OpenCASCADE 平面求交 eryar@163.com OpenCASCADE提供了类IntAna_QuadQuadGeo用来计算两个二次曲面quadric(球面.圆柱面.圆锥面及平面,平面是二 ...
- MessageBox用法
一 函数原型及参数 function MessageBox(hWnd: HWND; Text, Caption: PChar; Type: Word): Integer; hWnd:对话框父窗口句柄, ...
- Windows API 第16篇 GetLogicalDrivers 获取驱动器位掩码
函数原型:DWORD GetLogicalDrives(VOID);The GetLogicalDrives function retrieves a bitmask representing the ...