最近公共祖先问题(ST算法)

推荐几篇讲解该问题比较详细的博客点击打开链接 点击打开链接  点击打开链接 

本篇主要讲解如何采用解决RMQ问题的ST算法来解决LCA问题

步骤如下:

1 DFS遍历

DFS序用ver[maxn]保存(包含回溯时经过的点)

用first[maxn]保存每一个点第一次被访问时候的位置(即ver值)

用depth[maxn]保存每一个点的深度(包括回溯时经过的点)

check[maxn]标记一个点是否被访问过

dis[maxn]保存每一个点到根节点的距离

2 ST算法

Min[maxn][maxm]保存区间使得depth[x]最小的x的值

3 RMQ(访问)

访问得到相应区间使得depth[x]最小的x的值

4 LCA求出最近公共祖先

两个点的LCA一定是两个点在DFS序中(第一次出现)出现的位置之间深度最小的那个点

5 求出两点间的距离

dis[u]+dis[v]-2*dis[lca]

附上一道裸的LCA问题 hdu2586点击打开链接

How far away ?

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


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
 

AC代码:

#include<iostream>
#include<cmath>
#include<stdio.h>
#include<string.h>
#define maxn 40005
using namespace std;
int t;
int n,m;
int a,b,c;
struct node
{
    int s;
    int e;
    int w;
    int next;
}edge[maxn*2];
int head[maxn];
bool check[maxn];
int ver[maxn*2];//储存DFS序
int tot;//访问序号
int first[maxn];//每一个点第一次被访问的时候的位置
int dis[maxn];//每一点到根结点的距离
int depth[maxn*2];//第X位置的点的深度
int Min[maxn*2][18];
void add(int s,int e,int w,int &k)
{
    edge[k].s=s;
    edge[k].e=e;
    edge[k].w=w;
    edge[k].next=head[s];
    head[s]=k++;
    swap(s,e);
    edge[k].s=s;
    edge[k].e=e;
    edge[k].w=w;
    edge[k].next=head[s];
    head[s]=k++;
}
void dfs(int u,int dep)
{
    check[u]=true;
    ver[++tot]=u;
    first[u]=tot;
    depth[tot]=dep;
    for(int k=head[u];k!=-1;k=edge[k].next)
    {
        int v=edge[k].e;
        int w=edge[k].w;
        if(!check[v])
        {
        dis[v]=dis[u]+w;
        dfs(v,dep+1);
        ver[++tot]=u;
        depth[tot]=dep;
        }
    }
}
void ST(int len)
{
    for(int i=1;i<=len;i++)
        Min[i][0]=i;
    for(int i=1;(1<<i)<=len;i++)
    {
        for(int j=1;j+(1<<i)-1<=len;j++)
        {
            int one=Min[j][i-1];
            int two=Min[j+(1<<(i-1))][i-1];
            if(depth[one]<depth[two])
                Min[j][i]=one;
            else
                Min[j][i]=two;
        }
    }
}
int RMQ(int l,int r)
{
    int k=(int)(log((double)(r-l+1))/(log((double)2)));
    int one=Min[l][k];
    int two=Min[r-(1<<k)+1][k];
    if(depth[one]<=depth[two])
        return one;
    else
        return two;
}
int LCA(int l,int r)
{
    int x=first[l];
    int y=first[r];
    if(x>y)
        swap(x,y);
    if(x==y)
        return ver[x];
    int temp=RMQ(x,y);
    return ver[temp];
}
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        tot=0;
        dis[1]=0;
        memset(check,false,sizeof(check));
        memset(head,-1,sizeof(head));
        scanf("%d%d",&n,&m);
        int num=0;
        for(int i=1;i<n;i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            add(a,b,c,num);
        }
        dfs(1,1);
        ST(2*n-1);
        int q1,q2;
        while(m--)
        {
            scanf("%d%d",&q1,&q2);
            int temp=LCA(q1,q2);
            int ans=dis[q1]+dis[q2]-2*dis[temp];
            printf("%d\n",ans);
        }
    }
    return 0;
}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值