HDU 2586 How far away ?(LCA) 题解

题目来源:

http://acm.hdu.edu.cn/showproblem.php?pid=2586

题目描述:

 

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

ECJTU 2009 Spring Contest

Recommend

lcy

解题思路:

      题目大概就是给你一颗树,然后求两个点之间的最短距离,如果用lca求的话,就是求出a和b的最近公共祖先c,然后我们可以知道a和b的最短距离就是dis【a】+dis【b】-2*dis【c】,dis的点到根节点的距离,那么就是一题裸的lca问题了。。。

代码:

#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <iomanip>
const int Maxn=40010;
using namespace std;
struct newt1{
	int to,next,val;
}edge[2*Maxn];
struct new2{
	int id,to,next;
}query[405];
int n,m,dis[Maxn],head_e[Maxn],head_q[Maxn],father[Maxn],ans[405],vis[Maxn],cnte,cntq;
void init()
{
	for(int i=1;i<=n;i++)
	{
		father[i]=i;
	}
	memset(dis,0,sizeof(dis));
	memset(head_e,-1,sizeof(head_e));
	memset(head_q,-1,sizeof(head_q));
	memset(vis,0,sizeof(vis));
	cnte=cntq=0;
}
int fi(int x)
{
	if(x==father[x])return x;
	return father[x]=fi(father[x]);
}
void Union(int x,int y)
{
	int u=fi(x),v=fi(y);
	if(u==v)return ;
	father[y]=x;
}
void addedge(int u,int v,int w)
{
	edge[cnte].val=w;
	edge[cnte].to=v;
	edge[cnte].next=head_e[u];
	head_e[u]=cnte++;
}
void addquery(int u,int v,int id)
{
	query[cntq].id=id;
	query[cntq].to=v;
	query[cntq].next=head_q[u];
	head_q[u]=cntq++;
}
void tarjan(int u)
{
	vis[u]=1;
	for(int i=head_e[u];i!=-1;i=edge[i].next){
		int v=edge[i].to;
		if(!vis[v]){
			dis[v]=dis[u]+edge[i].val;
			tarjan(v);
			father[v]=u;
		}
	}
	for(int i=head_q[u];i!=-1;i=query[i].next){
		int v=query[i].to;
		if(vis[v]){
			int z=fi(v);
			ans[query[i].id]=dis[u]-dis[z]-dis[z]+dis[v];
		}
	}
}
int main()
{
	ios::sync_with_stdio(false);
	int t;
	cin>>t;
	while(t--)
	{
		
		cin>>n>>m;
		init();
		for(int i=1;i<=n-1;i++)
		{
			int a,b,c;
			cin>>a>>b>>c;
			addedge(a,b,c);
			addedge(b,a,c);
		}
		for(int i=1;i<=m;i++)
		{
			int a,b;
			cin>>a>>b;
			addquery(a,b,i);
			addquery(b,a,i);
		}
		dis[1]=0;
		tarjan(1);
		for(int i=1;i<=m;i++)
		cout<<ans[i]<<endl;
		
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40400202/article/details/81198393