BNUOJ 2105 Distance Queries

Distance Queries

Time Limit: 2000ms

Memory Limit: 30000KB

This problem will be judged on PKU. Original ID: 1986
64-bit integer IO format: %lld      Java class name: Main

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

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

Source

USACO 2004 February

解题:LCA求树上任意两点间的距离。任意两点间只有一条路啊!不然那还是树么?^_^。。。。

LCA(a,b) = c所以d(a,b) = d(a,root)+d(b,root)-2*d(c,root);人字形?嘻嘻!傻逼。。。。。当时我居然不能明白这个

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <vector>
 6 #include <climits>
 7 #include <algorithm>
 8 #include <cmath>
 9 #define LL long long
10 #define INF 0x3f3f3f
11 using namespace std;
12 const int maxn = 100010;
13 struct arc{
14     int to,w;
15 };
16 struct query{
17     int to,id;
18 };
19 int n,m,k,ans[maxn],d[maxn],uf[maxn];
20 bool vis[maxn];
21 vector<arc>g[maxn];
22 vector<query>q[maxn];
23 int Find(int x){
24     if(x != uf[x])
25         uf[x] = Find(uf[x]);
26     return uf[x];
27 }
28 void tarjan(int u,int ds){
29     vis[u] = true;
30     d[u] = ds;
31     uf[u] = u;
32     int i;
33     for(i = 0; i < g[u].size(); i++){
34         if(!vis[g[u][i].to]) {tarjan(g[u][i].to,ds+g[u][i].w);uf[g[u][i].to] = u;}
35     }
36     for(i = 0; i < q[u].size(); i++)
37     if(vis[q[u][i].to]){
38         ans[q[u][i].id] = d[u]+d[q[u][i].to]-2*d[Find(q[u][i].to)];
39     }
40 }
41 int main(){
42     int i,j,u,v,w;
43     char ch;
44     while(~scanf("%d %d",&n,&m)){
45         for(i = 0; i <= n; i++){
46             g[i].clear();
47             q[i].clear();
48             d[i] = 0;
49             vis[i] = false;
50         }
51         for(i = 0; i < m; i++){
52             scanf("%d %d %d %c",&u,&v,&w,&ch);
53             g[u].push_back((arc){v,w});
54             g[v].push_back((arc){u,w});
55         }
56         scanf("%d",&k);
57         for(i = 0; i < k; i++){
58             scanf("%d %d",&u,&v);
59             q[u].push_back((query){v,i});
60             q[v].push_back((query){u,i});
61         }
62         tarjan(1,0);
63         for(i = 0; i < k; i++)
64             printf("%d\n",ans[i]);
65     }
66     return 0;
67 }

BNUOJ 2105 Distance Queries

时间: 2024-08-28 19:12:44

BNUOJ 2105 Distance Queries的相关文章

POJ 题目1986 Distance Queries(LCA 离线)

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

poj 1986 Distance Queries LCA

题目链接:http://poj.org/problem?id=1986 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

POJ1986 Distance Queries【最近公共祖先】【Tarjan-LCA算法】

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

POJ1986 Distance Queries (LCA)(倍增)

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

poj 1986 Distance Queries 带权lca 模版题

Distance Queries 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

poj-1986 Distance Queries(lca+ST+dfs)

题目链接: 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 l

POJ-1986 Distance Queries(LCA、离线)

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

POJ 1986 Distance Queries LCA两点距离树

标题来源:POJ 1986 Distance Queries 意甲冠军:给你一棵树 q第二次查询 每次你问两个点之间的距离 思路:对于2点 u v dis(u,v) = dis(root,u) + dis(root,v) - 2*dis(roor,LCA(u,v)) 求近期公共祖先和dis数组 #include <cstdio> #include <cstring> #include <vector> using namespace std; const int max

POJ——T 1986 Distance Queries

http://poj.org/problem?id=1986 Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 14383   Accepted: 5063 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 lei