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 ...
随机推荐
- 【CF622F】The Sum of the k-th Powers (拉格朗日插值法)
用的dls的板子,因为看不懂调了好久...果然用别人的板子就是这么蛋疼- -|| num数组0~k+1储存了k+2个值,且这k+2个值是自然数i的k次方而不是次方和,dls的板子自己帮你算和的...搞 ...
- NuGet 命令行使用EntityFrameWork
初始化 Enable-migrations 迁移 Add-Migration Donator_Add_CreationTime 执行操作 UpDate-database 撤销更改 Update-Dat ...
- jQuery插件编写,
jQuery插件编写 jQuery插件 最近搞jquery插件的编写这里做下笔记 给jquery扩展的方式很多,看的我眼花缭乱 方式1 $.fun=function(){} 方式2 $.fn.fun= ...
- 全面理解python中self的用法
self代表类的实例,而非类. class Test: def prt(self): print(self) print(self.__class__) t = Test() t.prt() 执行结果 ...
- HTML - 表单标签相关
<html> <head></head> <body> <!-- 表单标签 : 收集其标签内部的数据, 提交给指定的服务器 action : 数据 ...
- 第一个duilib程序 - 实现HelloWorld详解
duilib是一个windows下的皮肤库,用win32写的... 先看个效果图吧: 要使用duilib库,必须先把库导入,代码如下: View Row Code 1 #include "x ...
- SQLServer索引的四个高级特性
一Index Building Filter索引创建时过滤 二Index Include Column索引包含列 三聚集索引Cluster Index 四VIEW INDEX视图索引 SQLSer ...
- Android基础控件TextClock和Chronometer的使用
1.简介 DigitalClock, TextClock,AnalogClock,Chronometer其中DigitalClock和AnalogClock废弃了! TextClock是在Androi ...
- 安装hadoop伪分布式
修改hosts cat /etc/hosts 127.0.0.1 mo.don.com 创建用户 useradd hadoop passwd hadoop sudo授权 visudo hadoop A ...
- 转:五种I/O模型和select函数简介
源地址:http://blog.csdn.net/jnu_simba/article/details/9070955 一.五种I/O模型 1.阻塞I/O 我们在前面所说的I/O模型都是阻塞I/O,即调 ...