Distance Queries
Time Limit: 2000MS   Memory Limit: 30000K
Total Submissions: 12846   Accepted: 4552
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

Hint

Farms 2 and 6 are 20+3+13=36 apart. 

题目连接:POJ 1986

简单模版题,一棵树中两点的距离$d(u,v)$可以用$d[u]+d[v]-2*d[lca(u,v)]$来求得,其中$d_i$是你设定的根到某一点$i$的距离,那显然首先随便找个点进行最短路或者直接DFS获得d数组,再Tarjan得出答案

代码:

  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <cstdlib>
  5. #include <sstream>
  6. #include <cstring>
  7. #include <bitset>
  8. #include <string>
  9. #include <deque>
  10. #include <stack>
  11. #include <cmath>
  12. #include <queue>
  13. #include <set>
  14. #include <map>
  15. using namespace std;
  16. #define INF 0x3f3f3f3f
  17. #define CLR(arr,val) memset(arr,val,sizeof(arr))
  18. #define LC(x) (x<<1)
  19. #define RC(x) ((x<<1)+1)
  20. #define MID(x,y) ((x+y)>>1)
  21. typedef pair<int,int> pii;
  22. typedef long long LL;
  23. const double PI=acos(-1.0);
  24. const int N=40010;
  25. struct edge
  26. {
  27. int to;
  28. int nxt;
  29. int w;
  30. };
  31. struct query
  32. {
  33. int to;
  34. int nxt;
  35. int id;
  36. };
  37.  
  38. edge E[N<<1];
  39. query Q[N<<1];
  40. int head[N],rhead[N],tot,rtot;
  41. int d[N],dx[N],vis[N],in[N];
  42. int pre[N],ances[N];
  43.  
  44. void init()
  45. {
  46. CLR(head,-1);
  47. CLR(rhead,-1);
  48. tot=rtot=0;
  49. CLR(d,0);
  50. for (int i=0; i<N; ++i)
  51. {
  52. pre[i]=i;
  53. ances[i]=0;
  54. }
  55. CLR(vis,0);
  56. CLR(in,0);
  57. CLR(dx,0);
  58. }
  59. int Find(int n)
  60. {
  61. if(pre[n]==n)
  62. return n;
  63. return pre[n]=Find(pre[n]);
  64. }
  65. inline void add(int s,int t,int d)
  66. {
  67. E[tot].to=t;
  68. E[tot].w=d;
  69. E[tot].nxt=head[s];
  70. head[s]=tot++;
  71. }
  72. inline void addquery(int s,int t,int id)
  73. {
  74. Q[rtot].id=id;
  75. Q[rtot].to=t;
  76. Q[rtot].nxt=rhead[s];
  77. rhead[s]=rtot++;
  78. }
  79. void LCA(int u)
  80. {
  81. vis[u]=1;
  82. ances[u]=u;
  83. int i,v;
  84. for (i=head[u]; ~i; i = E[i].nxt)
  85. {
  86. v = E[i].to;
  87. if(!vis[v])
  88. {
  89. LCA(v);
  90. pre[v]=u;
  91. ances[Find(u)]=u;
  92. }
  93. }
  94. for (i=rhead[u]; ~i; i = Q[i].nxt)
  95. {
  96. v=Q[i].to;
  97. if(vis[v])
  98. dx[Q[i].id]=d[u]+d[v]-(d[ances[Find(v)]]<<1);
  99. }
  100. }
  101. void dfs(int u,int fa,int sum)
  102. {
  103. d[u]=sum;
  104. for (int i=head[u]; ~i; i = E[i].nxt)
  105. {
  106. int v=E[i].to;
  107. if(v!=fa)
  108. dfs(v,u,sum+E[i].w);
  109. }
  110. }
  111. int main(void)
  112. {
  113. int n,m,a,b,c,i,k;
  114. char nouse[5];
  115. while (~scanf("%d%d",&n,&m))
  116. {
  117. init();
  118. for (i=0; i<m; ++i)
  119. {
  120. scanf("%d%d%d%s",&a,&b,&c,nouse);
  121. add(a,b,c);
  122. add(b,a,c);
  123. ++in[b];
  124. }
  125. scanf("%d",&k);
  126. for (i=0; i<k; ++i)
  127. {
  128. scanf("%d%d",&a,&b);
  129. addquery(a,b,i);
  130. addquery(b,a,i);
  131. }
  132. for (i=1; i<=n; ++i)
  133. {
  134. if(!in[i])
  135. {
  136. dfs(i,-1,0);
  137. LCA(i);
  138. break;
  139. }
  140. }
  141. for (i=0; i<k; ++i)
  142. printf("%d\n",dx[i]);
  143. }
  144. return 0;
  145. }

