HDU 2586 How far away ? (LCA模板题 Tarjan算法求最小公共祖先)

How far away ?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 12461    Accepted Submission(s): 4596


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


思路:给定一棵有根树,询问两个点之间的距离,比如(a,b),先令根节点到每个点的距离为 dis[i] , LCA(a,b)为a,b的最小公共祖先,可以得出一个计算公式:

d = dis(a) + dis(b) - 2 *dis(LCA(a,b))。

关键就在于求两个点的最小公共祖先,这里用到离线的tarjan算法,就是先将询问的点对存起来,在DFS过程中若遇到点对(a,b)中的a,则判断vis[b]是否为1,即之前是否访问过b,若之前访问过b,且当前正在访问a,说明b已经完成回溯,回溯到了某个a,b最近的公共父节点,然后find_set(b)就是LCA(a,b)。

代码如下:

#include<cstdio>
#include<cstring>
#include<iostream>
#define MAXN 40005
using namespace std;
struct Edge
{
	int id, w, next;
}edge[2 * MAXN];//每条边都要双向记录 
int m;
int cnt;
int dis[MAXN];//root到各个点的距离 
bool vis[MAXN];
int father[MAXN];//并查集的祖先节点 
int head[MAXN];//每条边的头节点 
int x[MAXN], y[MAXN], lca[MAXN];//记录询问的两个点及这两个点的LCA(最近公共祖先) 
void addEdge(int u, int v, int c)//双向加边 
{
	edge[cnt].id = v; edge[cnt].w = c; edge[cnt].next = head[u]; head[u] = cnt++;
 	edge[cnt].id = u; edge[cnt].w = c; edge[cnt].next = head[v]; head[v] = cnt++;
}
int find_set(int x)//寻找祖先节点 
{
	return x == father[x] ? x : father[x] = find_set(father[x]);
}
void tarjan(int k)
{
	vis[k] = 1;//dfs过程中保证每个点只访问一次 
	father[k] = k;//初始化 
	for(int i = 1; i <= m; i++)
	{
		if(x[i] == k && vis[y[i]])//如果有一个询问时(a,b),a是现在正在访问的点, 
		lca[i] = find_set(y[i]); //b在之前已经访问过并回溯到某个祖先节点了,并且 
		if(y[i] == k && vis[x[i]])//b已经找到了祖先节点,father[b]已经赋值, 
		lca[i] = find_set(x[i]);//那么find_set(b)一定是a与b的最小公共祖先 
	}
	for(int i = head[k]; i != -1; i = edge[i].next)
	{
		if(!vis[edge[i].id])
		{
			dis[edge[i].id] = dis[k] + edge[i].w;//获得各个点到根的距离 
			tarjan(edge[i].id);
			father[edge[i].id] = k;//回溯并赋值 
		}
	}
} 
int main()
{
	int cases, i, j, n, u, v, c;
	scanf("%d", &cases);
	while(cases --)
	{
		cnt = 0;
		memset(head, -1, sizeof(head));//初始化 
		memset(vis, 0, sizeof(vis));//初始化 
		scanf("%d%d", &n, &m);
		for(i = 1; i <= n - 1; i++)
		{
			scanf("%d%d%d", &u, &v, &c);
			addEdge(u, v, c);
		}
		for(i = 1; i <= m; i++)
		{
			scanf("%d%d", &x[i], &y[i]);
		}
		dis[1] = 0;
		tarjan(1);
		for(i = 1; i <= m; i++)
		{
			printf("%d\n",dis[x[i]] + dis[y[i]] - 2 * dis[lca[i]]);
		}
	} 
	return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值