题目传送门
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
题意
勇气小镇是一个有着n个房屋的小镇,为什么把它叫做勇气小镇呢,这个故事就要从勇气小镇成立的那天说起了,修建小镇的时候,为了让小镇有特色,镇长特地只修了n-1条路,并且规定说所有在勇气小镇的村民,每一次出门必须规划好路线,路线必须满足在到达终点之前绝对不走回头路。每个人都要这样,不然那个人就不配在小镇生活下去,因为他没有这个勇气。事实上,这并不能算一项挑战,因为n-1条路已经连通了每户人家,不回头地从起点到终点,只是一个时间上的问题。由于小镇上的福利特别好,所以小懒入住了这个小镇,他规划了m次的行程,每次从L房屋到R房屋,他想问你他每次从L房屋到R房屋需要走多远的路。
思路
题目给的是图看起来向一颗无向树,然后有m次询问每次问这两点的距离是多少。看起来好像可能做最短路,但是点太多了。复杂度都比较高,所以不能这么做,正确做法是树上差分。
- dfs深度遍历每个节点,记录每个节点的深度,特殊父节点,以及从根节点到达当前节点的距离
- 预处理维护完三个重要参数之后,对每次的查询找到两点的LCA。
- 现在问题就转化简单了,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 head[maxn];
int dep[maxn]; //节点深度
int in[maxn]; //节点入度,为了找到根
int tot;
void addedge(int x,int y,int z)
{
e[tot].from = x;
e[tot].to = y;
e[tot].w = z;
e[tot].next = head[x];
head[x] = tot++;
}
void clear_set()
{
tot = 0;
memset(dist,0,sizeof(dist));
memset(head,-1,sizeof(head));
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);
addedge(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;
}
愿你走出半生,归来仍是少年~