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. JMeter学习(三十一)Access Log Sampler

    前提: 在tomcat\conf\server.xml默认情况下,会有一段代码: <Valve className="org.apache.catalina.valves.Access ...

  2. java7-4 继承的练习

    1.Override和Overload的区别,Overload能改变返回值类型吗? 答: Override就是方法重写:在子类中,出现和父类中一模一样的方法声明的现象 Overload就是方法重载:在 ...

  3. [转] Linux下防火墙iptables用法规则详及其防火墙配置

    from: http://www.cnblogs.com/yi-meng/p/3213925.html 备注: 排版还不错,建议看以上的链接. iptables规则 规则--顾名思义就是规矩和原则,和 ...

  4. HTML5和css3的总结二

    继续接着昨天的整理 [倒影]:用的不是很多 -webkit-box-reflect:below 20px -webkit-linear-gradient(rgba(0,0,0,0,),rgba(0,0 ...

  5. PYTHON FABRIC实现远程操作和部署

    转载至:http://wklken.me/posts/2013/03/25/python-tool-fabric.html fabric title是开发,但是同时要干开发测试还有运维的活 (o(╯□ ...

  6. m3u8字段意义解析

    m3u8字段意义解析 HLS,Http Live Streaming是由Apple公司定义的用于实时流传输的协议,HLS基于HTTP协议实现,传输内容包括两部分,一是M3U8描述文件,二是TS媒体文件 ...

  7. [CareerCup] 5.8 Draw Horizonatal Line 画横线

    5.8 A monochrome screen is stored as a single array of bytes, allowing eight consecutive pixels to b ...

  8. Spring MVC的工作流程

    前端控制器(DispatcherServlet): (不需要我们开发)接收请求,响应结果,相当于转发器,中央处理器.减少了其它组件之间的耦合度. springmvc.xml是SpringMVC的一个全 ...

  9. IT男的”幸福”生活

    IT男的”幸福”生活 IT男的”幸福”生活"续1 IT男的”幸福”生活"续2  IT男的”幸福”生活"续3  IT男的”幸福”生活"续4  IT男的”幸福”生活 ...

  10. 通过WMI - Win32_Processor - ProcessorId获取到的并不是CPU的序列号,也并不唯一

    现在网上不少教程,教人通过WMI - Win32_Processor - ProcessorId来获取CPU的“序列号”,典型代码如下: public static string GetCPUSeri ...