题目链接:

Distance Queries

Time Limit: 2000MS   Memory Limit: 30000K
Total Submissions: 11531   Accepted: 4068
Case Time Limit: 1000MS

Description

Farmer John's cows refused to run in his marathon since he chose a path much too long for their leisurely lifestyle. He therefore wants to find a path of a more reasonable length. The input to this problem consists of the same input as in "Navigation Nightmare",followed by a line containing a single integer K, followed by K "distance queries". Each distance query is a line of input containing two integers, giving the numbers of two farms between which FJ is interested in computing distance (measured in the length of the roads along the path between the two farms). Please answer FJ's distance queries as quickly as possible! 
 

Input

* Lines 1..1+M: Same format as "Navigation Nightmare"

* Line 2+M: A single integer, K. 1 <= K <= 10,000

* Lines 3+M..2+M+K: Each line corresponds to a distance query and contains the indices of two farms.

 

Output

* Lines 1..K: For each distance query, output on a single line an integer giving the appropriate distance. 
 

Sample Input

  1. 7 6
  2. 1 6 13 E
  3. 6 3 9 E
  4. 3 5 7 S
  5. 4 1 3 N
  6. 2 4 20 W
  7. 4 7 2 S
  8. 3
  9. 1 6
  10. 1 4
  11. 2 6

Sample Output

  1. 13
  2. 3
  3. 36
  4.  
  5. 题意:
  6.  
  7. 给一棵树;问两点之间的距离;
  8.  
  9. 思路:
  10.  
  11. lca的模板题,一开始莫名其妙的wa了,看了好久才发现是一个地方写反了;后来又tle,把cin改成scanf就好了;看来还是不能用cin;
  12.  
  13. AC代码:
  1. /*2014300227 1986 Accepted 13984K 485MS G++ 2077B*/
  2. #include <iostream>
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <cmath>
  6. #include <algorithm>
  7. using namespace std;
  8. const int N=4e4+;
  9. typedef long long ll;
  10. const double PI=acos(-1.0);
  11. int n,m,cnt,head[N],vis[N],a[*N],dep[N],first[N],dis[N],num,dp[*N][],p[N];
  12.  
  13. struct Edge
  14. {
  15. int to,next,val;
  16. };
  17. Edge edge[*N];
  18. void add_edge(int s,int e,int va)
  19. {
  20. edge[cnt].to=e;
  21. edge[cnt].next=head[s];
  22. edge[cnt].val=va;
  23. head[s]=cnt++;
  24. }
  25. int dfs(int x,int deep)
  26. {
  27. vis[x]=;
  28. first[x]=num;
  29. a[num++]=x;
  30. dep[x]=deep;
  31. for(int i=head[x];i!=-;i=edge[i].next)
  32. {
  33. int y=edge[i].to;
  34. if(!vis[y])
  35. {
  36. dis[y]=dis[x]+edge[i].val;
  37. dfs(y,deep+);
  38. a[num++]=x;
  39. }
  40. }
  41. }
  42. int RMQ()
  43. {
  44. for(int i=;i<num;i++)
  45. {
  46. dp[i][]=a[i];
  47. }
  48. for(int j=;(<<j)<=num;j++)
  49. {
  50. for(int i=;i+(<<j)-<num;i++)
  51. {
  52. if(dep[dp[i][j-]]<dep[dp[i+(<<(j-))][j-]])dp[i][j]=dp[i][j-];
  53. else dp[i][j]=dp[i+(<<(j-))][j-];
  54. }
  55. }
  56. }
  57. int query(int l,int r)
  58. {
  59. int temp=(int)(log((r-l+)*1.0)/log(2.0));
  60. if(dep[dp[l][temp]]<dep[dp[r-(<<temp)+][temp]])return dp[l][temp];
  61. else return dp[r-(<<temp)+][temp];
  62. }
  63. int main()
  64. {
  65. while(scanf("%d%d",&n,&m)!=EOF)
  66. {
  67. cnt=;
  68. num=;
  69. for(int i=;i<=n;i++)
  70. {
  71. vis[i]=;
  72. head[i]=-;
  73. }
  74. int u,v,w;
  75. char s[];
  76. for(int i=;i<m;i++)
  77. {
  78. scanf("%d%d%d%s",&u,&v,&w,s);
  79. //cin>>u>>v>>w>>s;
  80. add_edge(u,v,w);
  81. add_edge(v,u,w);
  82. }
  83. dis[]=;
  84. dfs(,);
  85. RMQ();
  86. int q,fx,fy;
  87. scanf("%d",&q);
  88. while(q--)
  89. {
  90. scanf("%d%d",&fx,&fy);
  91. int lca;
  92. if(first[fx]<first[fy])
  93. {
  94. lca=query(first[fx],first[fy]);
  95. }
  96. else lca=query(first[fy],first[fx]);
  97. printf("%d\n",dis[fx]+dis[fy]-*dis[lca]);
  98. }
  99. }
  100. return ;
  101. }
  1.  

