Nearest Common Ancestors
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 14698   Accepted: 7839

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
题目大意:求树节点的最近公共祖先。
解题方法:这种题解题方法很多,我在这里用的是回溯,直接从要查找的节点不断的找父节点。
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string.h>
using namespace std; typedef struct
{
int parent;
bool bvisted;
}UFSTree; UFSTree Tree[]; void MakeSet(int n)
{
for (int i = ; i <= n; i++)
{
Tree[i].parent = i;
Tree[i].bvisted = false;
}
} void LCA(int x, int y)
{
Tree[x].bvisted = true;
x = Tree[x].parent;
while(Tree[x].parent != x)
{
Tree[x].bvisted = true;
x = Tree[x].parent;
}
while(Tree[y].parent != y)
{
if (Tree[y].bvisted == true)
{
break;
}
y = Tree[y].parent;
}
printf("%d\n", y);
} int main()
{
int n, nCcase, father, son, x, y;
scanf("%d", &nCcase);
while(nCcase--)
{
scanf("%d", &n);
MakeSet(n);
for (int i = ; i < n; i++)
{
scanf("%d%d", &father, &son);
Tree[son].parent = father;
}
scanf("%d%d", &x, &y);
LCA(x, y);
}
}

POJ 1330 Nearest Common Ancestors的更多相关文章

  1. POJ - 1330 Nearest Common Ancestors(基础LCA)

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

  2. POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA)

    POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA) Description A ...

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

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

  4. LCA POJ 1330 Nearest Common Ancestors

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

  5. POJ 1330 Nearest Common Ancestors(lca)

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

  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模板题】

    任意门:http://poj.org/problem?id=1330 Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000 ...

  8. POJ 1330 Nearest Common Ancestors (LCA,dfs+ST在线算法)

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

  9. POJ 1330 Nearest Common Ancestors(Targin求LCA)

    传送门 Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26612   Ac ...

  10. [最近公共祖先] POJ 1330 Nearest Common Ancestors

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

随机推荐

  1. C#移除HTML标记

    移除一段文字中的HTML标记,以消除其中包含的样式和段落等,最常用的办法可能就是正则表达式了.但是请注意,正则表达式并不能处理所有的HTML文档,所以有时采用一个迭代的方式会更好,如for循环.看下面 ...

  2. centos网卡错误Device eth0 does not seem to be present

    在使用vmware及VirtualBox迁移linux系统过程中,发现部署后的linux系统无法启动网卡 报错为 Bringing up interface eth0: Device eth0 doe ...

  3. JavaScript 语句 while

    while 语句用法 与for语句的用法之间的关系 for(i==1;i<5;i++) {document.write("12378<br />")  } 若使用 ...

  4. HTML之总结

    基础结构 基础标签有HTML,head,body. 合格的的HTML页面: <!DOCTYPE html> <html lang="en"> <hea ...

  5. loghelper.cs 代码

    唉,网上到处找一圈,真是麻烦,自己结合别人写的,重新整理一个. 这个破玩意最大的作用就是写微信那种没法顺利断点调试的程序的时候,在需要的地方写日志,然后去查看.真是回到当年用DW4写php的年代了,可 ...

  6. AndroidStudio的一些坑

    以下环境为Android Studio 1.3.2,Gradle 2.7(as自带2.4,另下载的2.7) 编译时提示Multiple dex files define: Lcom/sina/weib ...

  7. 聊天IM的时间戳显示规则

    ====================================================== 以下规则是在体验微信操作后,推测出来的规则,可能存在一些不准确的表述 ========== ...

  8. C#:实现快捷键自定义设置

    代码下载 C#实现快捷键自定义设置 需求 项目开发过程中,需要实现类似有道词典的软件设置中的自定义快捷键功能,如下图所示: 当我们相继按下Ctrl+Alt+M的时候,软件就会自动将快捷键显示在文本框中 ...

  9. 构造函数模式自己定义js对象

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  10. crond: unrecognized service 无crond解决办法

    运行计划任务时:service crond restart提示:crond: unrecognized service安装计划任务:yum -y install vixie-cron 另外附计划任务的 ...