1470 Closest Common Ancestors //LCA

本文介绍了一个算法问题——最近公共祖先(Closest Common Ancestor),该问题要求在一个给定的树形结构中找到两个节点的最近公共祖先。文章提供了一种使用并查集实现的解决方案,通过合并节点来确定其最近公共祖先。

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

Closest Common Ancestors
Time Limit: 2000MS Memory Limit: 10000K
Total Submissions: 7027 Accepted: 2199

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

 

#include<stdio.h>
#include<vector>
using namespace std;
const int MAX=901;
int f[MAX];
int r[MAX];
int indegree[MAX];//保存每个节点的入度
int visit[MAX];
vector<int> tree[MAX],Qes[MAX];
int ancestor[MAX];
int sum[MAX];
void init(int n)
{
    for(int i=1;i<=n;i++)
    {
        r[i]=1;//类似于rank数组
        f[i]=i;//用于并查集记录父亲节点
        indegree[i]=0;//节点的层号
        visit[i]=0;//是否被访问
        ancestor[i]=0;//祖先
        tree[i].clear();//清空
        Qes[i].clear();
        sum[i]=0;
    }

}
int find(int n)
{  //查找函数,并压缩路径
    if(f[n]==n) return n;
    else f[n]=find(f[n]);
    return f[n];
}
int Union(int x,int y)
{ //合并函数,如果属于同一分支则返回0,成功合并返回1
    int a=find(x);
    int b=find(y);
    if(a==b) return 0;
    //相等的话,x向y合并
    else if(r[a]<=r[b])
    {
        f[a]=b;
        r[b]+=r[a];
    }
    else
    {
        f[b]=a;
        r[a]+=r[b];
    }
    return 1;
}
void LCA(int u)
{
    ancestor[u]=u;
    int size=tree[u].size();
    for(int i=0;i<size;i++)
    {
        LCA(tree[u][i]);
        Union(u,tree[u][i]);
        ancestor[find(u)]=u;
    }
    visit[u]=1;
    size=Qes[u].size();
    for(int i=0;i<size;i++)
    {   //如果已经访问了问题节点,就可以返回结果了.
        if(visit[Qes[u][i]]==1)
        {
            //printf("%d/n",ancestor[find(Qes[u][i])]);
            sum[ancestor[find(Qes[u][i])]]++;
            //return;
        }
    }
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        init(n);
        int s,t,num;
        char a,b,c;
        for(int i=1;i<=n;i++)
        {
            scanf(" %d%c%c%d%c ",&s,&a,&b,&num,&c);
            for(int j=1;j<=num;j++)
            {
                scanf("%d",&t);
                tree[s].push_back(t);
                indegree[t]++;
            }
        }
        scanf("%d ",&num);
        for(int i=1;i<=num;i++)
        {
           scanf(" %c %d %d %c",&a,&s,&t,&b);//这里可以输入多组询问
           Qes[s].push_back(t);//相当于询问两次
           Qes[t].push_back(s);
        }
        for(int i=1;i<=n;i++)
        {   //寻找根节点
            if(indegree[i]==0)
            {
                LCA(i);
                break;
            }
        }
        for(int i=1;i<=n;i++)
        if(sum[i]!=0)
        printf("%d:%d/n",i,sum[i]);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值