HDU 2586 How far away(倍增法)

                                                                                                      How far away ?
                                                                                 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
                                                                                  Total Submission(s): 18063    Accepted Submission(s): 7004

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 

这一道题目意思是说,村庄之间有路可达,给你N个节点,N-1条路,然后M组查询,查询两个节点之间的距离。

N个节点N-1条边,那么就符合树的定义。所以题目给的就是一个树,就是求树上两个节点的距离。


这一道题目可以和LCA联系起来,求两个节点(a和b)的距离。两个节点必然由一个公共点连接起来,这个点就是LCA(最近公共祖先c)

那么求距离就可以转换为a到根节点的距离+b到根节点的距离—c到根节点的距离—c到根节点的距离。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<functional>
#define N 100000+10
using namespace std;
int n,m;
struct node
{
	int to,next,cost;
}e[N];
int cnt;
int fa[20][N];
int head[N],depth[N],dis[N];
void init()
{
	memset(head,-1,sizeof head);
	memset(depth,0,sizeof depth);
	memset(dis,0,sizeof dis);
	cnt=0;
}
void addedge(int u,int v,int w)//建图过程,建双向边
{
	e[cnt].to=v;
	e[cnt].cost=w;
	e[cnt].next=head[u];
	head[u]=cnt++;
}
void DFS(int u,int f)//遍历树
{
	fa[0][u]=f;
	for(int i=head[u];~i;i=e[i].next)//遍历所有相连的边
	{
		int To=e[i].to;
		if(To!=f)//去掉以后MLE,可能是递归求的过程中太多临时变量
		{//建树过程建双向边,会出现to=f的情况,去掉以后会陷入无限递归中
			dis[To]=dis[u]+e[i].cost;//更新距离
			depth[To]=depth[u]+1;//更新深度
			DFS(To,u);
		}
	}

}
void solve()
{
	depth[1]=1;//题目给的是一个树
	dis[1]=0;//无论怎么样的树,都可以把1视为根节点
	DFS(1,-1);
	for(int i=1;i<20;i++)//树上倍增
		for(int j=1;j<=n;j++)
			fa[i][j]=fa[i-1][fa[i-1][j] ];
}
int LCA(int u,int v)//求最近公共祖先
{
	if(depth[u]>depth[v])//保证V的深度比较大
		swap(u,v);
	for(int i=0;i<20;i++)//倍增到深度相同
		if((depth[v]-depth[u])>>i&1)//二进制特性,一定能跳到深度相同
			v=fa[i][v];
	if(u==v)
		return u;
	for(int i=19;i>=0;i--)//两者同时倍增
	{
		if(fa[i][u]!=fa[i][v])
		{
			u=fa[i][u];
			v=fa[i][v];
		}
	}
	return fa[0][v];
}
int main()
{
	int i,t;
	int a,b,c;
	while(scanf("%d",&t)!=EOF)
	{

		while(t--)
		{
			init();
			scanf("%d%d",&n,&m);
			for(i=1;i<n;i++)
			{
				scanf("%d%d%d",&a,&b,&c);
				addedge(a,b,c);
				addedge(b,a,c);
			}
			solve();
			for(i=1;i<=m;i++)
			{
				scanf("%d%d",&a,&b);
				int ans=dis[a]+dis[b]-2*dis[LCA(a,b)];
				printf("%d\n",ans);
			}
		}
		return 0;
	}
}


全部评论

相关推荐

11-01 08:48
门头沟学院 C++
伤心的候选人在吵架:佬你不要的,能不能拿户口本证明过户给我。。球球了
点赞 评论 收藏
分享
吃不饱的肱二头肌很想退休:tnnd 我以为选妹子呢,亏我兴高采烈的冲进来😠
投递快手等公司10个岗位
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务