POJ 1330 Nearest Common Ancestors【LCA】

本文探讨了如何在一个包含n个节点的树结构中,找出任意两个指定节点的最近公共祖先。通过层次递归与并查集的方法,实现了高效地计算两点间的祖先关系。实例展示了该算法在不同场景下的应用,包括边界条件和特殊情况的处理。

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

Nearest Common Ancestors

Time Limit: 1000MS

 

Memory Limit: 10000K

Total Submissions: 24210

 

Accepted: 12605

Description

A rooted tree is a well-known data structure in computer science and engineering. An example is shown below: 

 
In the figure, each node is labeled with an integer from {1, 2,...,16}. Node 8 is the root of the tree. Node x is an ancestor of node y if node x is in the path between the root and node y. For example, node 4 is an ancestor of node 16. Node 10 is also an ancestor of node 16. As a matter of fact, nodes 8, 4, 10, and 16 are the ancestors of node 16. Remember that a node is an ancestor of itself. Nodes 8, 4, 6, and 7 are the ancestors of node 7. A node x is called a common ancestor of two different nodes y and z if node x is an ancestor of node y and an ancestor of node z. Thus, nodes 8 and 4 are the common ancestors of nodes 16 and 7. A node x is called the nearest common ancestor of nodes y and z if x is a common ancestor of y and z and nearest to y and z among their common ancestors. Hence, the nearest common ancestor of nodes 16 and 7 is node 4. Node 4 is nearer to nodes 16 and 7 than node 8 is. 

For other examples, the nearest common ancestor of nodes 2 and 3 is node 10, the nearest common ancestor of nodes 6 and 13 is node 8, and the nearest common ancestor of nodes 4 and 12 is node 4. In the last example, if y is an ancestor of z, then the nearest common ancestor of y and z is y. 

Write a program that finds the nearest common ancestor of two distinct nodes in a tree. 

Input

The input consists of T test cases. The number of test cases (T) is given in the first line of the input file. Each test case starts with a line containing an integer N , the number of nodes in a tree, 2<=N<=10,000. The nodes are labeled with integers 1, 2,..., N. Each of the next N -1 lines contains a pair of integers that represent an edge --the first integer is the parent node of the second integer. Note that a tree with N nodes has exactly N - 1 edges. The last line of each test case contains two distinct integers whose nearest common ancestor is to be computed.

Output

Print exactly one line for each test case. The line should contain the integer that is the nearest common ancestor.

Sample Input

2

16

1 14

8 5

10 16

5 9

4 6

8 4

4 10

1 13

6 15

10 11

6 7

10 2

16 3

8 1

16 12

16 7

5

2 3

3 4

3 1

1 5

3 5

Sample Output

4

3

 

Source

Taejon 2002

 

题目大意:给出n个点,以及n-1条边,保证构成一颗树的情况下,求两个点的最近公共祖先。


思路:


1、首先在输入的时候,确定每个点的入度,入度为0的点必然是一颗树的根节点,因为LCA-tarjan算法是层次递归+并查集来实现的,所以我们要从跟节点向下递归。所以我们先确定每个节点的入度,找到根节点


2、然后从根节点开始层次递归,递归到的点首先都设定自己就是祖先,然后将其子节点依次遍历,向下Dfs,等待所有字节点构成的子树的子问题都解决了之后,再回到当前节点,设定所有子节点的祖先为自己,每次递归到的点,同时标记这个节点的LCA问题已经搞定了,vis【now】=1。


3、(设定要求的两个点的最近公共祖先的编号为u,v)如果递归到这样一种情况:递归到节点u的时候,v节点的LCA问题已解决(vis【v】==1),说明u和v属于同一个子树里边的两个点,并且v的祖先也是u的祖先。或者是遇到节点v的时候,u节点的LCA问题已解决(vis【u】==1),也能够说明u和v属于同一个子树里边的两个点,并且u的祖先也就是v的祖先。


因为点n比较大,直接用邻接矩阵是不现实的,这里用vector来存边。

AC代码:

#include<stdio.h>
#include<string.h>
#include<vector>
#include<algorithm>
using namespace std;
int degree[15000];
int vis[15000];
vector<int >mp[15000];
int f[15000];
int n,u,v,ok;
int find(int a)
{
    int r=a;
    while(f[r]!=r)
    r=f[r];
    int i=a;
    int j;
    while(i!=r)
    {
        j=f[i];
        f[i]=r;
        i=j;
    }
    return r;
}
void merge(int a,int b)
{
    int A,B;
    A=find(a);
    B=find(b);
    if(A!=B)
    f[B]=A;
}
void LCA(int now)
{
    if(ok==1)return ;
    vis[now]=1;
    f[now]=now;
    for(int i=0;i<mp[now].size();i++)
    {
        int to=mp[now][i];
        if(vis[to]==1)continue;
        LCA(to);
        merge(now,to);
    }
    if(ok==1)return ;
    if(now==u&&vis[v]==1)
    {
        ok=1;
        printf("%d\n",find(v));
        return ;
    }
    if(now==v&&vis[u]==1)
    {
        ok=1;
        printf("%d\n",find(u));
        return ;
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        memset(degree,0,sizeof(degree));
        for(int i=1;i<=n;i++)mp[i].clear();
        for(int i=1;i<=n;i++)f[i]=i;
        for(int i=1;i<=n;i++)vis[i]=0;
        for(int i=0;i<n-1;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            degree[y]++;
            mp[x].push_back(y);
        }
        scanf("%d%d",&u,&v);
        ok=0;
        for(int i=1;i<=n;i++)
        {
            if(degree[i]==0)
            {
                LCA(i);
                break;
            }
        }
    }
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值