G - Closest Common Ancestors

本文介绍了一个算法问题——求解树中节点的最近公共祖先,并提供了一种高效的解决方案。通过使用动态数组存储节点间的父子关系,文章实现了单次DFS遍历即可解决所有查询问题的方法。

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

G - Closest Common Ancestors
Time Limit:2000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u

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.

这道题感觉比A题要简单些,题意就是统计哪些点当了多少次祖先,然后依次输出,因为他的输入是按照某点及儿子一起输入的,所以可以用动态数组vector来存每个点的儿子,因为是统计,不需要一对一的输出,所以可以将需要查询的问题存在一个二维数组里,一次dfs后就可以完成所有的查询,还有一个很重要的就是,一定要用scanf,即使加了/*ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);*/这个cin也不能过。

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#define maxn 1000
using namespace std;
int n,num,cnt;
vector <int> anc[maxn];
bool isroot[maxn],vis[maxn];
int all[maxn],fa[maxn],que[1000][1000];
int Find(int a)
{
    if(a!=fa[a])
        a=Find(fa[a]);
    return a;
}
void dfs(int root)
{
    vis[root]=true;
    fa[root]=root;
    int len=anc[root].size();
    for(int i=0;i<len;i++)
    {
        int t=anc[root][i];
        dfs(t);
        fa[t]=root;
    }
    for(int i=1;i<=n;i++)
    {
        if(vis[i]&&que[root][i])
        {
            all[Find(i)]+=que[root][i];
            que[root][i]=0;                //查询后要把两个都清零,避免重复查询
            que[i][root]=0;
        }
    }
}
int main()
{
    /*ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);*/
    while(~scanf("%d",&n))
    {
        for(int i=0;i<n;i++)
        {
            anc[i].clear();
        }
    memset(isroot,true,sizeof(isroot));
    memset(all,0,sizeof(all));
    memset(vis,false,sizeof(vis));
    memset(que,0,sizeof(que));
    int a,b,c;
    for(int i=0;i<n;i++)
    {
        scanf("%d:(%d)",&a,&b);
        for(int j=0;j<b;j++)
        {
            scanf(" %d",&c);
            anc[a].push_back(c);
            isroot[c]=false;
        }
    }
    int q,u,v,root;
    scanf("%d",&q);
    for(int j=1;j<=n;j++)
    {
        if(isroot[j])
        {
            root=j;
            break;
        }
    }
    for(int i=0;i<q;i++)
    {
        scanf(" (%d %d)",&u,&v);
        que[u][v]++;                          //为了简单一点,可以不去查询哪个点所在的深度更深,可以正反都存起来
        que[v][u]++;
    }
    dfs(root);                      //dfs一定是从根节点开始查,所以要先找出根节点
    for(int i=1;i<=n;i++)
    {
        if(all[i])
            printf("%d:%d\n",i,all[i]);
    }
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值