最近公共祖先问题(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  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
#include
#include
#include
#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<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







你可能感兴趣的:(C语言-ACM-算法-题解)