Distance Queries
Time Limit: 2000MS | Memory Limit: 30000K | |
Total Submissions: 13792 | Accepted: 4870 | |
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.
解题思路:
1,输入的就是一个树,n个点m个边,后面的输入的字母没发现什么含义,可能就是迷惑人的,忽视掉。
2,LCA的tarjan算法,这个算法网上到处都有,//其实只看代码也看得懂,挺简单的思路
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
const int maxn = 100005 ;
int n,m;
struct Edge{
int from,to,dist ;
};
vector<Edge> edges ;
vector<int> G[maxn] ;
void add_edge(int from,int to,int dist){
edges.push_back((Edge){from,to,dist}) ;
int mm = edges.size() ;
G[from].push_back(mm-1) ;
}
vector<Edge> edges2 ;
vector<int> G2[maxn] ;
void add_edge2(int from,int to,int dist=0){
edges2.push_back((Edge){from,to,dist}) ;
int mm = edges2.size() ;
G2[from].push_back(mm-1) ;
}
int f[maxn] ;
int find(int x){
return x==f[x]?x:f[x]=find(f[x]) ;
}
bool vis[maxn] ;
int len[maxn] ;
void LCA(int u){
f[u] = u ;
vis[u] = true ;
for(int i=0;i<G[u].size();i++){
if(!vis[edges[G[u][i]].to]){
len[edges[G[u][i]].to] = len[u]+edges[G[u][i]].dist ;
LCA(edges[G[u][i]].to) ;
f[edges[G[u][i]].to] = u ;
}
}
for(int i=0;i<G2[u].size();i++){
if(vis[edges2[G2[u][i]].to]){
edges2[G2[u][i]^1].dist = edges2[G2[u][i]].dist = len[edges2[G2[u][i]].to]+len[u]-2*len[find(edges2[G2[u][i]].to)] ;
}
}
}
int main(){
while(~scanf("%d%d",&n,&m)){
memset(G,0,sizeof(G)) ;
memset(G2,0,sizeof(G2)) ;
memset(f,0,sizeof(f)) ;
memset(vis,false,sizeof(vis)) ;
memset(len,0,sizeof(len)) ;
edges.clear() ;
edges2.clear() ;
for(int i=0;i<n-1;i++){
int u,v,w;
char c[2];
scanf("%d%d%d%s",&u,&v,&w,c);
add_edge(u,v,w);
add_edge(v,u,w);
}
int k;
scanf("%d",&k) ;
for(int i=0;i<k;i++){
int u,v;
scanf("%d%d",&u,&v);
add_edge2(u,v);
add_edge2(v,u);
}
LCA(1) ;
for(int i=0;i<k;i++){
printf("%d\n",edges2[2*i].dist) ;
}
}
return 0;
}