HDU2586 How far away ?(树上倍增求LCA)

题目链接

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

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

ECJTU 2009 Spring Contest

 

题意:在树上查找询问的两个节点u和v的距离。

思路:先把无根树转化为有根树,以1为根节点。dis[i]表示i节点距离根节点1的距离。

#include<bits/stdc++.h>
using namespace std;

const int N=40010;//节点的个数
const int M=20;//树最大可能深度

struct Node
{
    int v,w;
    Node(int a,int b):v(a),w(b){};
};
vector<Node>edge[N];//edge[u][i].v,从u点出发的第i条边为v
int fa[N][M],depth[N],dis[N],n,m;
//fa[i][j]:i节点向上推移2的j次方的节点
//depth[i]:i节点在树中的深度
//dis[i]:以1节点为根节点,从1节点到i节点的路径距离
void add_edge(int u,int v,int w)
{
    edge[u].push_back(Node(v,w));
}//添加边

void dfs(int u,int root,int d)
{
    int i;
    depth[u]=d;
    fa[u][0]=root;//初始化
    int sz=edge[u].size();
    for(i=0;i<sz;i++)
    {
        int v=edge[u][i].v;
        int w=edge[u][i].w;
        if(v!=root)
        {
            dis[v]=dis[u]+w;
            dfs(v,u,d+1);
        }
    }
}//搜索出各点的深度,以1为根节点,根节点的深度为0

void bz()
{
    int i,j;
    for(j=1;(1<<j)<=N;j++)
    {
        for(i=1;i<=n;i++)
        {
            fa[i][j]=fa[fa[i][j-1]][j-1];
        }
    }
}

int LCA(int u,int v)
{
    if(depth[u]<depth[v])
        swap(u,v);//保证深度较大的为u
    int dc=depth[u]-depth[v];
    int i;
    for(i=M-1;i>=0;i--)
        if((1<<i)&dc)
            u=fa[u][i];
    if(u==v)
        return u;//u移到了和v一个点,该点就是他们的最近公共祖先
    for(i=M-1;i>=0;i--)//u和v现在已经在一层,他们往上走几层之后祖先序列相同
    {//找到最小的不满足相同序列的点,
        if(fa[u][i]!=fa[v][i])
        {
            u=fa[u][i];
            v=fa[v][i];
        }
    }
    return fa[u][0];//最后,最近公共祖先为找到的点的祖先
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int i,j,x,y,z;
        scanf("%d%d",&n,&m);
        for(i=1;i<=n;i++)
            edge[i].clear();
        for(i=1;i<=n-1;i++)
        {
            scanf("%d%d%d",&x,&y,&z);
            add_edge(x,y,z);
            add_edge(y,x,z);
        }
        dis[1]=0;
        dfs(1,0,0);
        bz();
        while(m--)
        {
            scanf("%d%d",&x,&y);
            int ans=dis[x]+dis[y]-2*dis[LCA(x,y)];
            printf("%d\n",ans);
        }
    }
    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值