Nearest Common Ancestors

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 37386   Accepted: 18694

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

题意:输入t代表有多个测试样例,每个样例第一行输入一个数n,表示有n个节点,接下来n-1行描述这n个节点的关系,第n行输入x,y要求x,y的最近公共祖先
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int N = 1e4 + ;
vector<int> ve[N];//ve[]是用来建表的一个数组
vector<int> que[N];//que[]是用来查询的一个数组
int ans, pre[N], vis[N];//pre[]是节点编号
int t, n;
int find(int x)//查找公共祖先
{
return pre[x] == x ? x : find(pre[x]);//距离x最近的一个没有更新父节点的点(pre[x]=x),就是最近的祖先节点
}
void init()
{
for (int i = ; i <= n; i++)
{
pre[i] = i;//初始化所有节点的父节点为它本身
vis[i] = ;
ve[i].clear();
que[i].clear();
}
} void dfs(int u, int fa)
{
vis[u] = ;//标记表示查询过
for (int i = ; i<ve[u].size(); i++)//借助并查集,在DFS过程中,我们每到达一个节点u,便创建一棵以u为根结点的子树,ve[u].size()就是这个节点子节点的数目
{
int v = ve[u][i];
dfs(v, u);//继续以v为子节点,u为根节点往下遍历到底
}
for (int j = ; j<que[u].size(); j++)//反向遍历,更新遍历过节点的父节点
{
int v = que[u][j];
if (vis[v] == )
{
ans = find(v);
}
}
pre[u] = fa;//更新父节点
} int main()
{
scanf("%d", &t);
while (t--)
{
scanf("%d", &n);//是节点数目
init();//初始化
int x, y;
for (int i = ; i<n - ; i++) //描述父子关系,建表
{
scanf("%d %d", &x, &y);
ve[x].push_back(y);//父子结点关系,X是父节点,y是子节点
vis[y] = ;//标记所有子节点,只有最顶上的根节点没有做过子节点才不会被标记
}
scanf("%d %d", &x, &y);
que[x].push_back(y);//查询
que[y].push_back(x);
for (int i = ; i <= n; i++)
{
if (vis[i] == )
{
memset(vis, , sizeof(vis));
dfs(i, -);//从根节点开始,因为根节点没有父节点,所以初始为-1
break;
}
}
printf("%d\n", ans);
}
return ;
}

poj 1330 Nearest Common Ancestors 求最近祖先节点的更多相关文章

  1. POJ 1330 Nearest Common Ancestors(求最近的公共祖先)

    题意:给出一棵树,再给出两个节点a.b,求离它们最近的公共祖先.方法一: 先用vector存储某节点的子节点,fa数组存储某节点的父节点,最后找出fa[root]=0的根节点root.      之后 ...

  2. POJ - 1330 Nearest Common Ancestors 最近公共祖先+链式前向星 模板题

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

  3. POJ 1330 Nearest Common Ancestors (最近公共祖先LCA + 详解博客)

    LCA问题的tarjan解法模板 LCA问题 详细 1.二叉搜索树上找两个节点LCA public int query(Node t, Node u, Node v) { int left = u.v ...

  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 倍增) 题意分析 给出一棵树,树上有n个点(n-1)条边,n-1个父子的边的关系a-b.接下来给出xy,求出xy的lca节 ...

  7. POJ 1330 Nearest Common Ancestors 倍增算法的LCA

    POJ 1330 Nearest Common Ancestors 题意:最近公共祖先的裸题 思路:LCA和ST我们已经很熟悉了,但是这里的f[i][j]却有相似却又不同的含义.f[i][j]表示i节 ...

  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. request-html

    目录 基本使用 获取链接( links 与 absolute_links ) CSS 选择器与 XPATH 支持 JavaScript 自定义 User-Agent 模拟表单提交(POST) asyn ...

  2. Linux centosVMware su命令、sudo命令、限制root远程登录

    一.su命令 Linux系统中有些事情只有root用户才能做,普通用户不能做,这时候就需要临时切换到root身份了. [root@davery ~]# whoamiroot [root@davery ...

  3. Mybatis入门(四)配置优化(一)

    这一章主要实验Mybatis的引入外部配置文件,属性(properties)这个属性都是可外部配置且可动态替换的,既可以在典型的 Java 属性文件中配置,亦可通过 properties 元素的子元素 ...

  4. 自定义sort排序

    java的sort自定义: 1.排序对象必须是封装类而不能是基本数据类型: 2.调用Arrays.sort(array, left, right, cmp)进行排序,array为数组,left.rig ...

  5. tcp/ip协议学习笔记一

    一. 简述 以前在学校学习计算机网络的时候学习多是网络7层模型OSI,了解了一些基本的计算机网络概念和协议通信格式,但是一直没弄明白其中的原理,包括各层之间的关系,应用,还有一些常见的令牌环网到底是什 ...

  6. 多实例mysql的安装和管理

    多实例mysql的安装和管理 http://blog.chinaunix.net/uid-20639775-id-3438560.html mysql的多实例有两种方式可以实现,两种方式各有利弊.第一 ...

  7. Visual c++6.0如何创建工程项目

    首先,打开电脑里安装的visual C++ 6.0 ,进入其选择的主界面,如果兼容性不对,先调整一下电脑与软件的兼容性. 进入主界面之后,点击左上角的文件,然后点击新建,可以创建新的文件. 打开后弹出 ...

  8. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 辅助类:"text-muted" 类的文本样式

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

  9. YUV图解 (YUV444, YUV422, YUV420, YV12, NV12, NV21)

    背景: 最近在研究音视频,了解YUV这样的格式对于音视频开发比较重要. 虽然这篇文章大部分是转载别人的,但是经过了校对以后,重新排版并补充了一部分内容   概览: 之所以提出yuv格式的原因,是为了解 ...

  10. when_did_you_born-瞟来的wp

    继上文,这次开始嫖when_did_you_born这题.前面的步骤大致是一样的就不赘述了,直接到代码分析. 字符串 这次呢在main函数处 按下F5进入调试 查看反汇编代码 可以清楚的看到它的逻辑一 ...