考点:
离线Tarjan求LCA
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
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
思路点拨:
给出离线Tarjan求LCA算法模板
int find(int x)
{
if(x!=fa[x])
{
fa[x] = find(fa[x]) ;
}
return fa[x] ;
}
void tarjan(int u) // 使用的时候任意一点开始搜索,因为是无向图
{
st[u] = 1 ;
for(int i = h[u] ; ~i ; i = ne[i])
{
int j = e[i] ;
if(!st[j])
{
dis[j] = dis[u] + w[i] ;
tarjan(j) ;
fa[j] = u ;
}
}
for(int i = 0 ; i < query[u].size() ; i ++)
{
int j = query[u][i] ;
int id = query_id[u][i] ;
if(st[j])
{
int lca = find(j) ;
ans[id] = dis[u]+dis[j]-2*dis[lca] ;
}
}
}
离线Tarjan利用了并查集的优越性,但是比较累赘的是他需要先记录题目给出的所有问题,在此基础上进行深搜并得出答案,这是我不是太喜欢这个算法的原因。
但也没有办法~背吧
参考文献
算法训练营
C++ 代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std ;
const int N = 40010 ,M = 2*N ;
const int K = 10010 ;
vector<vector<int> > query(N) ;
vector<vector<int> > query_id(N) ;
int h[N] , ne[M] , e[M] , w[M] ;
int idx ;
int n , m ;
int ans[K] ;
int dis[N] ;
int fa[N] ,st[N] ;
int find(int x)
{
if(x != fa[x])
{
fa[x] = find(fa[x]) ;
}
return fa[x] ;
}
void init()
{
memset(h,-1,sizeof h) ;
memset(fa, 0 ,sizeof fa) ;
memset(dis , 0 , sizeof dis) ;
memset(ans , 0 , sizeof ans) ;
memset(st , 0 , sizeof st) ;
query.clear() ;
query_id.clear() ;
}
void add(int a, int b ,int c)
{
e[idx] = b , w[idx] = c , ne[idx] = h[a] , h[a] = idx ++ ;
}
void lca(int u)
{
fa[u] = u ;
st[u] = 1 ;
for(int i = h[u] ; ~i ; i = ne[i])
{
int j = e[i] ;
if(!st[j])
{
dis[j] = dis[u] + w[i] ;
lca(j) ;
fa[j] = u ;
}
}
for(int i = 0 ; i < query[u].size() ; i ++ )
{
int j = query[u][i] ;
int id = query_id[u][i] ;
if(st[j])
{
int lca = find(j) ;
ans[id] = dis[u] + dis[j] - 2* dis[lca] ;
}
}
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
init() ;
while(m--)
{
int a , b , c ;
char op[2] ;
scanf("%d%d%d%s" , &a,&b,&c,op) ;
if(a>b) swap(a,b) ;
add(a,b,c) ;
add(b,a,c) ;
int pa = find(a) , pb = find(b) ;
if(pa!=pb)
{
fa[pa] = pb ;
}
}
// 记录询问
int k ;
scanf("%d" , &k) ;
for(int i = 0 ; i < k ; i ++)
{
int a , b ;
scanf("%d%d",&a,&b) ;
query[a].push_back(b) ;
query[b].push_back(a) ;
query_id[a].push_back(i) ;
query_id[b].push_back(i) ;
}
lca(1) ;
for(int i = 0 ; i < k; i ++)
{
printf("%d\n" , ans[i]) ;
}
}
return 0 ;
}