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
 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<deque>
#include<map>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0);
const double e=exp();
const int N = ; #define lson i << 1,l,m
#define rson i << 1 | 1,m + 1,r
int cnt,ans;
int a,b,n;
int root;
int head[N];
int is_root[N];
int father[N];
int vis[N]; struct edge
{
int to;
int next;
} edge[N]; int seek(int ss)
{
int mid;
int head=ss;
while(ss!=father[ss])
ss=father[ss]; while(head!=ss)
{
mid=father[head];
father[head]=ss;
head=mid;
}
return ss;
} void join(int xx,int yy)
{
int one=seek(xx);
int two=seek(yy);
if(one!=two)
father[two]=one; //注意把谁变成谁的上级
} void add(int x,int y)
{
edge[cnt].to=y;
edge[cnt].next=head[x];
head[x]=cnt++;
} void init()
{
int i,p,j;
int x,y;
cnt=;
memset(head,-,sizeof(head));
memset(is_root,,sizeof(is_root));
memset(vis,,sizeof(vis));
scanf("%d",&n);
for(i=; i<=n; i++)
father[i]=i;
for(i=; i<n; i++)
{
scanf("%d%d",&x,&y);
add(x,y);
is_root[y]=;
}
for(i=; i<=n; i++)
if(is_root[i]==)
root=i;
} void LCA(int u)
{
int i,p,j;
for(i=head[u]; i!=-; i=edge[i].next)
{
int v=edge[i].to;
LCA(v);
join(u,v);
vis[v]=;
} if(u==a&&vis[b]==)
ans=seek(b);
if(u==b&&vis[a]==)
ans=seek(a); return ;
} void solve()
{
scanf("%d%d",&a,&b);
LCA(root);
} int main()
{
int t,m,i,p,j;
scanf("%d",&t);
for(i=; i<=t; i++)
{
init();
solve(); printf("%d\n",ans);
}
return ;
}

POJ - 1330 Nearest Common Ancestors 最近公共祖先+链式前向星 模板题的更多相关文章

  1. POJ 1330 Nearest Common Ancestors (最近公共祖先LCA + 详解博客)

    LCA问题的tarjan解法模板 LCA问题 详细 1.二叉搜索树上找两个节点LCA public int query(Node t, Node u, Node v) { int left = u.v ...

  2. 【POJ】1330 Nearest Common Ancestors ——最近公共祖先(LCA)

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

  3. poj 1330 Nearest Common Ancestors 求最近祖先节点

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

  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)

    POJ - 1330 Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000KB   64bit IO Format: %l ...

  6. POJ 1330 Nearest Common Ancestors 倍增算法的LCA

    POJ 1330 Nearest Common Ancestors 题意:最近公共祖先的裸题 思路:LCA和ST我们已经很熟悉了,但是这里的f[i][j]却有相似却又不同的含义.f[i][j]表示i节 ...

  7. POJ.1330 Nearest Common Ancestors (LCA 倍增)

    POJ.1330 Nearest Common Ancestors (LCA 倍增) 题意分析 给出一棵树,树上有n个点(n-1)条边,n-1个父子的边的关系a-b.接下来给出xy,求出xy的lca节 ...

  8. LCA POJ 1330 Nearest Common Ancestors

    POJ 1330 Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24209 ...

  9. POJ 1330 Nearest Common Ancestors(lca)

    POJ 1330 Nearest Common Ancestors A rooted tree is a well-known data structure in computer science a ...

随机推荐

  1. 201621123037 《Java程序设计》第1周学习总结

    #作业01-Java基本概念 1. 本周学习总结 本周学习内容JDK JVM JRE 跨平台 .java .class 关键概念之间的联系: 总的来讲JDK是给开发人员们提供专门用来开发的环境,并且包 ...

  2. psp 第二周

    11号                                                                              12号 类别c 内容c 开始时间s 结 ...

  3. (转)Elasticsearch search-guard 插件部署

    我之前写了ELK+shield的部署文档,由于shield是商业收费的,很多人都推崇开源项目search-guard来做ELK的安全组件,准确来说是elasticsearch的安全组件.search- ...

  4. apache 部署web.py

    一.安装Mod_wsgi 1.先yum -y install httpd-devel,否则会提示没有apxs 2.如果在make时 wsgi报错apxs:Error: Command failed w ...

  5. excel表中判断A列与B列内容是否相同,相同的话在C列按条件输出!

    判断两列数据是否相同,有以下几个函数判断(做笔记于此,方便以后查找): 1.=IF(AND(A4=B4),"相同","") 在C列输出相同字符 2.=IF(A1 ...

  6. 九度-题目1203:IP地址

    http://ac.jobdu.com/problem.php?pid=1203 题目描述: 输入一个ip地址串,判断是否合法. 输入: 输入的第一行包括一个整数n(1<=n<=500), ...

  7. c#对一个类的扩展

    首先定义一个静态类,参数使用this约束并选择需要扩展的类,当然也可以 继续添加扩展是需要添加的参数 public static class StringExrprp { /// <summar ...

  8. 【Django】Django迁移数据库

    我们已经编写了博客数据库模型的代码,但那还只是 Python 代码而已,Django 还没有把它翻译成数据库语言,因此实际上这些数据库表还没有真正的在数据库中创建 为了让 Django 完成翻译,创建 ...

  9. "strcmp()" Anyone? UVA - 11732(trie出现的次数)

    给你n个单词,让他们两两比较,要求他们运用strcmp时,进行比较的次数. 边建树边统计 #include <iostream> #include <cstdio> #incl ...

  10. 【JQuery】遍历

    一.前言        接着上一章的内容,继续本章的学习. 二.内容 .add 将元素添加到匹配元素的集合中 .add(object) .add(selector,context) .addSelf ...