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 ...
随机推荐
- Vue 变异方法splice删除评论功能
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Vue v-for操作与computed结合功能
<!doctype html> <html lang="en"> <head id="head"> <meta cha ...
- 【LOJ2838】「JOISC 2018 Day 3」比太郎的聚会(设阈值预处理/分块)
点此看题面 大致题意: 给你一张\(DAG\),多组询问,每次问你在起点不为某些点的前提下,到达给定终点的最大距离是多少. 设阈值 由于限制点数总和与\(n\)同阶,因此容易想到去设阈值. 对于限制点 ...
- Unity Profiler 记录
版本 Unity 2018.4.6f1 空包 development build 魅蓝 note3 OPPO R9 VIVO x9 华为 P8 青春版 小米 8 SE iphone se Other ...
- js --自动播放音频
简介 基本使用 chrome下无法自动播放问题处理 简介 音频的播放使用audio进行操作,可以有两种方式处理(纯js和html标签+js). audio是html5的新标签,用于定义声音 audio ...
- java1.8 AQS AbstractQueuedSynchronizer学习
AQS concurrent并发包中非常重要的顶层锁类,往往用的比较多的是ReentrantLock,然而ReentrantLock的实现依赖AbstractQueuedSynchronizer在到上 ...
- 《一起学mysql》3
索引和查询优化 为什么要索引? 想想我们上小学的时候是怎么查字典的,比方查 理想的 “理”,首先在索引里找到声母 “l”,再找到 “li” 找到 “li”所在的页数, 我们之前建的所有mysq ...
- 通过requirements.txt文件创建虚拟环境副本
1.首先在原来的环境中生成一个需求文件requirements.txt,用于记录所有依赖包及其精确的版本号. (venv) $ pip freeze >requirements.txt 2.创建 ...
- Windows重要的win键
win+↓ 当前窗口操作,多按几下就缩没了(同理,其他箭头也一样) win+e 打开此电脑 win+v 展开剪切板 win+k 访问蓝牙 win+a win10的通知 win+d (切到桌面,再用能切 ...
- SQL Server like 字段
参考资料:Cannot resolve the collation conflict between "Chinese_PRC_CI_AS" and "SQL_L及由于排 ...