题目链接

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 ≤ NQ ≤ 100

40 points:

  • 1 ≤ NQ ≤ 105
  • There is less than 10 unique value of r in all queries

40 points:

  • 1 ≤ NQ ≤ 2 × 105

Example

Input:
4
1 2
2 3
1 4
2
1 4 2
2 4 2 Output:
1
2

Explanation

  • "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)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 洛谷 SP14932 LCA - Lowest Common Ancestor

    洛谷 SP14932 LCA - Lowest Common Ancestor 洛谷评测传送门 题目描述 A tree is an undirected graph in which any two ...

  4. 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 ...

  5. [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 ...

  6. [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 ...

  7. [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 ...

  8. 数据结构与算法(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 ...

  9. 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. 异常处理记录: Unable to compile class for JSP

    出错信息截图: 经过搜索引擎的帮助, 发现这些引发异常的可能原因: 1. tomcat的版本必须大于等于JDK的版本 2. maven中的jar与tomcat中jar冲突 看看pom.xml, 果然j ...

  2. 「题解」:[BZOJ4558]方

    问题: 方 时间限制: 2 Sec  内存限制: 256 MB 题面 题目描述 上帝说,不要圆,要方,于是便有了这道题.由于我们应该方,而且最好能够尽量方,所以上帝派我们来找正方形 上帝把我们派到了一 ...

  3. 二分图匹配——poj1469

    关于本题二分图的匹配关系始终是加单向边用左边去匹配右边,match表示的是右边的人匹配的对应的左边的点 /* 关于本题二分图的匹配 链接的关系始终是单向边 用左边去匹配右边,match表示的是右边的人 ...

  4. 微信H5支付签名校验错误

    参数一定按照我得顺序写,这样可以不用排序,签名在图二. H5支付最坑的一点就是文档坑爹!!!文档中有一个场景信息字段写的是必填,实际上是不需要的!!因为这个字段找了一下午bug,用签名校验工具是成功的 ...

  5. PAT甲级——A1082 Read Number in Chinese

    Given an integer with no more than 9 digits, you are supposed to read it in the traditional Chinese ...

  6. 整合SSH框架最基本的例子

    ssh框架整合 一.思路 1.导包 struts2: \apps\struts2-blank\WEB-INF\lib\所有包 struts2-spring-plugin-2.3.28.jar hibe ...

  7. MFC 多屏显示

    概念 HMONITOR : 显示器句柄. 有效的显示器,该值不为空. 当WM_DISPLAYCHANGE 心消息发送的时候, 任何小时起都有可能被移除, 所以应用程序时刻检查全部的HMONITORS是 ...

  8. 分批次删除大表数据的shell脚本

    #!/bin/bash # 分别是主机名,端口,用户,密码,数据库,表名称,字段名称 readonly HOST="XXX" readonly PORT=" readon ...

  9. Odoo中使用的部分表名及用途

    res_users 用户res_groups 用户组(角色)res_lang 语言res_partner 供应商/客户/联系人res_font 字体res_company 公司res_bank 银行r ...

  10. 利用zepto.js实现移动页面图片全屏滑动

    HTML <%-- Created by IntelliJ IDEA. User: fanso2o Date: 2017/2/28 Time: 16:09 To change this temp ...