JDOJ 3055: Nearest Common Ancestors

JDOJ传送门

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

POJ1330改

本题历史背景:

本题初次使用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不太会的同学请参考我的博客补习:

博客链接:

求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的更多相关文章

  1. POJ 1330 Nearest Common Ancestors(Targin求LCA)

    传送门 Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26612   Ac ...

  2. [最近公共祖先] POJ 1330 Nearest Common Ancestors

    Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 27316   Accept ...

  3. POJ 1330 Nearest Common Ancestors

    Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14698   Accept ...

  4. POJ1330 Nearest Common Ancestors

      Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24587   Acce ...

  5. POJ 1330 Nearest Common Ancestors(Tree)

    题目:Nearest Common Ancestors 根据输入建立树,然后求2个结点的最近共同祖先. 注意几点: (1)记录每个结点的父亲,比较层级时要用: (2)记录层级: (3)记录每个结点的孩 ...

  6. 【POJ】1330 Nearest Common Ancestors ——最近公共祖先(LCA)

    Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18136   Accept ...

  7. POJ 1330 Nearest Common Ancestors LCA题解

    Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19728   Accept ...

  8. POJ - 1330 Nearest Common Ancestors(基础LCA)

    POJ - 1330 Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000KB   64bit IO Format: %l ...

  9. POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA)

    POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA) Description A ...

随机推荐

  1. VBS实现UTC时间和本地时间互转

    本地时间转UTC时间 dim SWDT, datetime, utcTime Set SWDT = CreateObject("WbemScripting.SWbemDateTime&quo ...

  2. PyCharm工具配置和快捷键使用

    PyCharm是一款高效开发Python程序的IDE,包含有自动联想.语法高亮.代码调试.管理多个版本Python解释器等功能.本文主要描述Python界面个性化定制方法(字体.颜色配置).常用配置和 ...

  3. docker Dockerfile实战

    目录 Dockerfile实战 基础pm2 Dockerfile keymetrics/pm2:8-alpine keymetrics/pm2:12-alpine pm2 node Dockerfil ...

  4. (十九)golang--函数参数的传递方式

    两种传递方式: 值传递:值类型参数默认 引用传递:引用类型参数默认 一般来说,地址传递效率高,因为数据量小. 值类型:int.float.bool.string.数组.结构体: 引用类型:指针.切片. ...

  5. fiddler抓包-7-C端弱网测试

    前言大家平时也会发现我们有时候在地铁.高铁.电梯等等某个时候网络信号比较差导致网络延迟较大,这时是否有友好提示呢?甚至有可能发生崩溃等等...所以我们是可以通过fiddler来对web.APP.PC客 ...

  6. 【shell脚本】定时备份日志===logBackup.sh

    定时备份日志 设置执行权限 [root@VM_0_10_centos shellScript]# chmod a+x logBackup,sh 脚本内容 [root@VM_0_10_centos sh ...

  7. vue中toggle切换的3种写法

    前言:查看下面代码,在任意编辑器中直接复制粘贴运行即可 1:非动态组件(全局注册2个组件,借用v-if指令和三元表达式) <!DOCTYPE html> <html> < ...

  8. HDU 1723 Distribute Message DP

    The contest’s message distribution is a big thing in prepare. Assuming N students stand in a row, fr ...

  9. java 金额数字转换大写算法

    根据人民币大写金额规范,转换有几点要注意的: 阿拉伯数字中间有"0"时,中文大写金额中间可以只写一个"零"字.如¥1,409.50,应写成人民币壹仟肆佰零玖圆伍 ...

  10. 在IT产品白皮书中遇到的缩略词

    在IT产品白皮书中遇到的缩略词 更新中...