# HDU2586(How far away)

How far away

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. dfs深度遍历每个节点，记录每个节点的深度，特殊父节点，以及从根节点到达当前节点的距离
2. 预处理维护完三个重要参数之后，对每次的查询找到两点的LCA。
3. 现在问题就转化简单了，x，y两点的距离就转化为 x -> root + y -> root - 2*LCA - >root的距离
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <stack>
using namespace std;
const int maxn = 40005;
const int maxd = 20;
struct edge{
int from;
int to;
int w;
int next;
}e[maxn];
int dist[maxn];				//记录当前节点到根节点的距离
int f[maxn][maxd];			//预处理表
int dep[maxn];				//节点深度
int in[maxn];				//节点入度，为了找到根
int tot;
{
e[tot].from = x;
e[tot].to = y;
e[tot].w = z;
}
void clear_set()
{
tot = 0;
memset(dist,0,sizeof(dist));
memset(f,0,sizeof(f));
memset(dep,0,sizeof(dep));
memset(in,0,sizeof(in));
}
void dfs(int x,int fx)
{
dep[x] = dep[fx] + 1;
f[x][0] = fx;
for(int i = 1;i < maxd;i++){
f[x][i] = f[f[x][i-1]][i-1];
}
for(int i = head[x];~i;i = e[i].next){
int y = e[i].to;
dist[y] = dist[x] + e[i].w;
dfs(y,x);
}
}
int LCA(int x,int y)
{
if(dep[x] < dep[y])		swap(x,y);
int d = dep[x] - dep[y];
for(int i = 0;i < maxd;i++){
if(((1<<i)&d)){
x = f[x][i];
}
}
if(x == y)		return x;
for(int i = maxd-1;i >= 0;i--){
if(f[x][i] != f[y][i]){
x = f[x][i];
y = f[y][i];
}
}
return f[x][0];
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
clear_set();
int n,m;
scanf("%d%d",&n,&m);
int x,y,z;
for(int i = 0;i < n-1;i++){
scanf("%d%d%d",&x,&y,&z);
in[y]++;
}
int rt = 0;
for(int i = 1;i <= n;i++){
if(in[i] == 0){
rt = i;
break;
}
}
dfs(rt,0);
while(m--){
scanf("%d%d",&x,&y);
int t = LCA(x,y);
int ans = dist[x] + dist[y] - 2*dist[t];
printf("%d\n",ans);
}
}
return 0;
}


10-09 1021

06-25 700

08-18 702

01-24 3382

06-06 334

04-12 617

08-15 261

11-25 983

03-06 75

04-18 23

02-26 551

08-17 4311

07-08 237

11-22 16

10-26 277

07-12 1109

10-18 400

10-15 167

02-25 1092

12-20 59

07-10 227

03-19 82万+

04-14 60万+

02-28 9359

03-01 14万+

03-08 7万+

04-25 7万+

03-10 13万+

03-10 19万+

#### MySQL数据库面试题（2020最新版）

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客