How far away ?(LCA)模板

本文介绍了一种求解树状结构中任意两点间最短路径的高效算法。通过深度优先搜索预处理节点深度信息,并利用RMQ算法快速查找最近公共祖先(LCA),进而计算两点间的距离。

How far away ?

http://acm.hdu.edu.cn/showproblem.php?pid=2586

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

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

 

 

Recommend

lcy   |   We have carefully selected several similar problems for you:  3486 2888 3234 2818 3038 

裸裸的裸题。就是求一棵树上的两点间的最短距离。

代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=80000+100;
const int maxm=80000+100;
int cnt,num,head[maxn];
int first[maxn],que[maxn];
int depth[maxn];
int dp[maxn][20];
int dist[maxn];
bool vis[maxn];
struct node{
    int to,next,val;
}G[maxm<<1];
void init(){
    memset(head,-1,sizeof(head));
    memset(vis,false,sizeof(vis));
    cnt=0;
    num=0;
}
void add(int a,int b,int c){
    G[++cnt].to=b;
    G[cnt].val=c;
    G[cnt].next=head[a];
    head[a]=cnt;
}
int dfs(int u,int deep){
    vis[u]=true;///表示是否访问过
    que[++num]=u;///num在此处相当于dfs_clock
    first[u]=num;///表示第一次走的编号
    depth[num]=deep;

    for(int i=head[u];i!=-1;i=G[i].next){
            int v=G[i].to;
            int w=G[i].val;
        if(!vis[v]){
            dist[v]=dist[u]+w;
            dfs(v,deep+1);
            que[++num]=u;
            depth[num]=deep;
        }
    }
}
void ST(int n)
{
    for(int i=1;i<=n;i++)
        dp[i][0] = i;
    for(int j=1;(1<<j)<=n;j++){
        for(int i=1;i+(1<<j)-1<=n;i++){
            int a = dp[i][j-1] , b = dp[i+(1<<(j-1))][j-1];
            dp[i][j] = depth[a]<depth[b]?a:b;
        }
    }
}
//两个节点之间的路径深度最小的就是LCA
int RMQ(int l,int r)
{
    int k=0;
    while((1<<(k+1))<=r-l+1)
        k++;
    int a = dp[l][k], b = dp[r-(1<<k)+1][k]; //保存的是编号
    return depth[a]<depth[b]?a:b;
}
int LCA(int u ,int v)
{
    int x = first[u] , y = first[v];
    if(x > y) swap(x,y);
    int res = RMQ(x,y);
    return que[res];
}

int main()
{
    int t,n,m,a,b,c;
    scanf("%d",&t);
    while(t--){
        init();
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n-1;i++){
            scanf("%d%d%d",&a,&b,&c);
            add(a,b,c);
            add(b,a,c);
        }
        dfs(1,1);
        ST(2*n-1);
        while(m--){
            scanf("%d%d",&a,&b);
            int lca=LCA(a,b);
            ///cout<<"lca:"<<lca<<endl;
            ///cout<<dist[a]<<"   "<<dist[b]<<"  "<<endl;
            printf("%d\n",dist[a]+dist[b]-2*dist[lca]);
        }
    }
    return 0;
}

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值