LCA POJ 1330 Nearest Common Ancestors
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 24209 | Accepted: 12604 |
Description
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
Output
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
#define N 10100
#include<iostream>
using namespace std;
#include<cstdio>
#include<cstring>
struct Edge{
int v,last;
}edge[N*];
bool visit[N],root[N];
int father[N],ance[N];
int T,n,head[N];
int x,y;
void add_edge(int u,int v,int k)
{
edge[k].v=v;
edge[k].last=head[u];
head[u]=k;
}
void input()
{
memset(root,false,sizeof(root));
memset(edge,,sizeof(edge));
memset(head,,sizeof(head));/*注意多组数据之间的衔接,把数据都清空了*/
scanf("%d",&n);
for(int i=;i<n;++i)
{
int u,v;
scanf("%d%d",&u,&v);
add_edge(u,v,i);
father[i]=i;
ance[i]=;
root[v]=true;
visit[i]=false;
}
scanf("%d%d",&x,&y);
father[n]=n;
ance[n]=;
visit[n]=false; }
int find(int k)
{
return (father[k]==k)?father[k]:father[k]=find(father[k]);
}
void tarjan(int k)
{
ance[k]=k;
for(int l=head[k];l;l=edge[l].last)
{
tarjan(edge[l].v);
father[edge[l].v]=k;
ance[edge[l].v]=k;
}
visit[k]=true;
if(k==x&&visit[y])
{
printf("%d\n",ance[find(y)]);
}
if(k==y&&visit[x])
{
printf("%d\n",ance[find(x)]);
}
}
int main()
{
scanf("%d",&T);
while(T--)
{
input();
for(int i=;i<=n;++i)
if(!root[i])
{
tarjan(i);
break;
}
}
return ;
}
LCA POJ 1330 Nearest Common Ancestors的更多相关文章
- POJ - 1330 Nearest Common Ancestors(基础LCA)
POJ - 1330 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %l ...
- POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA)
POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA) Description A ...
- POJ.1330 Nearest Common Ancestors (LCA 倍增)
POJ.1330 Nearest Common Ancestors (LCA 倍增) 题意分析 给出一棵树,树上有n个点(n-1)条边,n-1个父子的边的关系a-b.接下来给出xy,求出xy的lca节 ...
- POJ 1330 Nearest Common Ancestors(lca)
POJ 1330 Nearest Common Ancestors A rooted tree is a well-known data structure in computer science a ...
- POJ 1330 Nearest Common Ancestors 倍增算法的LCA
POJ 1330 Nearest Common Ancestors 题意:最近公共祖先的裸题 思路:LCA和ST我们已经很熟悉了,但是这里的f[i][j]却有相似却又不同的含义.f[i][j]表示i节 ...
- POJ 1330 Nearest Common Ancestors 【LCA模板题】
任意门:http://poj.org/problem?id=1330 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000 ...
- POJ 1330 Nearest Common Ancestors (LCA,dfs+ST在线算法)
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14902 Accept ...
- POJ 1330 Nearest Common Ancestors(Targin求LCA)
传送门 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26612 Ac ...
- POJ 1330 Nearest Common Ancestors LCA题解
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 19728 Accept ...
随机推荐
- javascript复习笔记
/* Javascript:用来在页面中编写特效,和HTML.CSS一样都是有浏览器解析 Javascript语言: 一.JS如何运行(javascript,jscript,vbscript,appl ...
- 面向过程编程(OPP) 和面向对象编程(OOP)的关系
面向过程编程(OPP) 和面向对象编程(OOP)的关系 原文链接:http://blog.csdn.net/phphot/article/details/3985480 关于面向过程的编程(OPP)和 ...
- 导航狗IT周报第十五期(July 8, 2018)
摘要:Seclists.Org: 微信支付SDK存在XXE漏洞:WordPress 4.9.6存在文件删除漏洞:linux中常用的文件打包/解包与压缩/解压缩命令总结… 安全播报 Seclists.O ...
- 如何把一篇Word文档里的所有换行符去掉?
编辑-查找,查找框输入 ^13替换框不输入点击全部替换
- vsftpd 安装配置详细教程
linux下ftp软件不少,大致特点:<br /> wu-ftp:比较老牌,但针对它的攻击比较多,设置比较麻烦,但功能比较强大.<br /> vsftpd:功能强大,配置也比较 ...
- MongoDB之数据库命令操作(二)
现在详细学习一下mongodb的数据库操作. 查询语句 db.xxx(集合name).find() # 查询 db.xxx(集合name).findOne() # 只返回一个 db.xxx(集合nam ...
- 安装ssh-keygen
转载自:http://www.daoan.com/forums/index.php?forumid=5&mods=topicdisplay&postid=4 sudo apt-get ...
- HZ与Jiffies
2.4 内核定时器 内核中许多部分的工作都高度依赖于时间信息.Linux内核利用硬件提供的不同的定时器以支持忙等待或睡眠等待等时间相关的服务.忙等待时,CPU 会不断运转.但是睡眠等待时,进程将放弃C ...
- javascript反混淆之packed混淆(一)
javascript反混淆之packed混淆(一) 什么是JavaScript反混淆,在理解这个概念前我们先来看下什么是代码混淆,代码混淆,是将计算机程序的代码,转换成一种功能上等价,但是难于阅读和理 ...
- java获取weblogic应用运行路径
String url = TemplateBuilder(当前类).class.getClassLoader().getResource("").getPath(); String ...