2586.How far away ?

这个题以前写过在线LCA(ST)的,HDU2586.How far away ?-在线LCA(ST)

现在贴一个离线Tarjan版的

代码:

  1. //A-HDU2586-LCA-tarjan离线版
  2. #include<iostream>
  3. #include<cstdio>
  4. #include<cstring>
  5. #include<algorithm>
  6. #include<bitset>
  7. #include<cassert>
  8. #include<cctype>
  9. #include<cmath>
  10. #include<cstdlib>
  11. #include<ctime>
  12. #include<deque>
  13. #include<iomanip>
  14. #include<list>
  15. #include<map>
  16. #include<queue>
  17. #include<set>
  18. #include<stack>
  19. #include<vector>
  20. using namespace std;
  21. typedef long long ll;
  22.  
  23. const double PI=acos(-1.0);
  24. const double eps=1e-;
  25. const ll mod=1e9+;
  26. const int inf=0x3f3f3f3f;
  27. const int maxn=4e4+;
  28. const int maxm=+;
  29. #define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  30.  
  31. struct node{
  32. int x,y;
  33. //node(int xx,int yy){
  34. // x=xx,y=yy;
  35. //}node(){}
  36. };
  37.  
  38. vector<node> edge[maxn],q[maxn];
  39. int ans[maxn],dis[maxn],fa[maxn],vis[maxn];
  40. int n,m,aa,bb,cc;
  41.  
  42. int find(int x)//找父节点(祖先)
  43. {
  44. return x==fa[x]?x:fa[x]=find(fa[x]);
  45. }
  46.  
  47. void unio(int x,int y)//并查集压缩路径
  48. {
  49. fa[find(y)]=find(x);
  50. }
  51.  
  52. void init()//初始化
  53. {
  54. for(int i=;i<=n;i++){
  55. edge[i].clear();
  56. q[i].clear();
  57. fa[i]=i;//初始化自己的父亲节点是自己
  58. vis[i]=;
  59. ans[i]=;
  60. dis[i]=;
  61. }
  62. }
  63.  
  64. void dfs(int x)//tarjan
  65. {
  66. vis[x]=;
  67. for(int i=;i<edge[x].size();i++){
  68. int v=edge[x][i].x;
  69. if(!vis[v]){
  70. dis[v]=dis[x]+edge[x][i].y;
  71. dfs(v);
  72. unio(x,v);
  73. }
  74. }
  75. for(int i=;i<q[x].size();i++){
  76. int v=q[x][i].x;
  77. if(vis[v])
  78. ans[q[x][i].y]=dis[x]+dis[v]-*dis[find(v)];
  79. }
  80. }
  81.  
  82. int main()
  83. {
  84. int T;
  85. scanf("%d",&T);
  86. while(T--){
  87. scanf("%d%d",&n,&m);
  88. init();
  89. for(int i=;i<n;i++){
  90. scanf("%d%d%d",&aa,&bb,&cc);
  91. //edge[aa].push_back(node(bb,cc));
  92. //edge[bb].push_back(node(aa,cc));
  93. edge[aa].push_back({bb,cc});
  94. edge[bb].push_back({aa,cc});
  95. }
  96. for(int i=;i<=m;i++){
  97. scanf("%d%d",&aa,&bb);
  98. //q[aa].push_back(node(bb,i));
  99. //q[bb].push_back(node(aa,i));
  100. q[aa].push_back({bb,i});
  101. q[bb].push_back({aa,i});
  102.  
  103. }
  104. dfs();
  105. for(int i=;i<=m;i++)
  106. printf("%d\n",ans[i]);
  107. }
  108. }

溜了。

