LCA——JD 3055 Nearest Common Ancestors
3055: Nearest Common Ancestors
Time Limit: 1 Sec Memory Limit: 128 MB
Description
给定N个节点的一棵树,有K次查询,每次查询a和b的最近公共祖先。
样例中的16和7的公共祖先(LCA:Least Common Ancestors)是4。
Input
第一行两个整数N(1 < N <= 105)、K(1 <= K <= 105)
第2~N行,每行两个整数a、b(1 <= a,b <= N),表示a是b的父亲。
第N+1~N+K+1行,每行两个整数a、b(1 <= a,b <= N),表示询问a和b的最近公共祖先是谁。
Output
输出K行,第i行表示第i个查询的最近公共祖先是谁。
Sample Input
Sample Output
HINT
30%数据 N<=20,K<=5。小数据,方便调试
50%数据 N<=1000,K<=1000。中数据,暴力可过
100%数据 1 < N <= 105,1 <= K <= 105。大数据,请使用树上倍增、LCA转RMQ&ST、离线Tarjan、树链剖分求LCA
Source
poj1330改
- #include<stdio.h>
- int n,t;
- int f[100001][21];
- /*‘f[i][j]从第i个点向上蹦2^j步的落脚点’*/
- int log_me[100001];
- int head[100001];
- int to[100001];
- int next[100001];
- int level[100001];
- int queue[100001];
- int power[21];
- int idx,all_fa,max_dep;
- bool is[100001];
- void bfs(int p)
- {
- int front,tail;
- front=tail=0;
- queue[tail++]=p;
- while(front<tail)
- {
- int idx2=queue[front++];
- for(int i=head[idx2];i;i=next[i])
- {
- level[to[i]]=level[idx2]+1;
- queue[tail++]=to[i];
- if(level[to[i]]>max_dep)
- max_dep=level[to[i]];
- }
- }
- }
- int main()
- {
- scanf("%d%d",&n,&t);
- for(int i=1;i<n;i++)
- {
- int a,b;
- scanf("%d%d",&a,&b);
- f[b][0]=a;
- next[++idx]=head[a];
- head[a]=idx;
- to[idx]=b;
- is[b]=true;
- }
- log_me[0]=-1;
- for(int i=1;i<=n;i++)
- {
- log_me[i]=log_me[i>>1]+1;
- if(!is[i])
- all_fa=i;
- }
- power[0]=1;
- for(int i=1;i<=17;i++)
- power[i]=power[i-1]*2;
- bfs(all_fa);
- f[all_fa][0]=all_fa;
- f[0][0]=all_fa;
- for(int j=1;j<=17;j++)
- for(int i=0;i<=n;i++)
- f[i][j]=f[f[i][j-1]][j-1];
- for(;t;t--)
- {
- int a,b;
- scanf("%d%d",&a,&b);
- if(a==b)
- {
- printf("%d\n",a);
- continue;
- }
- int deep,low,diff_val;
- if(level[a]>level[b])
- deep=a,low=b;
- else
- deep=b,low=a;
- diff_val=level[deep]-level[low];
- for(;level[deep]>level[low];diff_val=level[deep]-level[low])
- deep=f[deep][log_me[diff_val]];
- if(deep==low)
- {
- printf("%d\n",deep);
- continue;
- }
- int dep=log_me[level[deep]];
- while(dep>=0)
- {
- if(f[deep][dep]!=f[low][dep])
- {
- deep=f[deep][dep];
- low=f[low][dep];
- dep=log_me[level[deep]];
- }
- dep--;
- }
- printf("%d\n",f[deep][0]);
- }
- }
LCA——JD 3055 Nearest Common Ancestors相关推荐
- JDOJ 3055: Nearest Common Ancestors
JDOJ 3055: Nearest Common Ancestors JDOJ传送门 Description 给定N个节点的一棵树,有K次查询,每次查询a和b的最近公共祖先. 样例中的16和7的公共 ...
- 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模板题】
任意门:http://poj.org/problem?id=1330 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000 ...
- Nearest Common Ancestors(LCA板子)
题目链接:http://poj.org/problem?id=1330 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 1000 ...
- 【POJ - 1330】Nearest Common Ancestors(lca,模板题)
题干: A rooted tree is a well-known data structure in computer science and engineering. An example is ...
- POJ - 1330 Nearest Common Ancestors(树上倍增/树链剖分求LCA)
题目链接:点击查看 题目大意:给出一棵有根树,我们需要求出两个点的lca 题目分析:因为题目说了是有根树,所以我们在建图的时候直接建有向图就好了,并且记录一下每个点的入度,建完图后找一下入度为0的点, ...
- poj 1330 Nearest Common Ancestors LCA/DFS
题目链接: http://poj.org/problem?id=1330 题意: 求出两点间的最近公共祖先. 题解: 第一种: 并查集维护:http://www.cnblogs.com/procedu ...
- POJ 1330:Nearest Common Ancestors【lca】
题目大意:唔 就是给你一棵树 和两个点,问你这两个点的LCA是什么 思路:LCA的模板题,要注意的是在并查集合并的时候并不是随意的,而是把叶子节点合到父节点上 #include<cstdio&g ...
- [POJ1330 Nearest Common Ancestors]
[关键字]:LCA [题目大意]:求出两点间的最近公共祖先. //=================================================================== ...
最新文章
- 技术干货 | 如何选择上班路线最省时间?从A/B测试数学原理说起
- Android Parcelable
- crontab执行脚本中文乱码,手动执行没有问题
- scrapy-splash抓取动态数据例子六
- vmware添加新硬盘 挂载新硬盘 硬盘扩容
- (十四)struts2的国际化
- Shiro+springboot+mybatis(md5+salt+散列)认证与授权-02
- burpsuite下载使用详讲
- 支撑性服务 自动化能力
- 给正在努力的您几条建议(附开源代码)
- ShadeGraph教程之节点详解6:Procedural Nodes
- Java并发编程框架Disruptor
- Atitit 软件理论方面的书籍 目录 1. 计算机科学分为计算机理论和计算机应用。 计算机基础理论包含以下几部分:	2 1.1. ( 1) 程序理论( 程序逻辑、程序正确性验证、形式开发方法等
- 诺基亚S40系统手机使用技巧大全(此乃刘某整理)
- HFSS仿真结果输出
- docker-Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon runn
- 解决有道云笔记中Markdown语法中代码块字体太小的问题
- 苹果在高端手机市场碾压安卓手机,国产旗舰不入前十
- CREO2——解决CREO生成的二维图drw转换成CAD的dwg格式尺寸失真问题
- 数学主题分类 - 中文(下)