POJ 1986 Distance Queries(Tarjan离线法求LCA)的更多相关文章

  1. POJ - 1986 Distance Queries(离线Tarjan算法)

    1.一颗树中,给出a,b,求最近的距离.(我没考虑不联通的情况,即不是一颗树的情况) 2.用最近公共祖先来求, 记下根结点到任意一点的距离dis[],这样ans = dis[u] + dis[v] - ...

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

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

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

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

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

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

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

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

  6. POJ 1986 Distance Queries(LCA Tarjan法)

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

  7. POJ 1986 Distance Queries (Tarjan算法求最近公共祖先)

    题目链接 Description Farmer John's cows refused to run in his marathon since he chose a path much too lo ...

  8. POJ 1986 Distance Queries (最近公共祖先,tarjan)

    本题目输入格式同1984,这里的数据范围坑死我了!!!1984上的题目说边数m的范围40000,因为双向边,我开了80000+的大小,却RE.后来果断尝试下开了400000的大小,AC.题意:给出n个 ...

  9. poj 1986 Distance Queries LCA

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

随机推荐

  1. 简单的实现UIpicker上面的取消确定按钮

    1 因为我用的xib实现的添加picker 和textfiled的, @interface ViewController : UIViewController<UITextFieldDelega ...

  2. 百度地图API使用记录

    用户数据图层的总教程: 就是把用户数据存到LBS云里面,应用从云里面读数据 http://developer.baidu.com/map/jsdevelop-9.htm 上传数据的地方: http:/ ...

  3. BZOJ4584 : [Apio2016]赛艇

    首先将值域离散化成$O(n)$个连续段. 设$f[i][j][k]$表示第$i$个学校派出的数量在第$j$个连续段,在第$j$个连续段一共有$k$个学校的方案数.用组合数以及前缀和转移即可. 时间复杂 ...

  4. 使用Adobe Edge Inspect在各种设备中轻松测试同一页面

    有过移动网站开发经历的开发者都知道,在各种设备中测试同一页面是一项非常繁琐的工作.现在,我们可以使用Adobe Edge Inspect来简化这一工作.如果使用Edge Inspect,可以在各种设备 ...

  5. ACM 懒省事的小明

    懒省事的小明 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述       小明很想吃果子,正好果园果子熟了.在果园里,小明已经将所有的果子打了下来,而且按果子的不同种 ...

  6. ACM 变态最大值

    变态最大值 时间限制:1000 ms  |  内存限制:65535 KB 难度:1   描述 Yougth讲课的时候考察了一下求三个数最大值这个问题,没想到大家掌握的这么烂,幸好在他的帮助下大家算是解 ...

  7. 【CodeVS】p1299 切水果

    题目描述 Description 简单的说,一共N个水果排成一排,切M次,每次切[L,R]区间的所有水果(可能有的水果被重复切),每切完一次输出剩下水果数量 数据已重新装配,不会出现OLE错误 时限和 ...

  8. 【hdu】p1754I Hate It

    I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  9. Android -- 简单的图片浏览器

    1. 效果图

  10. NOI模拟赛Day3

    终于A题啦鼓掌~开心~ 开考看完题后,觉得第二题很好捏(傻叉上线 搞到十一点准备弃疗了然后突然发现我会做第一题 于是瞎码了码,就去准备饭票了... 好了,停止扯淡(就我一个我妹子每天不说话好难受QAQ ...