Nearest Common Ancestors
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 24587   Accepted: 12779

Description

A rooted tree is a well-known data structure in computer science and engineering. An example is shown below:

 
In the figure, each node is labeled with an integer from {1, 2,...,16}. Node 8 is the root of the tree. Node x is an ancestor of node y if node x is in the path between the root and node y. For example, node 4 is an ancestor of node 16. Node 10 is also an ancestor of node 16. As a matter of fact, nodes 8, 4, 10, and 16 are the ancestors of node 16. Remember that a node is an ancestor of itself. Nodes 8, 4, 6, and 7 are the ancestors of node 7. A node x is called a common ancestor of two different nodes y and z if node x is an ancestor of node y and an ancestor of node z. Thus, nodes 8 and 4 are the common ancestors of nodes 16 and 7. A node x is called the nearest common ancestor of nodes y and z if x is a common ancestor of y and z and nearest to y and z among their common ancestors. Hence, the nearest common ancestor of nodes 16 and 7 is node 4. Node 4 is nearer to nodes 16 and 7 than node 8 is.

For other examples, the nearest common ancestor of nodes 2 and 3 is node 10, the nearest common ancestor of nodes 6 and 13 is node 8, and the nearest common ancestor of nodes 4 and 12 is node 4. In the last example, if y is an ancestor of z, then the nearest common ancestor of y and z is y.

Write a program that finds the nearest common ancestor of two distinct nodes in a tree.

Input

The input consists of T test cases. The number of test cases (T) is given in the first line of the input file. Each test case starts with a line containing an integer N , the number of nodes in a tree, 2<=N<=10,000. The nodes are labeled with integers 1, 2,..., N. Each of the next N -1 lines contains a pair of integers that represent an edge --the first integer is the parent node of the second integer. Note that a tree with N nodes has exactly N - 1 edges. The last line of each test case contains two distinct integers whose nearest common ancestor is to be computed.

Output

Print exactly one line for each test case. The line should contain the integer that is the nearest common ancestor.

Sample Input

2
16
1 14
8 5
10 16
5 9
4 6
8 4
4 10
1 13
6 15
10 11
6 7
10 2
16 3
8 1
16 12
16 7
5
2 3
3 4
3 1
1 5
3 5

Sample Output

4
3

Source

LCA(最近公共祖先)

先找到树根,之后倍增处理出每个点的祖先结点,然后同时上溯即可。

 //lca
#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
using namespace std;
const int mxn=;
int n;
vector<int> e[mxn];
int fa[mxn][];
int dep[mxn];
int in[mxn];
void add_edge(int u,int v){
e[u].push_back(v);
in[v]++;
}
void dfs(int u){//求点深度,倍增算祖先结点fa
for(int i=;i<e[u].size();i++){
int v=e[u][i];
fa[v][]=u;
dep[v]=dep[u]+;
for(int j=;(<<j)<=dep[v];j++){
fa[v][j]=fa[fa[v][j-]][j-];
}
dfs(v);
}
return;
}
int lca(int a,int b){
if(dep[a]<dep[b])swap(a,b);//(处理深度更大的那个)
for(int i=;i!=-;i--)//从高位开始试。(其实从低开始也一样?)
if(dep[a]>=dep[b]+(<<i))a=fa[a][i];//此步结束后,a和b查找到的深度相同
if(a==b)return a;
for(int i=;i!=-;i--)//共同上溯
if(fa[a][i]!=fa[b][i])a=fa[a][i],b=fa[b][i];
return fa[a][];
}
int main(){
int T;
scanf("%d",&T);
int i,j;
while(T--){
memset(dep,,sizeof(dep));
memset(fa,,sizeof(fa));
memset(in,,sizeof(in));
scanf("%d",&n);
for(i=;i<=n;i++) e[i].clear();
int u,v;
for(i=;i<n;i++){
scanf("%d%d",&u,&v);
add_edge(u,v);
}
int root=;
for(i=;i<=n;i++)if(in[i]==){root=i;break;}//入度为0的那个点是根
dep[root]=;
fa[root][]=-;
dfs(root);
int a,b;
scanf("%d%d",&a,&b);
printf("%d\n",lca(a,b));
}
return ;
}

