您的位置:首页 > 其它

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

2016-08-01 18:31 363 查看

How far away ?

[b]Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 12461    Accepted Submission(s): 4596
[/b]

[align=left]Problem Description[/align]
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.
 

[align=left]Input[/align]
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.
 

[align=left]Output[/align]
For each test case,output m lines. Each line represents the answer of the query. Output a bland line after each test case.
 

[align=left]Sample Input[/align]

2
3 2
1 2 10
3 1 15
1 2
2 3

2 2
1 2 100
1 2
2 1

 

[align=left]Sample Output[/align]

10
25
100
100

 

[align=left]Source[/align]
ECJTU 2009 Spring Contest

思路:给定一棵有根树,询问两个点之间的距离,比如(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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  LCA Tarjan