Distance Queries
Time Limit: 2000MS | Memory Limit: 30000K | |
Total Submissions: 13849 | Accepted: 4880 | |
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.
* 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
一个裸的LCA
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int maxn = 40000+10;
struct tree{
int to,next,w;
}Tree[maxn<<1];
struct query{
int to,next;
int id;
}Query[maxn<<1];
int vist[maxn],dist[maxn],fat[maxn],head_tree[maxn],head_query[maxn];
int cnt_tree,cnt_query;
int N,M,Q;
int res[maxn][3],Flag[maxn];
void Init(){
cnt_tree = cnt_query = 0;
for(int i = 1; i <= N; i++){
head_tree[i] = head_query[i] = -1;
dist[i] = vist[i] = Flag[i] = 0;
fat[i] = i;
}
}
int Find(int x){
if(x != fat[x])
x = Find(fat[x]);
return x;
}
void ADD_tree(int u,int v,int w){
Tree[cnt_tree].to = v;
Tree[cnt_tree].w = w;
Tree[cnt_tree].next = head_tree[u];
head_tree[u] = cnt_tree++;
}
void ADD_query(int u,int v,int id){
Query[cnt_query].to = v;
Query[cnt_query].id = id;
Query[cnt_query].next = head_query[u];
head_query[u] = cnt_query++;
}
void Tarjan(int u,int father){
for(int i = head_tree[u]; ~i; i = Tree[i].next){
int v = Tree[i].to;
if(!vist[v] && v!=father){
dist[v] = dist[u]+Tree[i].w;
Tarjan(v,u);
fat[v] = u;
}
}
vist[u] = 1;
for(int i = head_query[u]; ~i; i = Query[i].next){
int v = Query[i].to;
if(vist[v]){
res[Query[i].id][0] = dist[u];
res[Query[i].id][1] = dist[v];
int temp =Find(v);
res[Query[i].id][2] = dist[temp];
}
}
}
int main(){
int u,v,w;
char ch[5];
while(~scanf("%d %d",&N,&M)){
Init();
for(int i = 0; i < M; i++){
scanf("%d %d %d %s",&u,&v,&w,ch);
ADD_tree(u,v,w);
ADD_tree(v,u,w);
Flag[v] = 1;
}
scanf("%d",&Q);
for(int i = 0; i < Q; i++){
scanf("%d %d",&u,&v);
ADD_query(u,v,i);
ADD_query(v,u,i);
}
int pos;
for(int i = 1; i <= N; i++){
if(!Flag[i]){
pos = i;
break;
}
}
Tarjan(pos,-1);
for(int i = 0; i < Q; i++)
printf("%d\n",res[i][0]+res[i][1]-2*res[i][2]);
}
return 0;
}