POJ1330 Nearest Common Ancestors的更多相关文章

  1. POJ1330 Nearest Common Ancestors(最近公共祖先)(tarjin)

    A - Nearest Common Ancestors Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld &am ...

  2. POJ1330 Nearest Common Ancestors (JAVA)

    经典LCA操作.. 贴AC代码 import java.lang.reflect.Array; import java.util.*; public class POJ1330 { // 并查集部分 ...

  3. [POJ1330]Nearest Common Ancestors(LCA, 离线tarjan)

    题目链接:http://poj.org/problem?id=1330 题意就是求一组最近公共祖先,昨晚学了离线tarjan,今天来实现一下. 个人感觉tarjan算法是利用了dfs序和节点深度的关系 ...

  4. POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA)

    POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA) Description A ...

  5. POJ 1330 Nearest Common Ancestors (LCA,dfs+ST在线算法)

    Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14902   Accept ...

  6. JDOJ 3055: Nearest Common Ancestors

    JDOJ 3055: Nearest Common Ancestors JDOJ传送门 Description 给定N个节点的一棵树,有K次查询,每次查询a和b的最近公共祖先. 样例中的16和7的公共 ...

  7. POJ 1330 Nearest Common Ancestors(Targin求LCA)

    传送门 Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26612   Ac ...

  8. [最近公共祖先] POJ 1330 Nearest Common Ancestors

    Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 27316   Accept ...

  9. POJ 1330 Nearest Common Ancestors

    Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14698   Accept ...

随机推荐

  1. smarty中section遍历数组

    smarty中section遍历数组

  2. no.3 base64

    tool.chinaz.com的base64有的无法解密: 如:ZXZhbCgkX1BPU1RbcDRuOV96MV96aDNuOV9qMXVfU2gxX0oxM10pNTU2NJC3ODHHYWJI ...

  3. Jdeveloper 太慢 slowly

    https://blogs.oracle.com/shay/entry/is_your_jdeveloper_slow_it_sho http://bexhuff.com/2012/09/jdevel ...

  4. php基础19:文件

    <?php //1.打开文件的更好的方法是通过 fopen() 函数.此函数为您提供比 readfile() 函数更多的选项. //fopen() 的第一个参数包含被打开的文件名,第二个参数规定 ...

  5. IBatis.net介绍

    IBatis.net介绍 IBatis.net 是2001年发起的开源项目,它是一个轻量级的ORM框架,现在IBatisNET已经是属于Apache下的一个子项目了,最新版本是1.6.2. 官方网站: ...

  6. 分享到微信微博空间等第三方平台的JS代码

    分享功能有利于传播更多优质的内容,所以在web项目中也是比较常用的.今天就抽空整理下常用的分享平台的JS代码.这些代码可以在对应平台的官方网站上生成,官网上对分享内容的参数也有详尽说明.这里只对常用的 ...

  7. JS案例之4——Ajax多图上传

    近期项目中有好几次用到多图上传,第一次在项目中真正用到Ajax技术,稍微整理了下,贴个案例出来. 我们传统的做法是当用户提交一个表单时,就向web服务器端发送一个请求.服务器接受并处理传来的表单信息, ...

  8. 《图解tcp/ip》读书笔记(二)

    <图解tcp/ip>读书笔记(二) 本周主要阅读的是本书的第三章--数据链路. 当然了,从某些角度讲,我认为这一章就是计算机网络的最基本的内容之一.整章讲述了数据链路层的作用和相关技术,主 ...

  9. Servlet响应的中文字符集问题

    在Servlet中利用response向客户端浏览器输出中文时有时会遇到乱码问题,总结如下: response输出流有两种,一是以字节流输出,一是以字符流输出. 一.以字节流输出: 1.默认编码输出木 ...

  10. Unknown column '' in 'field list'解决方案

    很多人在用php+MySQL做网站往数据库插入数据时发现如下错误: 注册失败!Unknown column '1a' in 'field list' 结果发现用数字提交是没有问题的,其他如char型就 ...