首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

PAT甲级-1004 Counting Leaves (30分)

  • 24-03-03 00:23
  • 2236
  • 5445
blog.csdn.net

点击链接PAT甲级-AC全解汇总

题目:
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.

Input Specification:
Each input file contains one test case. Each case starts with a line containing 0

ID K ID[1] ID[2] … ID[K]

where ID is a two-digit number representing a given non-leaf node, K is the number of its children, followed by a sequence of two-digit ID’s of its children. For the sake of simplicity, let us fix the root ID to be 01.

The input ends with N being 0. That case must NOT be processed.

Output Specification:
For each test case, you are supposed to count those family members who have no child for every seniority level starting from the root. The numbers must be printed in a line, separated by a space, and there must be no extra space at the end of each line.

The sample case represents a tree with only 2 nodes, where 01 is the root and 02 is its only child. Hence on the root 01 level, there is 0 leaf node; and on the next level, there is 1 leaf node. Then we should output 0 1 in a line.

Sample Input:

2 1
01 1 02
  • 1
  • 2

Sample Output:

0 1
  • 1

题意:
第一行输入节点数(貌似没啥用) 和所有父节点数,也就是之后输入的行数
然后每行输入当前节点编号 他的子节点数 和一系列子节点
输出要求是输出每层有多少个叶节点

我的代码:

#include
using namespace std;

struct node{
    int level;//好像没什么用
    vector<int>sons;
}all_father[120];
int max_level=0;
int level_nonleaf[120]={0};

void DFS(int father,int level)
{
    all_father[father].level=level;
    if(all_father[father].sons.size()==0)
    {
        level_nonleaf[level]++;
        return;
    }
    level++;
    max_level=max(max_level,level);
    for(int i=0;i<all_father[father].sons.size();i++)
    {
        DFS(all_father[father].sons[i],level);
    }
}

int main()
{
    int N,M;
    cin>>N>>M;

    for(int i=0;i<M;i++)//M行,每行以父节点开头,它的子随后
    {
        int father_id,num_son;
        cin>>father_id>>num_son;
        for(int j=0;j<num_son;j++)
        {
            int son;
            cin>>son;
            all_father[father_id].sons.push_back(son);
        }
    }

    DFS(1,0);

    for(int i=0;i<=max_level;i++)
    {
        cout<<level_nonleaf[i];
        if(i!=max_level)cout<<" ";
    }

    return 0;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54

注意: 输入不一定是根节点开头,一开始做错了,仔细读题发现固定root的id为01,如果输入第一个父节点不是01的话,就没办法直接计算层号了,所以得先把所有数据输入之后在进行统计

如果输入是按照层输入的话,可以用下面的代码:

#include
using namespace std;

int main()
{
    int N,M;
    cin>>N>>M;

    int node_level[500]={0};//每个结点自己在第几层
    int level_nonleaf[500]={0};
    int max_level=1;

    for(int i=0;i<M;i++)//M行,每行以非叶结点开头,它的子随后
    {
        int id_father,num_son,level_son=2;//root,当作第一层
        cin>>id_father>>num_son;
        if(i!=0)//不是第一层,子节点都为(父+1)层
        {
            int level_father=node_level[id_father];
            level_nonleaf[level_father]--;//父层叶节点个数-1
            level_son=level_father+1;
        }

        max_level=level_son>max_level?level_son:max_level;//记录层数,方便输出
        level_nonleaf[level_son]+=num_son;//子层叶节点个数加上子数
        for(int j=0;j<num_son;j++)//k个子节点
        {
            int id_son;
            cin>>id_son;
            node_level[id_son]=level_son;
        }
    }

    //逐层输出多少个叶节点;
    for(int i=1;i<=max_level;i++)
    {
        cout<<level_nonleaf[i];
        if(i!=max_level)cout<<" ";
    }

    return 0;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
注:本文转载自blog.csdn.net的邂逅模拟卷的文章"https://blog.csdn.net/qq_34451909/article/details/105135549"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

101
推荐
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top