HDU 2586.How far away ?-离线LCA(Tarjan)的更多相关文章

  1. hdu 2586 How far away ? ( 离线 LCA , tarjan )

    How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  2. hihoCoder #1067 : 最近公共祖先·二 [ 离线LCA tarjan ]

    传送门: #1067 : 最近公共祖先·二 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 上上回说到,小Hi和小Ho用非常拙劣——或者说粗糙的手段山寨出了一个神奇的网站 ...

  3. poj1470 Closest Common Ancestors [ 离线LCA tarjan ]

    传送门 Closest Common Ancestors Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 14915   Ac ...

  4. hdu 2586 How far away?(LCA模板题+离线tarjan算法)

    How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  5. 【HDU 2586 How far away?】LCA问题 Tarjan算法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意:给出一棵n个节点的无根树,每条边有各自的权值.给出m个查询,对于每条查询返回节点u到v的最 ...

  6. HDU 2586 How far away ? (LCA,Tarjan, spfa)

    题意:给定N个节点一棵树,现在要求询问任意两点之间的简单路径的距离,其实也就是最短路径距离. 析:用LCA问题的Tarjan算法,利用并查集的优越性,产生把所有的点都储存下来,然后把所有的询问也储存下 ...

  7. HDU 2586 How far away ? (LCA)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 LCA模版题. RMQ+LCA: #include <iostream> #incl ...

  8. Hdu 2586 树链剖分求LCA

    Code: #include<cstdio> #include<cstring> #include<vector> #include<algorithm> ...

  9. hdu 2586 How far away? (LCA模板)

    题意: N个点,形成一棵树,边有长度. M个询问,每个询问(a,b),询问a和b的距离 思路: 模板题,看代码.DFS预处理算出每个结点离根结点的距离. 注意: qhead[maxn],而不是qhea ...

随机推荐

  1. Android 多屏幕适配 dp和px的关系

    一直以来别人经常问我,android的多屏幕适配到底是怎么弄,我也不知道如何讲解清楚,或许自己也是挺迷糊. 以下得出的结论主要是结合官方文档进行分析的https://developer.android ...

  2. 【Python】Linux crontab定时任务配置方法(详解)

    CRONTAB概念/介绍 crontab命令用于设置周期性被执行的指令.该命令从标准输入设备读取指令,并将其存放于“crontab”文件中,以供之后读取和执行. cron 系统调度进程. 可以使用它在 ...

  3. hibernate笔记(二)

    目标: 关联映射(hibernate映射) 1. 集合映射 2. 一对多与多对一映射 (重点) 3. 多对多映射 4. inverse/lazy/cascade 1. 集合映射 开发流程: 需求分析/ ...

  4. poj 2155 Matrix (树状数组)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 16797   Accepted: 6312 Descripti ...

  5. OJ 列表

    维护一个 OJ 列表.(这件事似乎没啥意义?) AtCoder hihoCoder Codeforces DMOJ CodeChef CS Academy HackerRank HackerEarth ...

  6. [BZOJ3473][BZOJ3277]字符串

    [BZOJ3473][BZOJ3277]字符串 试题描述 给定 \(n\) 个字符串,询问每个字符串有多少子串(不包括空串)是所有 \(n\) 个字符串中至少 \(k\) 个字符串的子串? 输入 第一 ...

  7. [NOIP2018 TG D2T1]旅行

    题目大意:$NOIP\;TG\;D2T1$ 题解:一棵树的很简单,第一个点一定是$1$,只需要对每个节点,找最小的没有访问过的节点访问即可,我写的是$O(n\log_2n)$. 考虑基环树的部分,一个 ...

  8. Spring源码解析-事件

    Spring事件的组件 主要是3个组件: 1.ApplicationEvent   事件 2.ApplicationListener 监听器,对事件进行监听 3.ApplicationEventMul ...

  9. ng websocket

    ng使用websocket 1.安装依赖库npm install ws --save 2.安装类型定义文件 npm install @types/ws --save 3.编写服务 import { I ...

  10. ViewData和ViewBag的那些事

    既然结论是“共享着相同的数据”,那我们就证实一下吧. 看来结论是正确的. 去查看定义,发现他们的类型是不一样的,ViewData是ViewDataDictionary,ViewBag是dynamic. ...