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. WebSocket 反爬虫

    目录 WebSocket握手验证反爬虫 WebSocket 消息校验反爬虫 WebSocket Ping 反爬虫 总结 WebSocket握手验证反爬虫 ! HTTP协议 请求头 服务器端创建 soc ...

  2. Linux centosVMware shell脚本中的逻辑判断、文件目录属性判断、if特殊用法、case判断

    一.shell脚本中的逻辑判断 格式1:if 条件 ; then 语句; fi 格式2:if 条件; then 语句; else 语句; fi 格式3:if …; then … ;elif …; th ...

  3. luogu P2765 魔术球问题

    题目中没有说球的上限是多少,只告诉了柱子,那么我们就应该以柱子为界去增加球,考虑将每两个能组成完全平方数的点连边,就形成了一个DAG(有向无环图),由于是DAG,可以转换为最小覆盖问题,即最多有n条路 ...

  4. 关于fpga的后仿真重要性

    也许你天天做些fpga,写完代码就直接编译成功,锁定引脚后,马上使用signaltaII软件. 也许你一天,你发现signaltapII看信号的痛苦,一个源代码文件修改一点,要花个20分钟编译一次. ...

  5. css限制文字显示字数长度,超出部分自动用省略号显示,防止溢出到第二行

    为了保证页面的整洁美观,在很多的时候,我们常需要隐藏超出长度的文字.这在列表条目,题目,名称等地方常用到. 效果如下: 未限制显示长度,如果超出了会溢出到第二行里.严重影响用户体验和显示效果. 我们在 ...

  6. when_did_you_born-瞟来的wp

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

  7. MySQL学习之SQL基础(一)DML

    DML(data Manipulation language) INSERT DELETE UPDATE SELECT INSERT mysql> desc emp; +----------+- ...

  8. hue中访问impala报错

    hue中访问impala报错:Could not connect to node03:21050 原因:impala服务未启动. 解决方法:在3个节点上都启动impala: 主节点node03启动以下 ...

  9. Swift Access Levels (变量关键字访问权限)

  10. vue - 子组件向父组件 传递方法和参数

    1,子组件 TodoItem.vue  : <template>   <div class="todo-item" :class="{'is-compl ...