hdu 2586 How far away ?

本文介绍了一种解决树上两点间距离问题的有效算法——倍增LCA算法,并通过一个具体的编程题例进行实践讲解。该算法通过预处理每个点的第2^n次方个父节点,实现对任意两个节点的最近公共祖先查询。


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.
 

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
lcy   |   We have carefully selected several similar problems for you:   3486  2874  2888  3234  2818 


题意:给你一棵树,每条边都有权值,m次询问,每次问任意两点之间的距离。

思路:我是特意来补这道题的。。因为感觉这个算法挺实用的,好像很多时候都要用到。这是倍增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));
		}
	}
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值