您的位置:首页 > 其它

HDU 2586 How far away ? LCA离线tarjan思想

2017-06-29 09:28 429 查看


How far away ?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 15746    Accepted Submission(s): 5982


Problem Description

There are n houses in the village and some bidirectional roads connecting them. Every day peole always like to ask like this "How far is it if I want to go from house A to house B"? Usually it hard to answer. But luckily int this village the answer is always
unique, since the roads are built in the way that there is a unique simple path("simple" means you can't visit a place twice) between every two houses. Yout task is to answer all these curious people.

 

Input

First line is a single integer T(T<=10), indicating the number of test cases.

  For each test case,in the first line there are two numbers n(2<=n<=40000) and m (1<=m<=200),the number of houses and the number of queries. The following n-1 lines each consisting three numbers i,j,k, separated bu a single space, meaning that there is a road
connecting house i and house j,with length k(0<k<=40000).The houses are labeled from 1 to n.

  Next m lines each has distinct integers i and j, you areato answer the distance between house i and house j.

 

Output

For each test case,output m lines. Each line represents the answer of the query. Output a bland line after each test case.

 

Sample Input

2
3 2
1 2 10
3 1 15
1 2
2 3

2 2
1 2 100
1 2
2 1

 

Sample Output

10
25
100
100

解题思路:

1,输入的图和查询的点都告诉你了,离线算法就可以做了

2,tarjan经常用于LCA的离线算法,他本来是一个求联通分量的算法

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
const int maxn = 40005 ;
struct Edge {
int form,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] ;
bool vis[maxn] ;
int len[maxn] ;
int find(int x){
return x==f[x]?x:f[x]=find(f[x]);
}
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]){///这里给双向边都存了结果,得益于神奇的^1运算
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() {
int t ;
scanf("%d",&t) ;
while(t--) {
memset(G,0,sizeof(G)) ;
memset(G2,0,sizeof(G2)) ;
memset(len,0,sizeof(len)) ;
memset(vis,false,sizeof(vis)) ;
edges.clear() ;
edges2.clear() ;

int n,m;
scanf("%d%d",&n,&m) ;
for(int i=0; i<n-1; i++) {
int u,v,w ;
scanf("%d%d%d",&u,&v,&w) ;
add_edge(u,v,w) ;
add_edge(v,u,w) ;
}
for(int i=0; i<m; i++) {
int u,v ;
scanf("%d%d",&u,&v) ;
add_edge2(u,v) ;
add_edge2(v,u) ;
}
LCA(1) ;
for(int i=0; i<m; i++) {
printf("%d\n",edges2[2*i].dist) ;
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: