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

本文介绍了一种使用LCA算法解决特定图论问题的方法,即如何计算两点间的最短路径距离。通过预处理每个节点到其祖先节点的距离,可以高效地计算任意两点之间的距离。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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[N],deep[N],dis[N],p[N][20];
//p[i][j]记录i节点往上走2^j步到达的节点,f[i]记录i的父节点,
//deep[i]记录i的深度,dis[i]记录i到根节点的距离
vector <Node> node[N];

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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值