题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cmath>
  4. #include<queue>
  5. #include<vector>
  6. #include<string.h>
  7. #include<cstring>
  8. #include<algorithm>
  9. #include<set>
  10. #include<map>
  11. #include<fstream>
  12. #include<cstdlib>
  13. #include<ctime>
  14. #include<list>
  15. #include<climits>
  16. #include<bitset>
  17. using namespace std;
  18. #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  19. #define fopen freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
  20. #define scd(a) scanf("%d",&a)
  21. #define scf(a) scanf("%lf",&a)
  22. #define scl(a) scanf("%lld",&a)
  23. #define sci(a) scanf("%I64d",&a)
  24. #define scs(a) scanf("%s",a)
  25. #define left asfdasdasdfasdfsdfasfsdfasfdas1
  26. #define tan asfdasdasdfasdfasfdfasfsdfasfdas
  27. typedef long long ll;
  28. typedef unsigned int un;
  29. const int desll[][]={{,},{,-},{,},{-,}};
  30. const ll mod=1e9+;
  31. const int maxn=4e4+;
  32. const int maxm=3e8+;
  33. const double eps=1e-;
  34. int m,n;
  35.  
  36. struct node
  37. {
  38. int b,nex,c;
  39. }no[maxn*];
  40. int head[maxn],sz=;
  41. int add(int a,int b,int c){
  42. no[sz].b=b;
  43. no[sz].c=c;
  44. no[sz].nex=head[a];
  45. head[a]=sz++;
  46. }
  47. int father[maxn],tan[maxn*][],dep[maxn];
  48. int ar[maxn*],le,in[maxn];
  49. ll dis[maxn];
  50. void dfs1(int u,int pre){
  51. for(int i=head[u];i!=-;i=no[i].nex){
  52. int v=no[i].b;
  53. if(v==pre)continue;
  54. dis[v]=dis[u]+no[i].c;
  55. father[v]=u;
  56. dep[v]=dep[u]+;
  57. ar[le++]=u;
  58. dfs1(v,u);
  59. }
  60. in[u]=le;
  61. ar[le++]=u;
  62. }
  63. void init()
  64. {
  65. memset(tan,,sizeof(tan));
  66. for(int i=;i<le;i++){
  67. tan[i][]=ar[i];
  68. }
  69. for(int j=;j<;j++){
  70. for(int i=le-;i>=;i--){
  71. if(i+(<<j)->=le)continue;
  72. int x=tan[i][j-],y=tan[i+(<<(j-))][j-];
  73. if(dep[x]<dep[y])tan[i][j]=x;
  74. else tan[i][j]=y;
  75. //cout<<i<<" "<<j<<" "<<tan[i][j]<<endl;
  76. }
  77. }
  78. }
  79. int sameFather(int a,int b)
  80. {
  81. a=in[a];
  82. b=in[b];
  83. if(a>b)swap(a,b);
  84. int res=floor(log(b-a+)/log());
  85. int x=tan[a][res],y=tan[b-(<<res)+][res];
  86. if(dep[x]<dep[y])return x;
  87. else return y;
  88. }
  89.  
  90. int main()
  91. {
  92. int t;scd(t);
  93. while(t--){
  94. scd(n);scd(m);
  95. memset(head,-,sizeof(head));sz=;
  96. for(int i=;i<n;i++){
  97. int a,b,c;
  98. scd(a);scd(b);scd(c);
  99. add(a,b,c);
  100. add(b,a,c);
  101. }
  102. memset(dis,,sizeof(dis));
  103. memset(dep,,sizeof(dep));
  104. dep[]=;
  105. dfs1(,);
  106. father[]=;
  107. init();
  108. for(int i=;i<m;i++){
  109. int a,b;
  110. scd(a);scd(b);
  111. int x=sameFather(a,b);
  112. printf("%d\n",dis[a]+dis[b]-*dis[x]);
  113. }
  114. }
  115. return ;
  116. }

