How far away ?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 15599 Accepted Submission(s): 5924
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.
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
Source
Recommend
思路:我是特意来补这道题的。。因为感觉这个算法挺实用的,好像很多时候都要用到。这是倍增LCA的裸题。思维类似RMQ,通过预处理出每个点的第2^n次方个父节点,从而达到logn查询任意两个节点的最近公共祖先。具体写法看代码吧。。。自己手写的留着当模板用。
#include<iostream>
#include<cmath>
#include<queue>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring>
#include<string>
#include<utility>
#include<map>
#include<vector>
#include<vector>
#define maxn 40005
#define inf 0x3f3f3f3f
using namespace std;
typedef long long LL;
const double eps = 1e-8;
const int mod = 1e9 + 7;
const int M = 20;
struct node{
int v, value, next;
}p[maxn << 1];
int len, head[maxn], dis[maxn], depth[maxn], n, m, father[maxn][25];
void addedge(int u, int v, int value){
p[len].v = v;
p[len].value = value;
p[len].next = head[u];
head[u] = len++;
}
void dfs(int x, int fa){
father[x][0] = fa;
for (int i = head[x]; ~i; i = p[i].next){
if (p[i].v == fa)
continue;
dis[p[i].v] = dis[x] + p[i].value;
depth[p[i].v] = depth[x] + 1;
dfs(p[i].v, x);
}
}
void presolve(){
dis[1] = depth[1] = 0;
dfs(1, 0);
for (int i = 1; i <= n; i++)
for (int j = 1; j < M; j++)
father[i][j] = father[father[i][j - 1]][j - 1];
}
int lca(int x, int y){
if (depth[x] != depth[y]){
if (depth[x] > depth[y])
swap(x, y);
int distant = depth[y] - depth[x];
for (int i = 0; i < M; i++){
if (distant&(1 << i))
y = father[y][i];
}
}
if (x == y)
return x;
for (int i = M; i >= 0; i--){
if (father[x][i] != father[y][i]){
x = father[x][i];
y = father[y][i];
}
}
return father[x][0];
}
int query(int x, int y){
return dis[x] + dis[y] - (dis[lca(x, y)] << 1);
}
int main(){
int t;
scanf("%d", &t);
while (t--){
len = 0;
memset(head, -1, sizeof(head));
scanf("%d%d", &n, &m);
for (int i = 0; i < n - 1; i++){
int u, v, value;
scanf("%d%d%d", &u, &v, &value);
addedge(u, v, value);
addedge(v, u, value);
}
presolve();
while (m--){
int x, y;
scanf("%d%d", &x, &y);
printf("%d\n", query(x, y));
}
}
}
本文介绍了一种解决树上两点间距离问题的有效算法——倍增LCA算法,并通过一个具体的编程题例进行实践讲解。该算法通过预处理每个点的第2^n次方个父节点,实现对任意两个节点的最近公共祖先查询。
428

被折叠的 条评论
为什么被折叠?



