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. 使用命令virsh管理网络设备,创建桥设备 和 使用virt-manager创建虚拟机

    # 1:查看网卡接口 virsh iface-list Name State MAC Address ------------------------------------------------- ...

  2. HTML&CSS实体

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  3. psp 第二周

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

  4. week1词频统计

    使用java完成对txt格式的英文短片进行字符提取及统计. package nenu.softWareProject; import java.io.*;import java.util.*; pub ...

  5. eclipse中jsp页面Invalid location of tag 解决办法分析小结

    在jsp页面使用标签过程中有时候不注意规则的话,eclipse会提示一些错误,下面针对这些错误提出相应的解决办法: <form></form>标签 1. Invalid loc ...

  6. shell 一些符号的使用

    给你个全的,你在Linux环境下多试下就明白了:$0 这个程式的执行名字$n 这个程式的第n个参数值,n=1..9$* 这个程式的所有参数,此选项参数可超过9个.$# 这个程式的参数个数$$ 这个程式 ...

  7. Spring 学习 3- AOP

    什么是AOP aop就是纵向的编程,业务1和业务2都需要一个共同的操作,与其往每个业务中都添加同样的代码,不如写一遍代码,让两个业务共同使用这段代码. spring中面向切面编程用的是代理模式,它的实 ...

  8. ES2015中let的暂时性死区(TDZ)

    Tomporal Dead Zone (TDZ)是ES2015中对作用域新的专用定义.是对于某些遇到在区块作用域绑定早于声明语句时的情况.Tomporal Dead Zone (TDZ)可以理解为时间 ...

  9. 洛谷 P2421 A-B数对(增强版)

    题目描述 给出N 个从小到大排好序的整数,一个差值C,要求在这N个整数中找两个数A 和B,使得A-B=C,问这样的方案有多少种? 例如:N=5,C=2,5 个整数是:2 2 4 8 10.答案是3.具 ...

  10. 洛谷P3938 斐波那契

    题目戳 题目描述 小 C 养了一些很可爱的兔子. 有一天,小 C 突然发现兔子们都是严格按照伟大的数学家斐波那契提出的模型来进行 繁衍:一对兔子从出生后第二个月起,每个月刚开始的时候都会产下一对小兔子 ...