lca最短公共祖先模板(hdu2586)的更多相关文章

  1. LCA(最近公共祖先)模板

    Tarjan版本 /* gyt Live up to every day */ #pragma comment(linker,"/STACK:1024000000,1024000000&qu ...

  2. LCA最近公共祖先模板(求树上任意两个节点的最短距离 || 求两个点的路进(有且只有唯一的一条))

    原理可以参考大神 LCA_Tarjan (离线) TarjanTarjan 算法求 LCA 的时间复杂度为 O(n+q) ,是一种离线算法,要用到并查集.(注:这里的复杂度其实应该不是 O(n+q)  ...

  3. LCA最近公共祖先模板代码

    vector模拟邻接表: #include<iostream> #include<cstdio> #include<cstring> #include<cma ...

  4. LCA(最近公共祖先)之倍增算法

    概述 对于有根树T的两个结点u.v,最近公共祖先LCA(T,u,v)表示一个结点x,满足x是u.v的祖先且x的深度尽可能大. 如图,3和5的最近公共祖先是1,5和2的最近公共祖先是4 在本篇中我们先介 ...

  5. CodeVs.1036 商务旅行 ( LCA 最近公共祖先 )

    CodeVs.1036 商务旅行 ( LCA 最近公共祖先 ) 题意分析 某首都城市的商人要经常到各城镇去做生意,他们按自己的路线去做,目的是为了更好的节约时间. 假设有N个城镇,首都编号为1,商人从 ...

  6. lca 最近公共祖先

    http://poj.org/problem?id=1330 #include<cstdio> #include<cstring> #include<algorithm& ...

  7. Tarjan算法应用 (割点/桥/缩点/强连通分量/双连通分量/LCA(最近公共祖先)问题)(转载)

    Tarjan算法应用 (割点/桥/缩点/强连通分量/双连通分量/LCA(最近公共祖先)问题)(转载) 转载自:http://hi.baidu.com/lydrainbowcat/blog/item/2 ...

  8. LCA近期公共祖先

    LCA近期公共祖先 该分析转之:http://kmplayer.iteye.com/blog/604518 1,并查集+dfs 对整个树进行深度优先遍历.并在遍历的过程中不断地把一些眼下可能查询到的而 ...

  9. LCA 近期公共祖先 小结

    LCA 近期公共祖先 小结 以poj 1330为例.对LCA的3种经常使用的算法进行介绍,分别为 1. 离线tarjan 2. 基于倍增法的LCA 3. 基于RMQ的LCA 1. 离线tarjan / ...

随机推荐

  1. codeforces 1060 A

    https://codeforces.com/contest/1060/problem/A 题意:电话号码是以8开头的11位数,给你n 个数问最多可以有多少个电话号码 题解:min(8的个数,n/11 ...

  2. Ubuntu 编译Webkit --gtk

    转载自:http://www.linuxidc.com/Linux/2011-10/44809.htm webkit是一个浏览器内核,google的chrome就是基于它的,下面介绍一下如何在Ubun ...

  3. 前端跨域之jsonp跨域

    jsonp跨域原理 原理:因为通过script标签引入的js是不受同源策略的限制的(比如baidu.com的页面加载了google.com的js).所以我们可以通过script标签引入一个js或者一个 ...

  4. Lucene4.6查询时完全跳过打分,提高查询效率的实现方式

    由于索引的文件量比较大,而且应用中不需要对文档进行打分,只需要查询出所有满足条件的文档.所以需要跳过打分来提高查询效率.一开始想用ConstantScoreQuery,但是测试发现这个类虽然让所有返回 ...

  5. 【hdu3080】01背包(容量10^7)

    [题意]n个物品,有wi和vi,组成若干个联通块,只能选取一个联通块,问得到m的价值时最小要多少空间(v).n<=50,v<=10^7 [题解] 先用并查集找出各个联通块. 这题主要就是v ...

  6. NOIP2005过河(青蛙过河)

    题目传送门 这道题主要是因为L长度最大可以为1e9 而石子却最多只有100个 这样就浪费了很多时间空间 所以我们压缩一波路径就可以了 剩余的就是枚举每个点以及i-y到i-x的dp了 这里要说一句为什么 ...

  7. 【HDU】6146 Pokémon GO

    [题意]一个2*n的网格,再保证步数最少的情况下,求从任意格出发遍历完所有格的方案数,格子八连通.n<=10000,T<=100. [算法]递推,DP [题解]原题链接:蓝桥杯 格子刷油漆 ...

  8. #error#错误原因:Cannot find executable for CFBundle 0x8ad60b0 (not loaded)

    #error#错误原因:Cannot find executable for CFBundle 0x8ad60b0 </Applications/Xcode.app/Contents/Devel ...

  9. 小白科普之JavaScript的DOM模型

    微信公众号“前端大全”推送了一篇名为“通俗易懂的来讲讲DOM”的文章,把javascript原生DOM相关内容讲解的很详细.仔细读了一遍,觉得整理总结的不错,对自己也很使用,所以把内容整理过来,并根据 ...

  10. linux下su和su - 的区别

    linux使用中常会使用su来切换用户 使用su切换为tom用户 [root@bogon ~]# su tom[tom@bogon root]$ [tom@bogon root]$ pwd/root ...