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

16 11 148 510 165 94 68 44 101 136 1510 116 710 216 38 116 1216 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改

[cpp] view plaincopyprint?
  1. #include<stdio.h>
  2. int n,t;
  3. int f[100001][21];
  4. /*‘f[i][j]从第i个点向上蹦2^j步的落脚点’*/
  5. int log_me[100001];
  6. int head[100001];
  7. int to[100001];
  8. int next[100001];
  9. int level[100001];
  10. int queue[100001];
  11. int power[21];
  12. int idx,all_fa,max_dep;
  13. bool is[100001];
  14. void bfs(int p)
  15. {
  16. int front,tail;
  17. front=tail=0;
  18. queue[tail++]=p;
  19. while(front<tail)
  20. {
  21. int idx2=queue[front++];
  22. for(int i=head[idx2];i;i=next[i])
  23. {
  24. level[to[i]]=level[idx2]+1;
  25. queue[tail++]=to[i];
  26. if(level[to[i]]>max_dep)
  27. max_dep=level[to[i]];
  28. }
  29. }
  30. }
  31. int main()
  32. {
  33. scanf("%d%d",&n,&t);
  34. for(int i=1;i<n;i++)
  35. {
  36. int a,b;
  37. scanf("%d%d",&a,&b);
  38. f[b][0]=a;
  39. next[++idx]=head[a];
  40. head[a]=idx;
  41. to[idx]=b;
  42. is[b]=true;
  43. }
  44. log_me[0]=-1;
  45. for(int i=1;i<=n;i++)
  46. {
  47. log_me[i]=log_me[i>>1]+1;
  48. if(!is[i])
  49. all_fa=i;
  50. }
  51. power[0]=1;
  52. for(int i=1;i<=17;i++)
  53. power[i]=power[i-1]*2;
  54. bfs(all_fa);
  55. f[all_fa][0]=all_fa;
  56. f[0][0]=all_fa;
  57. for(int j=1;j<=17;j++)
  58. for(int i=0;i<=n;i++)
  59. f[i][j]=f[f[i][j-1]][j-1];
  60. for(;t;t--)
  61. {
  62. int a,b;
  63. scanf("%d%d",&a,&b);
  64. if(a==b)
  65. {
  66. printf("%d\n",a);
  67. continue;
  68. }
  69. int deep,low,diff_val;
  70. if(level[a]>level[b])
  71. deep=a,low=b;
  72. else
  73. deep=b,low=a;
  74. diff_val=level[deep]-level[low];
  75. for(;level[deep]>level[low];diff_val=level[deep]-level[low])
  76. deep=f[deep][log_me[diff_val]];
  77. if(deep==low)
  78. {
  79. printf("%d\n",deep);
  80. continue;
  81. }
  82. int dep=log_me[level[deep]];
  83. while(dep>=0)
  84. {
  85. if(f[deep][dep]!=f[low][dep])
  86. {
  87. deep=f[deep][dep];
  88. low=f[low][dep];
  89. dep=log_me[level[deep]];
  90. }
  91. dep--;
  92. }
  93. printf("%d\n",f[deep][0]);
  94. }
  95. }

LCA——JD 3055 Nearest Common Ancestors相关推荐

  1. JDOJ 3055: Nearest Common Ancestors

    JDOJ 3055: Nearest Common Ancestors JDOJ传送门 Description 给定N个节点的一棵树,有K次查询,每次查询a和b的最近公共祖先. 样例中的16和7的公共 ...

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

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

  3. POJ 1330 Nearest Common Ancestors 【LCA模板题】

    任意门:http://poj.org/problem?id=1330 Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000 ...

  4. Nearest Common Ancestors(LCA板子)

    题目链接:http://poj.org/problem?id=1330 Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 1000 ...

  5. 【POJ - 1330】Nearest Common Ancestors(lca,模板题)

    题干: A rooted tree is a well-known data structure in computer science and engineering. An example is ...

  6. POJ - 1330 Nearest Common Ancestors(树上倍增/树链剖分求LCA)

    题目链接:点击查看 题目大意:给出一棵有根树,我们需要求出两个点的lca 题目分析:因为题目说了是有根树,所以我们在建图的时候直接建有向图就好了,并且记录一下每个点的入度,建完图后找一下入度为0的点, ...

  7. poj 1330 Nearest Common Ancestors LCA/DFS

    题目链接: http://poj.org/problem?id=1330 题意: 求出两点间的最近公共祖先. 题解: 第一种: 并查集维护:http://www.cnblogs.com/procedu ...

  8. POJ 1330:Nearest Common Ancestors【lca】

    题目大意:唔 就是给你一棵树 和两个点,问你这两个点的LCA是什么 思路:LCA的模板题,要注意的是在并查集合并的时候并不是随意的,而是把叶子节点合到父节点上 #include<cstdio&g ...

  9. [POJ1330 Nearest Common Ancestors]

    [关键字]:LCA [题目大意]:求出两点间的最近公共祖先. //=================================================================== ...

最新文章

  1. 技术干货 | 如何选择上班路线最省时间?从A/B测试数学原理说起
  2. Android Parcelable
  3. crontab执行脚本中文乱码,手动执行没有问题
  4. scrapy-splash抓取动态数据例子六
  5. vmware添加新硬盘 挂载新硬盘 硬盘扩容
  6. (十四)struts2的国际化
  7. Shiro+springboot+mybatis(md5+salt+散列)认证与授权-02
  8. burpsuite下载使用详讲
  9. 支撑性服务 自动化能力
  10. 给正在努力的您几条建议(附开源代码)
  11. ShadeGraph教程之节点详解6:Procedural Nodes
  12. Java并发编程框架Disruptor
  13. Atitit 软件理论方面的书籍 目录 1. 计算机科学分为计算机理论和计算机应用。 计算机基础理论包含以下几部分: 2 1.1. ( 1) 程序理论( 程序逻辑、程序正确性验证、形式开发方法等
  14. 诺基亚S40系统手机使用技巧大全(此乃刘某整理)
  15. HFSS仿真结果输出
  16. docker-Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon runn
  17. 解决有道云笔记中Markdown语法中代码块字体太小的问题
  18. 苹果在高端手机市场碾压安卓手机,国产旗舰不入前十
  19. CREO2——解决CREO生成的二维图drw转换成CAD的dwg格式尺寸失真问题
  20. 数学主题分类 - 中文(下)

热门文章

  1. 人工智能还能研究古文字?
  2. 在GIS中用ggmap地理空间数据分析
  3. 信用评分卡 (part 5 of 7)
  4. 聚焦五大亮点,神策数据 A/B 测试功能全新发布!
  5. 机器学习基础---降维方法---T分布随机近邻嵌入(TSNE)推导
  6. LEARNING A SAT SOLVER FROM SINGLE-BIT SUPERVISION 2020-05-01
  7. 1分钟教会你二进制撩妹(汉)读心术
  8. C++进阶提取txt中的建筑物信息并且删除特定信息
  9. tomcat环境配置,新鸟过来学习一下吧
  10. 《Python从入门到实践》第五章动手试一试