首页 > 其他 > 详细

poj 1986 Distance Queries(LCA)

时间:2015-09-17 21:21:07      阅读:385      评论:0      收藏:0      [点我收藏+]

Description

Farmer Johns 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 FJs 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
7 6
1 6 13 E
6 3 9 E
3 5 7 S
4 1 3 N
2 4 20 W
4 7 2 S
3
1 6
1 4
2 6

 

Sample Output

13
3
36

 

Hint

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

 

Source

 

题意:给一棵带权重的树,共有k个查询,每次查询树中2个结点的距离。结点数n最大为40000,k最大10000

分析:首先我们将无根树转为有根树,可以在O(n)时间内得到每个结点到根结点的距离。由于在树中从一个结点走到另一个结点的路径是唯一的,所以a到b的路径一定经过lca(a,b),设lca(a,b)=c。此时不难发现d(a,b)=d(a,root)+d(b,root)-2*d(c,root)。

这里用的是并查集的方法查找LCA,时间1000+ms,感觉有点慢

技术分享
 1 #pragma comment(linker, "/STACK:1024000000,1024000000")
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<math.h>
 7 #include<algorithm>
 8 #include<queue>
 9 #include<set>
10 #include<bitset>
11 #include<map>
12 #include<vector>
13 #include<stdlib.h>
14 #include <stack>
15 using namespace std;
16 #define PI acos(-1.0)
17 #define max(a,b) (a) > (b) ? (a) : (b)  
18 #define min(a,b) (a) < (b) ? (a) : (b)
19 #define ll long long
20 #define eps 1e-10
21 #define MOD 1000000007
22 #define N 200000
23 #define inf 1e12
24 vector<pair<int,int> >edge[N];
25 vector<pair<int,int> >que[N];
26 int n,m,q; 
27 int ans[N];
28 int dis[N];
29 int fa[N];
30 int vis[N];
31 int sum;
32 int find(int x){
33     return fa[x]==x?x:fa[x]=find(fa[x]);
34 }
35 void LCA(int u,int p){
36     fa[u]=u;
37     for(int i=0;i<edge[u].size();i++){
38         int v=edge[u][i].first;
39         if(v==p) continue;
40         dis[v]=dis[u]+edge[u][i].second;
41         LCA(v,u);
42         fa[v]=u;
43     }
44     vis[u]=1;
45     if(sum==q) return;
46     for(int i=0;i<que[u].size();i++){
47         int v=que[u][i].first;
48         if(vis[v]){
49             ans[que[u][i].second]=dis[u]+dis[v]-2*dis[find(v)];
50         }
51     }
52 }
53 int main()
54 {
55     while(scanf("%d%d",&n,&m)==2){
56         
57         for(int i=0;i<N;i++){
58             edge[i].clear();
59             que[i].clear();
60         }
61         sum=0;
62         memset(vis,0,sizeof(vis));
63         
64         char s[3];
65         for(int i=0;i<m;i++){
66             int a,b,c;
67             scanf("%d%d%d%s",&a,&b,&c,s);
68             edge[a].push_back(make_pair(b,c));
69             edge[b].push_back(make_pair(a,c));
70         }
71         
72         scanf("%d",&q);
73         for(int i=0;i<q;i++){
74             int x,y;
75             scanf("%d%d",&x,&y);
76             que[x].push_back(make_pair(y,i));
77             que[y].push_back(make_pair(x,i));
78             ans[i]=0;
79         }
80         dis[1]=0;
81         LCA(1,0);
82         
83         for(int i=0;i<q;i++){
84             printf("%d\n",ans[i]);
85         }
86     }
87     return 0;
88 }
View Code

 

poj 1986 Distance Queries(LCA)

原文:http://www.cnblogs.com/UniqueColor/p/4817607.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!