HDU2586---How far away ?(lca算法)

Posted i am back

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU2586---How far away ?(lca算法)相关的知识,希望对你有一定的参考价值。

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

 


LCA_倍增是LCA的在线算法,时间和空间复杂度分别是O((n+q)log n)和O(n log n)。
对于这个算法,我们从最暴力的算法开始:
①如果a和b深度不同,先把深度调浅,使他变得和浅的那个一样
②现在已经保证了a和b的深度一样,所以我们只要把两个一起一步一步往上移动,直到他们到达同一个节点,也就是他们的最近公共祖先了。

 

/*
    author:gsw
    data:2018.04.30
    link:http://acm.hdu.edu.cn/showproblem.php?pid=2586
    account:tonysave
*/
#define ll long long
#define IO ios::sync_with_stdio(false);
#define maxn 40005

#include<vector>
#include<math.h>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int t,n,m;
class Node{
    public:
        int nex,len;
};
vector<Node> tree[maxn];
int dis[maxn];int deep[maxn];
int fa[maxn];

void init()
{
    for(int i=0;i<n;i++)
        tree[i].clear();
    memset(dis,0,sizeof(dis));
    memset(deep,0,sizeof(deep));
    memset(fa,0,sizeof(fa));
}
Node ne;
void dfs(int num,int faa)
{
    for(int i=0;i<tree[num].size();i++)
    {
        ne=tree[num][i];
        if(ne.nex!=faa)
        {
            fa[ne.nex]=num;
            deep[ne.nex]=deep[num]+1;
            dis[ne.nex]=dis[num]+ne.len;
            dfs(ne.nex,num);
        }
    }
}
int lca(int a,int b)
{
    if(deep[a]>deep[b])
        swap(a,b);
    while(deep[b]>deep[a])
        b=fa[b];
    while(a!=b)
        a=fa[a],b=fa[b];
    return a;
}

int main()
{
    int a,b,c;Node tem;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        init();
        for(int i=0;i<n-1;i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            tem.nex=b;tem.len=c;
            tree[a].push_back(tem);
            tem.nex=a;
            tree[b].push_back(tem);
        }
        dfs(1,0);
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&a,&b);
            int ans=lca(a,b);
            ans=dis[a]+dis[b]-2*dis[ans];
            printf("%d\n",ans);
        }
    }
}

 

以上是关于HDU2586---How far away ?(lca算法)的主要内容,如果未能解决你的问题,请参考以下文章

hdu 2586 How far away ?

HDU 2586 How far away ?

HDU2586---How far away ?(lca算法)

HDU-2586 How far away?

HDU-2586 How far away?

hdu——2586 How far away ?