您的位置:首页 > 其它

HDU2586 How far away ?(LCA求最近公共祖先)

2018-01-27 13:36 302 查看


How far away ?

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

Total Submission(s): 19650    Accepted Submission(s): 7704


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


23 21 2 103 1 151 22 32 21 2 1001 22 1

 

Sample Output


1025100100

 
用dis[i]表示节点i到根节点的距离,则a,b两点间距离等于dis[a]+dis[b]-2*dis[lca(a,b)];
LCA算法:
 常见的求最近公共祖先的算法是倍增算法。 
 首先对于每个结点先进行 DFS 预处理出它的深度,再记录下它们往父 亲方向走 2^0,2^1,2^2,··· ,2^k 步所到达的结点。在这里 2^k 大于整棵树的最 大深度。 
 预处理完后,需要查询两个点 u 和 v 的 LCA 的时候,先将 u 和 v 中深 度较大的一个利用先前处理出的数组走到和另一个结点相同的深度,这的所 需要的操作次数不会超过 log2|depth(u)−depth(v)|。
 接下来从 k 开始往下枚举,如果 u 和 v 如果往上走 2^i 后不相同,那么 就将它们一起往上走这么多步。 
 结束后如果 u 和 v 仍然不相等,再往上走一步。最后的顶点就是它们的 LCA。

#include <bits/stdc++.h>
using namespace std;
const int N = 40000 + 10;
struct Node{
int to,w;
};
int n,m;
int f
,deep
,dis
,p
[20];
//p[i][j]记录i节点往上走2^j步到达的节点,f[i]记录i的父节点,
//deep[i]记录i的深度,dis[i]记录i到根节点的距离
vector <Node> node
;

void dfs(int u,int pre){//首先对于每个结点先进行 DFS 预处理出它的深度
for (int i = 0;i < node[u].size();i++){
int v = node[u][i].to;
if (v != pre){
dis[v] = dis[u] + node[u][i].w;
deep[v] = deep[u] + 1;
f[v] = u;
dfs(v,u);
}
}
}

void init(){//记录下它们往父 亲方向走 2^0,2^1,2^2,··· ,2^k 步所到达的结点
for (int i = 1;i <= n;i++) p[i][0] = f[i];//向上走2^0到达父亲节点
for (int j = 1;(1 << j) <= n;j++){
for (int i = 1;i <= n;i++){
if (p[i][j-1] != -1){
p[i][j] = p[p[i][j-1]][j-1];//i走2^j到达的节点等于i走2^(j-1)到达的节点再走2^(j-1)
}
}
}
}

int lca(int a,int b){
if (deep[a] < deep[b]) swap(a,b);
int k = 0;
while ((1 << k) < deep[a]) k++;
k--;
for (int j = k;j >= 0;j--){//先将u和v中深度较大的一个利用先前处理出的数组走到和另一个结点相同的深度
if (deep[a] - (1 << j) >= deep[b]){
a = p[a][j];
}
}
if (a == b) return a;
for (int j = k;j >= 0;j--){//接下来从 k 开始往下枚举
if (p[a][j] != p[b][j]){//如果 u 和 v 如果往上走 2^i 后不相同,那么 就将它们一起往上走这么多步
a = p[a][j]; b=p[b][j];
}
}
return f[a];//结束后如果 u 和 v 仍然不相等,再往上走一步。最后的顶点就是它们的 LCA。
}

int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
memset(f,-1,sizeof(-1));
memset(p,-1,sizeof(-1));
for (int i = 0;i < n-1;i++){
int a,b,w;
scanf("%d%d%d",&a,&b,&w);
node[a].push_back(Node{b,w});
node[b].push_back(Node{a,w});
}
deep[1] = 0; dis[1] = 0;
dfs(1,-1);
init();
for (int i = 0;i < m;i++){
int a,b;
scanf("%d%d",&a,&b);
printf("%d\n",dis[a]+dis[b]-2*dis[lca(a,b)]);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: