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. java并发之CopyOnWirteArrayList

    java并发之CopyOnWirteArrayList CopyOnWirteArrayList的实现 它用了ReentrantLock保证了add,set,remove操作的安全,同时使用volat ...

  2. scikit_learn (sklearn)库中NearestNeighbors(最近邻)函数的各参数说明

    NearestNeighbors(n_neighbors=5, radius=1.0, algorithm='auto', leaf_size=30, metric='minkowski', p=2, ...

  3. 如何关闭 Mac OS X EI Capitan 系统文件保护

    在修改 Mac OS X EI Capitan 系统的某些系统文件的过程中,你可能会遇到即使你加了 sudo 以管理员身份去操作页会被拒绝, 这是一个名为 SIP(System Integrity P ...

  4. 网站Webshell大马密码极速暴力爆破工具-cheetah

    Cheetah是一个基于字典的暴力密码webshel​​l工具,运行速度与猎豹猎物一样快. Cheetah的工作原理是能根据自动探测出的web服务设置相关参数一次性提交大量的探测密码进行爆破,爆破效率 ...

  5. Ubuntu18.04 ElasticSearch7.3.2集群搭建(一)

    集群规划: Hostname Elasticsearch Kibana Marvel Marvel Client node01 √ √ √ √ node02 √ √ node03 √ √ node04 ...

  6. Jquery选择器大全汇总

    一.选择器 1.三个基本选择器,$("#ID") .$(".className")  .$("tagName") 2.其他选择器 //htm ...

  7. Linux学习计划(一)

    一.用途:网络服务器 二.优点: 1.开源免费 2.良好的可移植性 3.安全性 三.安装Linux 工具:VMware workstation .centOS7 安装步骤 图片加载中... 说明: Ⅰ ...

  8. 【转载】Jmeter关联-正则表达式提取器

            今天研发同事提供了一个验证token的接口,要验证token的正确性,现在将整个过程做如下记录: 场景:验证token的正确性 原理:首先用户登录成功后,会在Response head ...

  9. call 和 apply 用法

    ECMAScript规范中,所有函数都包含这两个方法,并且两个方法的使用基本一致,都是用于改变函数的作用域,即改变函数体内 this 指向.不同的是 call 方法的第二个参数可以接收任意个参数,以逗 ...

  10. 学习进度-10 python爬虫

    学习爬虫的第一个案例是小说爬虫. 小说爬虫首先是解析小说页面源代码,在页面源代码中可以看到小说每章节的内容链接 爬虫的代码: import requests import re url = 'http ...