JDOJ 3055: Nearest Common Ancestors
JDOJ 3055: Nearest Common Ancestors
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
16 1 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
Sample Output
4
HINT
30%数据 N<=20,K<=5。小数据,方便调试
50%数据 N<=1000,K<=1000。中数据,暴力可过
100%数据 1 < N <= 105,1 <= K <= 105。大数据,请使用树上倍增、LCA转RMQ&ST、离线Tarjan、树链剖分求LCA
Source
本题历史背景:
本题初次使用C++语言提交于2019.9.11晚20:01
看到没有人用C语言交,就用原代码混了C语言榜首第一。
但是却被\(iamrjj\)给顶了。为了防止我夺回第一,他用了各种技巧把时间提到了64ms,却死活不告诉我算法和代码实现方式。
当然,顺带着,他还Diss了我几句。
UPD:2019.9.12 晚19:21
正义终归是正义@ysy20021208
在机房大佬的帮助加各种卡常技巧加我++的RP(行正则正)
我终于夺回了被\(iamrjj\)临时掌控的榜首位置
时间是这样的(为了防止\(iamrjj\)盗代码我不贴代码和改进思路,有对此好奇的请私聊我)
256ms--104ms--60ms--48ms
上两发最优解证明:


在此建议大家:
不要怕这类事情发生,有些位置天生就属于一个人,只要肯付出努力,谁也抢不走。
题解:
这道题就是LCA的裸题
只不过我这次用了倍增,又心血来潮卡了C语言的最优解。
所以附上代码,如果倍增LCA不太会的同学请参考我的博客补习:
博客链接:
#include<stdio.h>
#pragma GCC optimize(2)
#pragma GCC optimize(3)
int n,k,tot,root;
int fa[200008];
int head[200008],nxt[200008],to[200008];
int deep[200008],v[200008],f[200008][21];
int read()
{
int x=0,f=1;
char ch=getchar();
while(ch<'0'|| ch>'9')
{
if(ch=='-')
f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
x=x*10+ch-'0',ch=getchar();
return x*f;
}
void add(int x,int y)
{
to[++tot]=y;
nxt[tot]=head[x];
head[x]=tot;
}
void dfs(int x)
{
v[x]=1;
for(int i=head[x];i;i=nxt[i])
{
int y=to[i];
if(v[y]==1)
continue;
deep[y]=deep[x]+1;
f[y][0]=x;
dfs(y);
}
}
int lca(int x,int y)
{
if(deep[x]<deep[y])
{
int t=y;
y=x;
x=t;
}
for(int i=20;i>=0;i--)
if(deep[f[x][i]]>=deep[y])
x=f[x][i];
if(x==y)
return x;
for(int i=20;i>=0;i--)
if(f[x][i]!=f[y][i])
{
x=f[x][i];
y=f[y][i];
}
return f[x][0];
}
int main()
{
n=read();k=read();
for(int i=1;i<n;i++)
{
int x,y;
x=read();y=read();
add(x,y);
add(y,x);
fa[y]=x;
}
for(int i=1;i<=n;i++)
if(fa[i]==0)
{
root=i;
break;
}
deep[root]=1;
dfs(root);
for(int i=1;i<=20;i++)
for(int j=1;j<=n;j++)
f[j][i]=f[f[j][i-1]][i-1];
for(int i=1;i<=k;i++)
{
int x,y;
x=read();y=read();
printf("%d\n",lca(x,y));
}
return 0;
}
JDOJ 3055: Nearest Common Ancestors的更多相关文章
- 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
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 27316 Accept ...
- POJ 1330 Nearest Common Ancestors
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14698 Accept ...
- POJ1330 Nearest Common Ancestors
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24587 Acce ...
- POJ 1330 Nearest Common Ancestors(Tree)
题目:Nearest Common Ancestors 根据输入建立树,然后求2个结点的最近共同祖先. 注意几点: (1)记录每个结点的父亲,比较层级时要用: (2)记录层级: (3)记录每个结点的孩 ...
- 【POJ】1330 Nearest Common Ancestors ——最近公共祖先(LCA)
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18136 Accept ...
- 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)
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 ...
随机推荐
- JS中的undefined,null,"",0,'0'和false
){ console.log(); } '){ console.log() } '){ console.log() } if(false==0.0){ console.log() } if(false ...
- 多台Linux 7.x服务器具有相同的UUID网络链接参数,肿么办?
1.查看多台服务器的UUID网络链接参数是否相同 我这里使用SecureCRT的全部交互功能,直接批量输出 /etc/sysconfig/network-scripts/ifcfg-ens33 的内 ...
- 解决WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!
问题: 当我想要利用win10本地的cmd进行: ssh root@192.168.1.230 时,出现了如下错误: C:\Users\Raodi>ssh root@192.168.1.230 ...
- keras和tensorflow搭建DNN、CNN、RNN手写数字识别
MNIST手写数字集 MNIST是一个由美国由美国邮政系统开发的手写数字识别数据集.手写内容是0~9,一共有60000个图片样本,我们可以到MNIST官网免费下载,总共4个.gz后缀的压缩文件,该文件 ...
- Unity开发实战探讨-资源的加载释放最佳策略简要心得
Unity开发实战探讨-资源的加载释放最佳策略简要心得 看过我另外一篇关于Unity资源释放随笔<Unity开发实战探讨-资源的加载释放最佳策略>如果觉得略微复杂,那么下面是一些比较简要的 ...
- Docker系列之学习笔记
一.Docker简介 1.1.Docker架构 Docker 使用客户端-服务器 (C/S) 架构模式,分为Docker守护进程和客户端,Docker 客户端,实际上是 docker 的二进制程序,D ...
- 【Oracle】Oracle自动内存管理AMM
Oracle自动内存管理AMM AMM(Automatic Memory Management)自动内存管理,分配一整块内存区域,Oracle数据库自动分配管理SGA和PGA的内存.具体通过设置两个参 ...
- 数据竞争检查工具(TSan)
https://github.com/google/sanitizers/wiki https://github.com/google/sanitizers/wiki/ThreadSanitizerC ...
- ASP.NET MVC EF 连接数据库(三)-----Code First
Code first (VS2015 ,Sql Server2014) 新建MVC项目 实例: 在数据库中会有个新建的数据库和表 源码地址:https://note.youdao.com/ynotes ...
- Tp5.1开发初入门
今天需要给金融部门那边做一个信用卡的推广页面,他们系统是用PHP的tp框架做的.我记得最早做tp还是2的时候,和现在的5.1相差太大了,中间开发的时候,还是遇到了点问题.所以,把今天的问题记录下,作个 ...