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. QTP基础学习(一)安装目录介绍

    上一篇介绍了QTP 10 安装,安装完成后就可以看到文件的目录了,这里主要介绍以下几个目录及作用. 简单介绍部分目录 1.addins:插件包 2.bin目录:可执行程序,这里存储了很多配置文件.运行 ...

  2. AE二次开发技巧之撤销、重做

    原文地址:http://www.cnblogs.com/wylaok/articles/2363208.html 可以把AE自带的重做.撤销按钮或工具添加到axToolBarControl上,再把ax ...

  3. ArcGis 统计方法

    from:http://blog.sina.com.cn/s/blog_4177d50b0100fjbg.html 概述 一般常用的统计功能例如:唯一字段统计.数据行数统计.数据值求和统计等. 1.基 ...

  4. ftp虚拟账号登陆

    配置使用虚拟用户登录的FTP服务器,可以避免使用操作系统帐号作为FTP用户带来的一些安全问题,也便于通过数据库或其它程序来进行管理.废话不多说,这里记录下ftp虚拟账号登陆的部署过程及其中遇到的问题: ...

  5. jquery中的get和set

    jquery中通过参数的个数来判断是get方法还是set方法: css: function(name, value ) { return value !== undefined ? jQuery.st ...

  6. Datagrid数据导出到excel文件的三种方法

    原文连接: http://www.cnblogs.com/xieduo/articles/606202.html 一.文件保存在服务器,提供下载 方法一:导出到csv文件,存放在服务器端任一路径,然后 ...

  7. 【原创】有关Silverlight DataGrid双击事件的分析 完整分析 代码与示例

    公司项目用的silverlight,而且silverlight一些技术 资料比较少.所以分享出来 给大家参考参考. 有关Silverlight中DataGrid 双击事件 的代码 如下: 1. 前台x ...

  8. C语言 文件操作12--文件加密

    //文件加密解密 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include ...

  9. 卫星轨道和两行数据TLE

    最近由于Sino-2和北斗的关系,很多网友贴了表示卫星运行轨道的TLE数据.这里想对卫星轨道参数和TLE的格式做一个简单介绍.虽然实际上没有人直接读TLE数据,而都是借助软件来获得卫星轨道和位置信息, ...

  10. Caffe学习系列(8):solver优化方法

    上文提到,到目前为止,caffe总共提供了六种优化方法: Stochastic Gradient Descent (type: "SGD"), AdaDelta (type: &q ...