(LCA在线算法)Closest Common Ancestors POJ - 1470

本文详细介绍了一种离线LCA算法的实现过程,用于解决给定树结构中多个顶点对的最近公共祖先问题。文章首先介绍了算法的基本概念和输入输出格式,随后通过具体的示例说明了算法的工作原理,并提供了完整的C++代码实现。该算法适用于处理大量查询,能够有效地找到两个节点的最近公共祖先。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

Write a program that takes as input a rooted tree and a list of pairs of vertices. For each pair (u,v) the program determines the closest common ancestor of u and v in the tree. The closest common ancestor of two nodes u and v is the node w that is an ancestor of both u and v and has the greatest depth in the tree. A node can be its own ancestor (for example in Figure 1 the ancestors of node 2 are 2 and 5)

Input

The data set, which is read from a the std input, starts with the tree description, in the form: 

nr_of_vertices 
vertex:(nr_of_successors) successor1 successor2 ... successorn 
...
where vertices are represented as integers from 1 to n ( n <= 900 ). The tree description is followed by a list of pairs of vertices, in the form: 
nr_of_pairs 
(u v) (x y) ... 

The input file contents several data sets (at least one). 
Note that white-spaces (tabs, spaces and line breaks) can be used freely in the input.

Output

For each common ancestor the program prints the ancestor and the number of pair for which it is an ancestor. The results are printed on the standard output on separate lines, in to the ascending order of the vertices, in the format: ancestor:times 
For example, for the following tree: 

Sample Input

5
5:(3) 1 4 2
1:(0)
4:(0)
2:(1) 3
3:(0)
6
(1 5) (1 4) (4 2)
      (2 3)
(1 3) (4 3)

Sample Output

2:1
5:5

Hint

Huge input, scanf is recommended.

Source

Southeastern Europe 2000

 

1:如果使用离线LCA算法做这道题的话,查询数的最大值要定义为50W!
2:多组输入!
3:需要对根节点进行特判,因为第一个点不一定是根节点!
4:然后一直看大家都说输入有问题,输入直接在括号前面加上一个空格不就直接吸收缓冲区里面的回车了吗。
scanf(" (%d %d)",&x,&y);
5:别忘了清空数组,因为如果你一开始是用单组输入的话,改成组输入的时候确实很容易忘。
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
#include<cmath>
#include<map>
#include<string.h>
#include<string>
using namespace std;
const int maxn=20000;
const int DEG=20;
int head[maxn],tot,top;
int fa[maxn][DEG],depth[maxn];
int number;
bool flag[maxn];

struct Edge
{
    int to,next;
} edge[maxn*2];

struct xiao
{
    int u,w;
} mapp[maxn];

bool cmp(xiao a,xiao b)
{
    return a.u<b.u;
}

void addedge(int u,int v)
{
    edge[tot].to=v;
    edge[tot].next=head[u];
    head[u]=tot++;
}

void init()
{
    number=0;
    tot=0;
    memset(head,-1,sizeof(head));
    memset(flag,false,sizeof(flag));
    memset(depth,0,sizeof(depth));
    memset(fa,0,sizeof(fa));
}

void debug()
{
    cout<<"____5_____"<<endl;
}

void BFS(int root)
{
    queue<int>que;
    depth[root]=0;
    fa[root][0]=root;
    que.push(root);
    while(!que.empty())
    {
        int tmp=que.front();
        que.pop();
        for(int i=1; i<DEG; ++i)
        {
            fa[tmp][i]=fa[fa[tmp][i-1]][i-1];
        }
        for(int i=head[tmp]; i!=-1; i=edge[i].next)
        {
            int v=edge[i].to;
            if(v==fa[tmp][0])
                continue;
            depth[v]=depth[tmp]+1;
            fa[v][0]=tmp;
            que.push(v);
        }

    }
}

int LCA(int u,int v)
{
    if(depth[u]>depth[v])
    {
        int x=u;
        u=v;
        v=x;
    }
    int hu=depth[u],hv=depth[v];
    int tu=u,tv=v;
    for(int det=hv-hu,i=0; det; det>>=1,i++)
        if(det&1)
            tv=fa[tv][i];

    if(tu==tv)
        return tu;
    for(int i=DEG-1; i>=0; --i)
    {
        if(fa[tu][i]==fa[tv][i])
            continue;
        tu=fa[tu][i];
        tv=fa[tv][i];
    }

    return fa[tu][0];
}

int main()
{

    int n;
    while(cin>>n)
    {
        init();
        for(int i=1; i<=n; ++i)
        {
            int u,x,v;
            scanf("%d:(%d)",&u,&x);
            for(int k=1; k<=x; ++k)
            {
                scanf("%d",&v);
                addedge(u,v);
                addedge(v,u);
                flag[v]=true;
            }
        }
        int root;
        for(int i=1; i <=n ; ++i)
        {
            if(!flag[i])
            {
                root=i;
                break;
            }
        }
        BFS(root);
        map<int,int>mmp;
        int m,x,y;
        cin>>m;
        while(m--)
        {
            scanf(" (%d %d)",&x,&y);
            int mm=LCA(x,y);
            if(mmp[mm]==0)
            {
                mapp[number++].u=mm;
            }

            mmp[mm]++;

        }
        sort(mapp,mapp+number,cmp);
        for(int i=0; i<number; ++i)
        {
            printf("%d:%d\n",mapp[i].u,mmp[mapp[i].u]);
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值