poj 1330 Nearest Common Ancestors lca 在线rmq
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
题意:n组数据,y-1条边,最后一个求lca;
博客:http://blog.csdn.net/barry283049/article/details/45842247;我的代码思路根据最后的在线算法得出;
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define mod 1000000007
#define inf 999999999
//#pragma comment(linker, "/STACK:102400000,102400000")
int scan()
{
int res = , ch ;
while( !( ( ch = getchar() ) >= '' && ch <= '' ) )
{
if( ch == EOF ) return << ;
}
res = ch - '' ;
while( ( ch = getchar() ) >= '' && ch <= '' )
res = res * + ( ch - '' ) ;
return res ;
}
struct is
{
int u,v;
int next;
}edge[];
int head[];
int deep[];
int rudu[];
int first[];
int dfn[];//存深搜的数组
int dp[][];
int point,jiedge;
int minn(int x,int y)
{
return deep[x]<=deep[y]?x:y;
}
void update(int u,int v)
{
jiedge++;
edge[jiedge].u=u;
edge[jiedge].v=v;
edge[jiedge].next=head[u];
head[u]=jiedge;
}
void dfs(int u,int step)
{
dfn[++point]=u;
deep[point]=step;
if(!first[u])
first[u]=point;
for(int i=head[u];i;i=edge[i].next)
{
int v=edge[i].v;
dfs(v,step+);
dfn[++point]=u;
deep[point]=step;
}
}
void st(int len)
{
for(int i=;i<=len;i++)
dp[i][]=i;
for(int j=;(<<j)<=len;j++)
for(int i=;i+(<<j)-<=len;i++)
{
dp[i][j]=minn(dp[i][j-],dp[i+(<<(j-))][j-]);
}
}
int query(int l,int r)
{
int lll=first[l];
int rr=first[r];
if(lll>rr) swap(lll,rr);
int x=(int)(log((double)(rr-lll+))/log(2.0));
return dfn[minn(dp[lll][x],dp[rr-(<<x)+][x])];
}
int main()
{
int x,y,z,i,t;
scanf("%d",&x);
while(x--)
{
memset(head,,sizeof(head));
memset(rudu,,sizeof(rudu));
memset(first,,sizeof(first));
point=;
jiedge=;
scanf("%d",&y);
for(i=;i<y;i++)
{
int u,v;
scanf("%d%d",&u,&v);
update(u,v);
rudu[v]++;
}
for(i=;i<=y;i++)
if(!rudu[i])
{
dfs(i,);
break;
}
st(point);
int u,v;
scanf("%d%d",&u,&v);
printf("%d\n",query(u,v));
}
return ;
}
poj 1330 Nearest Common Ancestors lca 在线rmq的更多相关文章
- 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,倍增算法,在线算法)
/* *********************************************** Author :kuangbin Created Time :2013-9-5 9:45:17 F ...
- poj 1330 Nearest Common Ancestors LCA
题目链接:http://poj.org/problem?id=1330 A rooted tree is a well-known data structure in computer science ...
- POJ 1330 Nearest Common Ancestors(LCA模板)
给定一棵树求任意两个节点的公共祖先 tarjan离线求LCA思想是,先把所有的查询保存起来,然后dfs一遍树的时候在判断.如果当前节点是要求的两个节点当中的一个,那么再判断另外一个是否已经访问过,如果 ...
- 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和ST我们已经很熟悉了,但是这里的f[i][j]却有相似却又不同的含义.f[i][j]表示i节 ...
- 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(lca)
POJ 1330 Nearest Common Ancestors A rooted tree is a well-known data structure in computer science a ...
随机推荐
- PAT Counting Leaves[一般]
1004 Counting Leaves (30)(30 分) A family hierarchy is usually presented by a pedigree tree. Your job ...
- 关闭Oracle 11g的DPR特性
关闭Oracle 11g的DPR(Direct Path Read)特性 查看event参数值: SQL> show parameter event NAME TYPE VALUE ------ ...
- 软件包管理:rpm命令管理-安装升级与卸载
严格区分大小写 卸载命令不许再包的目录下执行.
- liferay中数据库表的解析未完
页面布局 1:表layout 主要的字段有: 字段 privateLayout 0表示的是公开的页面 字段 layoutId 如果在同一个社区中有很多的界面,layoutId表示各个界面,按照顺序排列 ...
- yii2 restful api——app接口编程实例
<?php namespace common\components; use common\models\Cart; use common\models\User; use Yii; use y ...
- MySQL从删库到跑路_高级(四)——存储过程
作者:天山老妖S 链接:http://blog.51cto.com/9291927 一.存储过程简介 1.存储过程简介 存储过程是一组具有特定功能的SQl语句集组成的可编程的函数,经编译创建并保存在数 ...
- php判断数组元素是否存在某个字符串的方法
php判断数组元素是否存在某个字符串的方法: 方法一:采用in_array(value,array,type) type 可选.如果设置该参数为 true,则检查搜索的数据与数组的值的类型是否相同. ...
- C/C++之类型强制转化
强制转化四种类型可能很多人都常常忽略就象我一样,但是有时还是比较有用的.不了解的建议看看,一些机制我也不是十分了解,只是将一些用法写出来让大家看看. ...
- 删除github上个人的repositories的操作步骤
- Python Web学习笔记之IGMP和ICMP的差别
理论技术:TCP/IP协议族(四)ICMP和IGMP协议! 应该先说IP协议的,后来考虑到层次性,还是先把支撑协议介绍完在细说IP!因为IP是我的最爱也是我的痛!呵呵! 一.ICMP协议 为什么要使用 ...