HDU 2586 How far away ?(lca)

How far away ?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8878    Accepted Submission(s): 3087


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
 
lca 离线模板

/* ***********************************************
Author :
Created Time :2015/8/12 13:31:52
File Name :6.cpp
************************************************ */
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 40000+10
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << 61;
const double eps=1e-5;
using namespace std;

bool cmp(int a,int b){
return a>b;
}
int fa[maxn],n,m,pre[maxn],l;
int dis[maxn];
int a[300][3];
vector<int>vec[maxn];
struct node{
int v,next,w;
}edge[2*maxn+100];
map<pair<int,int>,int>mp;
void add(int u,int v,int w){
edge[l].v=v;
edge[l].w=w;
edge[l].next=pre[u];
pre[u]=l++;
}
int findfa(int x){
if(x==fa[x])return x;
else return fa[x]=findfa(fa[x]);
}

void Union(int x,int y){
x=findfa(x);
y=findfa(y);
fa[y]=x;
}
void dfs(int u){
int v;
fa[u]=u;
for(int i=pre[u];i+1;i=edge[i].next){
v=edge[i].v;
if(fa[v]==-1){
dis[v]=edge[i].w+dis[u];
dfs(v);
Union(u,v);
}
}
for(int j=0;j<vec[u].size();j++){
v=vec[u][j];
if(fa[v]!=-1){
mp[make_pair(u,v)]=mp[make_pair(v,u)]=dis[v]+dis[u]-2*dis[findfa(v)];
}
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
int t;
cin>>t;
int x,y,z;
while(t--){
scanf("%d%d",&n,&m);
l=0;
mp.clear();

memset(fa,-1,sizeof fa);
memset(pre,-1,sizeof pre);

cle(dis);cle(a);
for(int i=0;i<=n;i++)vec[i].clear();

for(int i=1;i<n;i++){
scanf("%d %d %d",&x,&y,&z);
add(x,y,z);
add(y,x,z);
}
for(int i=1;i<=m;i++){
scanf("%d %d",&x,&y);
vec[x].push_back(y);
vec[y].push_back(x);
a[i][0]=x;
a[i][1]=y;
}
dfs(1);
for(int i=1;i<=m;i++){
printf("%d\n",mp[make_pair(a[i][0],a[i][1])]);
}
}
return 0;
}



你可能感兴趣的:(HDU 2586 How far away ?(lca))