题目链接: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. 37 有n个人围成一圈,顺序排号,从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号那位.

    题目:有n个人围成一圈,顺序排号,从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号那位. public class _037NumberOff { public st ...

  2. msf、armitage

    msfconsole的命令: msfconsole use module :这个命令允许你开始配置所选择的模块. set optionname module :这个命令允许你为指定的模块配置不同的选项 ...

  3. jmeter监控内存,CPU等方法

    方法1: 使用插件来监控CPU,内存等的使用情况 1.需要的插件准备 JMeterPlugins-Standard-1.4.0.zip , JMeterPlugins-Extras-1.4.0.zip ...

  4. github注册流程

  5. C# 代码 获取桌面路径

    Environment.GetFolderPath(Environment.SpecialFolder.MyComputer); // // 摘要: // 获取由指定枚举标识的系统特殊文件夹的路径. ...

  6. Python一篇学会多进程

    阅读目录 1. Process 2. Lock 3. Semaphore 4. Event 5. Queue 6. Pipe 7. Pool 序. multiprocessing python 中的多 ...

  7. Postman使用手册2——管理收藏

    一.开始使用收藏夹 收藏夹会使你的工作效率更上一层楼 收藏夹可以让单个的request分组在一起,这些request可以被进一步的管理到文件夹来更准确的反应你的API.request也可以在保存到收藏 ...

  8. 追随自己的价值观:用研经理 Anne Diaz 职业探索之路

    『漫谈』系列聚焦了人性脆弱面的价值.每期的对话嘉宾可能是爱彼迎设计团队的成员,也可能来自设计界的其他领域.对话主题都是我们在工作中很少讨论的话题. 这些话题涉及不同方面,比如失败.人生道路.冲突.成长 ...

  9. linux使用rsync、inotify-tools实现多台服务器文件实时同步

    需求:将本地192.168.1.10上的/data/wwwroot目录同步到 1.来源服务器上安装rsync.inotify-tools yum -y install rsync yum -y ins ...

  10. luogu P1080国王游戏

    贪心加高精 传送门:QWQ 先考虑两个人 a0 b0 p1 a1 b1 p2 a2 b2 那么满足:\(\huge ans1=\max(\frac{a0}{b1} , \frac{a0a1}{b2}) ...