题目链接: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. MSSQLServer 存储过程

    一直对存储过程懵懵懂懂,翻了资料,觉得存储过程大有用处. 1.改善性能 SQL语句的执行需要先编译在执行,存储过程就是一组为了完成特定功能的SQL语句集,他可以一次编译,下次执行不再编译,提高运行效率 ...

  2. 跟我学习css3之transition

    HTML5和css3已经是将来的发展趋势,现在有很多移动端还有一些游戏公司已然使用它们开 发了比较成功的产品.我在2011年的时候也跟着技术潮流初浅的学习了html5+css3.毕竟那 时候我没有把学 ...

  3. 设计模式22:Strategy 策略模式(行为型模式)

    Strategy 策略模式(行为型模式) 动机(Motivation) 在软件构建过程中,某些对象使用的算法可能多种多样,经常改变,如果将这些算法都编码到对象中,将会使对象变得异常复杂:而且有时候支持 ...

  4. 【小梅哥FPGA进阶教程】第十一章 四通道幅频相可调DDS信号发生器

    十一.四通道幅频相可调DDS信号发生器 本文由山东大学研友袁卓贡献,特此感谢 实验目标 实现多通道可调信号发生器 实验平台 芯航线FPGA核心板.ADDA模块 实验现象 实现基于FPGA的多通道可调信 ...

  5. Java菜鸟之java基础语法,运算符(三)

    赋值运算符 (一)JAVA种的赋值运算符 = ,代表代表的等于,一般的形式是  左边变量名称   =     右边的需要赋的指或者表达式,如果左侧的变量类型级别比较高,就把右侧的数据转换成左侧相同的高 ...

  6. [转发]Oauth 1.0 1.0a 和 2.0 的之间的区别有哪些?

    原文地址:http://www.zhihu.com/question/19851243

  7. 加载log文件

    procedure Jslog(szLogText:string);var pMyFile : TextFile;begin Assignfile(pMyFile, 'log.txt'); if Fi ...

  8. Mvc 项目中使用Bootstrap以及基于bootstrap的 Bootgrid

    官方地址参考http://www.jquery-bootgrid.com/Examples Bootgrid 是一款基于BootStrap 开发的带有查询,分页功能的列表显示组件.可以在像MVC中开发 ...

  9. 数独高阶技巧入门之六——ALS

    ​在这个系列的第一篇(链及其简单应用)以及第四篇(简单异数链)中已经简单介绍过ALS结构的定义,即n格中存在n+1个不同的候选数 (双值格可视为特殊的ALS结构) .根据数独规则,在组成ALS的候选数 ...

  10. Python【读取文件,第一行与最后一行】

    文件小的读取方法 with open("a1.txt","r",encoding="gbk") as f: r = f.readlines( ...