poj-1986 Distance Queries(lca+ST+dfs)的更多相关文章

  1. POJ.1986 Distance Queries ( LCA 倍增 )

    POJ.1986 Distance Queries ( LCA 倍增 ) 题意分析 给出一个N个点,M条边的信息(u,v,w),表示树上u-v有一条边,边权为w,接下来有k个询问,每个询问为(a,b) ...

  2. POJ 1986 Distance Queries LCA两点距离树

    标题来源:POJ 1986 Distance Queries 意甲冠军:给你一棵树 q第二次查询 每次你问两个点之间的距离 思路:对于2点 u v dis(u,v) = dis(root,u) + d ...

  3. poj 1986 Distance Queries LCA

    题目链接:http://poj.org/problem?id=1986 Farmer John's cows refused to run in his marathon since he chose ...

  4. POJ 1986 - Distance Queries - [LCA模板题][Tarjan-LCA算法]

    题目链接:http://poj.org/problem?id=1986 Description Farmer John's cows refused to run in his marathon si ...

  5. POJ 1986 Distance Queries(LCA Tarjan法)

    Distance Queries [题目链接]Distance Queries [题目类型]LCA Tarjan法 &题意: 输入n和m,表示n个点m条边,下面m行是边的信息,两端点和权,后面 ...

  6. POJ 1986 Distance Queries / UESTC 256 Distance Queries / CJOJ 1129 【USACO】距离咨询(最近公共祖先)

    POJ 1986 Distance Queries / UESTC 256 Distance Queries / CJOJ 1129 [USACO]距离咨询(最近公共祖先) Description F ...

  7. POJ 1986 Distance Queries 【输入YY && LCA(Tarjan离线)】

    任意门:http://poj.org/problem?id=1986 Distance Queries Time Limit: 2000MS   Memory Limit: 30000K Total ...

  8. POJ 1986 Distance Queries(Tarjan离线法求LCA)

    Distance Queries Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 12846   Accepted: 4552 ...

  9. poj 1986 Distance Queries 带权lca 模版题

    Distance Queries   Description Farmer John's cows refused to run in his marathon since he chose a pa ...

随机推荐

  1. C# 嵌入dll 动软代码生成器基础使用 系统缓存全解析 .NET开发中的事务处理大比拼 C#之数据类型学习 【基于EF Core的Code First模式的DotNetCore快速开发框架】完成对DB First代码生成的支持 基于EF Core的Code First模式的DotNetCore快速开发框架 【懒人有道】在asp.net core中实现程序集注入

    C# 嵌入dll   在很多时候我们在生成C#exe文件时,如果在工程里调用了dll文件时,那么如果不加以处理的话在生成的exe文件运行时需要连同这个dll一起转移,相比于一个单独干净的exe,这种形 ...

  2. 转:DDR中端接技术基本概念

    DDR中端接技术基本概念  版权声明:转载请注明出处:http://blog.csdn.net/lg2lh https://blog.csdn.net/lg2lh/article/details/90 ...

  3. Linux XMind

    XMind这个软件好像不错的样子,至少在Windows/Linux/Mac下都可以工作,作为FreeMind的替代品应该是没什么问题(还有一个vym貌似也可以,可能没有XMind好,毕竟XMind有公 ...

  4. 有一个直方图,用一个整数数组表示,其中每列的宽度为1,求所给直方图包含的最大矩形面积。比如,对于直方图[2,7,9,4],它所包含的最大矩形的面积为14(即[7,9]包涵的7x2的矩形)。给定一个直方图A及它的总宽度n,请返回最大矩形面积。保证直方图宽度小于等于500。保证结果在int范围内。

    // ConsoleApplication5.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<vector> ...

  5. python os模块 常用函数

    os.getcwd() 获取当前工作目录 os.listdir() 返回指定目录下的所有文件和目录 os.remove() 删除单个文件 os.path.split() 以元祖形式返回一个路径的目录和 ...

  6. asp.net 后台多线程异步处理时的 进度条实现一(Ajax+Ashx实现以及封装成控件的实现)

    (更新:有的同学说源代码不想看,说明也不想看,只想要一个demo,这边提供一下:http://url.cn/LPT50k (密码:TPHU)) 工作好长时间了,这期间许多功能也写成了不少的控件来使用, ...

  7. android 导入项目 项目中文字乱码问题

    乱码问题出现了几次,一直没有在意,今天又出现了,现总结如下: eclipse之所以会出现乱码问题是因为eclipse编辑器选择的编码规则是可变的.一般默认都是UTF-8或者GBK,当从外部导入的一个工 ...

  8. python scrapy爬虫框架

    http://scrapy-chs.readthedocs.io/zh_CN/0.24/intro/tutorial.html scrapy 提取html的标签内容 from scrapy.selec ...

  9. 【峰回路转】Excel技巧百例 08.计算两个日期的差值

    在Excel中假设高速计算两个日期之间的差? 比如A日期为:2012/3/12   B日期为:2015/7/29  那么这两个日期之间差几年,差几个月.差多少天? 我们使用DateDif 函数来处理. ...

  10. EntityFramework走马观花之CRUD(下)

    我在Entity Framework系列文章的CRUD上篇中介绍了EF的数据查询,中篇谈到了EF的数据更新,下篇则聊聊EF实现CRUD的内部原理. 跟踪实体对象状态 在CRUD上篇和中篇谈到,为了实现 ...