poj 1330 Nearest Common Ancestors LCA
题目链接:http://poj.org/problem?id=1330
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.
题意描述:在一个DAG中,定义节点u是节点v的祖先:节点u是树根到节点v的路径上的一个节点。 给出一些节点之间的关系,求出两个节点的最近公共祖先。
算法分析:最近公共祖先(LCA)的入门题。
最近公共祖先算法的大致思路:
1:求出每个节点的2^k(0<=k<max_log_n)的祖先节点。节点u的2^0(第一代)祖先节点就是u的父亲节点,那么我们可以得到u的第一代、第二代、第四代、第八代...祖先节点。
2:把节点u和v深度大的节点根据1中的算法思想移到和深度小的节点同一深度(树根深度为0,树根的儿子节点深度为1),然后再一起往上移,即可求出LCA。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<vector>
#define inf 0x7fffffff
using namespace std;
const int maxn=+;
const int max_log_maxn=; int n,A,B,root;
vector<int> G[maxn];
int father[max_log_maxn][maxn],d[maxn]; void dfs(int u,int p,int depth)
{
father[][u]=p;
d[u]=depth;
int num=G[u].size();
for (int i= ;i<num ;i++)
{
int v=G[u][i];
if (v != father[][u]) dfs(v,u,depth+);
}
} void init()
{
dfs(root,-,);
for (int k= ;k+<max_log_maxn ;k++)
{
for (int i= ;i<=n ;i++)
{
if (father[k][i]<) father[k+][i]=-;
else father[k+][i]=father[k][father[k][i] ];
}
}
} int LCA()
{
if (d[A]<d[B]) swap(A,B);
for (int k= ;k<max_log_maxn ;k++)
{
if ((d[A]-d[B])>>k & )
{
A=father[k][A];
}
}
if (A==B) return A;
for (int k=max_log_maxn- ;k>= ;k--)
{
if (father[k][A] != father[k][B])
{
A=father[k][A];
B=father[k][B];
}
}
return father[][A];
} int main()
{
int t;
scanf("%d",&t);
while (t--)
{
scanf("%d",&n);
for (int i= ;i<=n ;i++) G[i].clear();
for (int i= ;i<max_log_maxn ;i++)
{
for (int j= ;j<maxn ;j++)
father[i][j]=-;
}
int a,b;
int vis[maxn];
memset(vis,,sizeof(vis));
for (int i= ;i<n- ;i++)
{
scanf("%d%d",&a,&b);
G[a].push_back(b);
vis[b]=;
}
scanf("%d%d",&A,&B);
for (int i= ;i<=n ;i++) if (!vis[i]) {root=i;break; }
init();
printf("%d\n",LCA());
}
return ;
}
poj 1330 Nearest Common Ancestors LCA的更多相关文章
- 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题解
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 19728 Accept ...
- poj 1330 Nearest Common Ancestors lca 在线rmq
Nearest Common Ancestors Description A rooted tree is a well-known data structure in computer scienc ...
- POJ 1330 Nearest Common Ancestors (LCA,倍增算法,在线算法)
/* *********************************************** Author :kuangbin Created Time :2013-9-5 9:45:17 F ...
- POJ 1330 Nearest Common Ancestors(LCA模板)
给定一棵树求任意两个节点的公共祖先 tarjan离线求LCA思想是,先把所有的查询保存起来,然后dfs一遍树的时候在判断.如果当前节点是要求的两个节点当中的一个,那么再判断另外一个是否已经访问过,如果 ...
- 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 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节 ...
随机推荐
- DataGridView 控件用法(可能不是很全面,因为这是自己常常用到的一些小总结):
一.DataGridView属性设置 1.我们单击选中行的时候,默认是选择一个单元格,不能选择一整行,我们只需设置DataGridView的属性SelectionMode为FullRowSelect ...
- C#操作xml
最进在做一个项目,需要用到xml做配置文件,但是自己忘的差不多了,所以就温习了一遍.以下是我项目中所用到的,所以肯定也不全面. 1.新建xml文件 using System.Xml;//需要引用 st ...
- centos6.7下编译安装lamp环境
编译C源代码: 前提:提供开发工具及开发环境 通过“包组”提供开发组件,CentOS 6: "Development Tools", "Server Platform D ...
- js一些实用例子
1.获取焦点选中文本内容 $("#id").focus(function(){ this.select(); }); 2.表单提交方式 A.自动提交 setTimeout(func ...
- Thinkphp 获取当前url
$_GET['_URL_'] 获取整个url,返回值是数组 $Think.MODULE_NAME 获取当前class的名称 $Think.ACTION_NAME 获取当前action的方法名称
- 通过URLHttpConnection方式来取得图片,并且显示在ImageView上
界面: 代码xml: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xml ...
- SHOW SLAVE STATUS几个常见参数
--显示当前读取的Master节点二进制日志文件和文件位置,对应线程I/O thread Master_Log_File: mysql-bin.000011 Read_Master_Log_Pos: ...
- ASP.NET Core文章汇总
现有Asp.Net Core 文章资料,2016 3-20月汇总如下 ASP.NET Core 1.0 与 .NET Core 1.0 基础概述 http://www.cnblogs.com/Irvi ...
- opensuse 安装 Anaconda3 之后出现Could not start d-bus. Can you call qdbus?
最近在安装了opensue Leap42.1之后,想要学习一下python,就安装了Anaconda3,并且将Anaconda3的安装路径添加到了PATH里,但是在重新启动系统后,出现了"C ...
- hudson项目中的运用
项目中持续集成管理一直是用的hudson,最近的话,hudson遇到不少问题,因为之前对这个也不是很熟悉,所以也花了比较多的时间去解决,现在刚好也可以总结下自己学习到的hudson知识. 首先在我看来 ...