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 ...
随机推荐
- POJ2976Dropping tests(分数规划)
传送门 题目大意:n个二元组a[i],b[i],去掉k个,求sigma a[i]/ sigma b[i]的最大值 代码: #include<iostream> #include<cs ...
- Start LaTex
目录 Size Color Shape Common Function Type Fill Label Beamer Example Size You can use: ultra thin , ve ...
- 分布式中session共享的解决方案:spring-session
Session是客户端与服务器通讯会话跟踪技术,是服务器与客户端保持整个通讯的会话基本信息.客户端在第一次访问服务器的时候,服务端会响应一个sessionId并且将它存入到本地的Cookie中,在之后 ...
- mysql批量更新数据(性能优化)--第二种方式
Spring+Mybatis 手动控制事务 参考: https://blog.csdn.net/qq_41750175/article/details/87621170 public boolean ...
- 特征金字塔网络Feature Pyramid Networks
小目标检测很难,为什么难.想象一下,两幅图片,尺寸一样,都是拍的红绿灯,但是一副图是离得很近的拍的,一幅图是离得很远的拍的,红绿灯在图片里只占了很小的一个角落,即便是对人眼而言,后者图片中的红绿灯也更 ...
- 基于Spark的电影推荐系统(推荐系统~7)
基于Spark的电影推荐系统(推荐系统~7) 22/100 发布文章 liuge36 第四部分-推荐系统-实时推荐 本模块基于第4节得到的模型,开始为用户做实时推荐,推荐用户最有可能喜爱的5部电影. ...
- Winform中设置ZedGraph的颜色填充使用Fill
场景 Winforn中设置ZedGraph曲线图的属性.坐标轴属性.刻度属性: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/10 ...
- js删除html标记 去掉所有html标记
js删除html标记 去掉所有html标记 function delHtml(str){ return str.replace(/<[^>]+>/g,""); / ...
- 关于网页布局中常见的margin: 0px ; padding: 0px; 总结
我们在网页布局中常用到margin: 0px; padding: 0px; 但是在大型的网站布局中通常不这样写通常是按下面这种方式. ul, li, ol, dl, dt, dd, div, p, ...
- <Android Studio> 1.如何APP配置权限
Android Studio必须手动配置权限: 如添加权限 : android.permission.RECEIVE_BOOT_COMPLETED 1.android.permission.WRITE ...