题目链接:http://poj.org/problem?id=1330

Nearest Common Ancestors
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 36918   Accepted: 18495

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

 
题目大意:输入T  T组样例  输入N  N个结点(1-N) 下面N-1行  每行两个数 u v  表示u是v的父亲  第N行表示询问 两个数的最近公共祖先
思路:不多说,完全板子
看代码:
#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
using namespace std;
const int maxn=1e4+;
int N;//节点个数
vector<int>v[maxn];//树
vector<int> query[maxn];
int indeg[maxn];//节点的入度
int fa[maxn],deep[maxn],ancestor[maxn];//父亲 深度 祖先
bool vis[maxn];//是否被检查过
int root;
void Init()
{
for(int i=;i<=N;i++)
{
v[i].clear();
query[i].clear();
indeg[i]=; }
return ;
}
void add_edge(int x,int y)
{
v[x].push_back(y);
indeg[y]++;
return ;
}
void Input_query()
{
int u,v;
scanf("%d%d",&u,&v);
query[u].push_back(v);//注意 两个都要存
query[v].push_back(u);
return ;
}
void Init_set()
{
for(int i=;i<=N;i++)
{
fa[i]=i;
ancestor[i]=i;
deep[i]=;
}
return ;
}
int Find(int x)
{
return fa[x]==x?x:fa[x]=Find(fa[x]);
}
void Union(int u,int v)
{
int du=Find(u);
int dv=Find(v);
if(du>dv)
{
fa[dv]=du;
return ;
}
else
{
fa[du]=dv;
if(deep[du]==deep[dv]) deep[dv]++;
}
return ;
}
void Tarjan(int p)
{
for(int i=;i<v[p].size();i++)//遍历子树
{
Tarjan(v[p][i]);
Union(p,v[p][i]);
ancestor[Find(p)]=p;
}
vis[p]=true;
for(int i=;i<query[p].size();i++)
{
if(vis[query[p][i]])
{
printf("%d\n",ancestor[Find(query[p][i])]);
}
}
return ;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{ scanf("%d",&N);
Init();
for(int i=;i<N;i++)
{
int u,v;
scanf("%d%d",&u,&v);
add_edge(u,v);
}
for(int i=;i<=N;i++)
{
if(indeg[i]==)
{
root=i;
break;
}
}
Input_query();
Init_set();
memset(vis,false,sizeof(vis));
Tarjan(root);
}
return ;
}

Nearest Common Ancestors(LCA板子)的更多相关文章

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

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

  2. POJ 1330 Nearest Common Ancestors LCA题解

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

  3. pku 1330 Nearest Common Ancestors LCA离线

    pku 1330 Nearest Common Ancestors 题目链接: http://poj.org/problem?id=1330 题目大意: 给定一棵树的边关系,注意是有向边,因为这个WA ...

  4. poj 1330 Nearest Common Ancestors lca 在线rmq

    Nearest Common Ancestors Description A rooted tree is a well-known data structure in computer scienc ...

  5. poj 1330 Nearest Common Ancestors LCA

    题目链接:http://poj.org/problem?id=1330 A rooted tree is a well-known data structure in computer science ...

  6. Nearest Common Ancestors(LCA)

    Description A rooted tree is a well-known data structure in computer science and engineering. An exa ...

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

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

  8. POJ 1330 Nearest Common Ancestors(LCA模板)

    给定一棵树求任意两个节点的公共祖先 tarjan离线求LCA思想是,先把所有的查询保存起来,然后dfs一遍树的时候在判断.如果当前节点是要求的两个节点当中的一个,那么再判断另外一个是否已经访问过,如果 ...

  9. POJ 1330 Nearest Common Ancestors (LCA,倍增算法,在线算法)

    /* *********************************************** Author :kuangbin Created Time :2013-9-5 9:45:17 F ...

随机推荐

  1. 总结- H5项目常见问题汇总及解决方案(转)

    H5项目常见问题及注意事项 Meta基础知识: H5页面窗口自动调整到设备宽度,并禁止用户缩放页面 //一.HTML页面结构 <meta name="viewport" co ...

  2. .NET基础 (01).NET基础概念

    .NET基础概念 1 什么是CTS.CLS和CLR2 开发和运行.NET程序需要的最基本环节是什么3 .NET是否支持多编程语言开发4 CLR技术和COM技术的比较5 什么是程序集和应用程序域 1 什 ...

  3. C#中的异步调用及异步设计模式(一)

    近期项目中使用了不少异步操作,关于“异步”做个总结.总结的内容大部分都来自于MSDN,还有一些自己的心得. 关于“异步”的使用可分为:使用层面和类库设计层面,细分如下: 一.使用异步方式调用同步方法( ...

  4. JavaScript - this详解 (二)

    用栗子说this Bug年年有,今年特别多 对于JavaScript这么灵活的语言来说,少了this怎么活! function 函数 this 对于没有实例化的function,我们称之为函数,即没有 ...

  5. TCP三次握手与防火墙规则

    一个(tct)socket连接需要在客户端与服务端开启一个隧道,客户端提供一个端口(new时可指定,也可不指定,随机),服务端的端口和地址一定要指定.在win下,服务端创建监听端口时,防火墙会提示阻止 ...

  6. HTTP 常见异常状态及Delphi IDHTTP 控件处理方式

    以下部分为网上查找,部分为工作中整理 200:请求成功 202:请求被接受,但处理尚未完成 302:请求到的资源在一个不同的URL处临时保存     处理方式:重定向到临时的URL(IDHTTP处理方 ...

  7. 7z文件格式及其源码的分析(四)

    这是7z文件格式及其源码的分析系列的第四篇. 上一篇讲到了7z文件静态结构的尾header部分.这一篇开始,将从7z实际压缩流程开始详细介绍7z文件尾header的详细结构. 一, 第一个概念: co ...

  8. HBase介绍(4)---常用shell命令

    进入hbase shell console$HBASE_HOME/bin/hbase shell如果有kerberos认证,需要事先使用相应的keytab进行一下认证(使用kinit命令),认证成功之 ...

  9. 达梦数据库(DaMeng)如何删除IDENTITY自增属性字段

    今天工作中使用到达梦数据库,要求删除具有IDENTITY自增属性的字段. 直接执行删除:ALTER TABLE <表名> DROP COLUMN <列名> CASCADE; 删 ...

  10. 如何使用OpenGL中的扩展

    如果你在Windows平台下开发OpenGL程序,那么系统中自带的OpenGL库就是1.1的,如果想使用1.2或者更高版本的OpenGL库,那么只能使用OpenGL扩展,在网上关于如何使用OpenGL ...