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 ...
随机推荐
- Python做接口自动化测试框架
框架结构如下: Test_Api_Project||---base.py|---base_api| |---register_api.py | |---send_sms_code_api.py|--- ...
- qt用mingw编译时报错 multiple definition of
网上相关回答不少,但过于简单,这里做一下记录. qt用mingw编译程序时报“multiple definition of …”这个错误,错误信息大概是如下图所示: 1 2 3 首先,检查自己的程序是 ...
- css3 3d翻转效果
<div class="demo"> <span class="front"> aaaaaaaaaaa ...
- Python: ljust()|rjust()|center()字符串对齐
通过某种对齐方式来格式化字符串 ①对于基本的操作,可以使用字符串的ljust(),rjust(),center() ②函数format()同样可以用来很容易的对齐字符串,使用<,>,~
- linux常用命令:killall 命令
killall命令用进程的名字来杀死进程. 1.命令格式: killall [ -egiqvw ] [ -signal ] [进程名称] 格式:killall -<signame> ...
- tortoisegit 代码的回滚方式 --两种
TortoiseGit有两种回滚代码方式, 一种是导出指定版本代码为zip格式,不影响源代码:另一种是直接在源代码上回滚, 指定版本之后写的代码都会被删除.下面分别介绍这两种方法: 首先进入版本日志对 ...
- PageRank算法与TextRank算法详解
PageRank算法: 该算法本质上属于有向带权图. 对于某个互联网网页A来说,该网页PageRank的计算基于以下两个基本假设: 数量假设:在Web图模型中,如果一个页面节点接收到的其他网页指向的入 ...
- php CI框架实现验证码功能和增强验证码安全性实战教程
php CI框架实现验证码功能和增强验证码安全性实战教程 CodeIgniter简称CI是最流行的一个php MVC框架之一,本人讲从实际项目使用中写系列实战经验,有别与其他的理论讲解文章,会附上实战 ...
- 将图片文件转化为字节数组字符串,并对其进行Base64编码处理,以及对字节数组字符串进行Base64解码并生成图片
实际开发中涉及图片上传并且量比较大的时候一般处理方式有三种 1.直接保存到项目中 最老土直接方法,也是最不适用的方法,量大对后期部署很不方便 2.直接保存到指定路径的服务器上.需要时候在获取,这种方式 ...
- 关于webpack的path和publicPath。
path:所有输出文件的目标路径; publicPath:输出解析文件的目录,url 相对于 HTML 页面 区别:path是webpack所有文件的输出的路径,必须是绝对路径,比如:output输出 ...