当前位置: 首页 > news >正文

【PAT甲级】1004 Counting Leaves

✍个人博客:https://blog.csdn.net/Newin2020?spm=1011.2415.3001.5343
📚专栏地址:PAT题解集合
📝原题地址:题目详情 - 1004 Counting Leaves (pintia.cn)
🔑中文翻译:数叶子结点
📣专栏定位:为想考甲级PAT的小伙伴整理常考算法题解,祝大家都能取得满分!
❤️如果有收获的话,欢迎点赞👍收藏📁,您的支持就是我创作的最大动力💪

1004 Counting Leaves

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<N<100, the number of nodes in a tree, and M (<N), the number of non-leaf nodes. Then M lines follow, each in the format:

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

Sample Output:

0 1

题意

第一行给定一个 N N N M M M ,分别表示该树的结点总数量以及非叶子结点数量。

接下来的 M M M 行,输入每个非叶子结点的孩子结点,格式为:

ID K ID[1] ID[2] ... ID[K]

ID 表示非叶子结点的编号, K K K 表示该结点有 K K K 个孩子结点,然后紧跟 K K K 个孩子结点的编号。

让我们统计该树中每一层的叶子结点个数,并输出。

如下图所示,cnt 表示每一层的叶子结点数:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dTa177FC-1663931950932)(PAT 甲级辅导.assets/2.png)]

思路

  1. 用链式前向星的方法存储该树。
  2. 递归更新每层叶子结点的数量,因为题目给定根结点是 1 ,所以从 1 开始向下递归,去递归它的孩子结点。如果在递归过程中 h[u] == -1 即结点 u 没有孩子结点,则将该层的叶子节点数加 1
  3. 最后输出每层的叶子结点数量。

代码

#include<bits/stdc++.h>
using namespace std;

const int N = 110;
int h[N], ne[N], e[N], idx;    //邻接表
int cnt[N];     //计算每一层的叶子结点数
int max_depth;  //树的最大深度

//链式前向星,往a向b加一条边
void add(int a, int b)
{
    e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}

//递归更新每层的叶子结点数量
void dfs(int u, int depth)
{
    //如果当前结点已经是叶子结点,则进行更新操作
    if (h[u] == -1)
    {
        max_depth = max(max_depth, depth);
        cnt[depth]++;
        return;
    }

    //递归遍历其孩子结点
    for (int i = h[u]; ~i; i = ne[i])
        dfs(e[i], depth + 1);
}

int main()
{
    int n, m;
    cin >> n >> m;

    //输入结点之间的关系
    memset(h, -1, sizeof h);
    while (m--)
    {
        int a, b, k;
        cin >> a >> k;
        while (k--)
        {
            cin >> b;
            add(a, b);
        }
    }

    dfs(1, 0);	//根结点为1

    //输出每层的叶子结点数
    cout << cnt[0];
    for (int i = 1; i <= max_depth; i++)
        cout << " " << cnt[i];
    cout << endl;

    return 0;
}

相关文章:

  • 【Vue】vue基础学习笔记
  • 边缘检测算子之间的优劣
  • 在软件测试摸爬滚打了8年,失业半年了。offer你在哪儿呀!
  • 15分钟了解sql注入(一) union注入
  • 基于混沌映射与差分进化自适应教与学优化算法-附代码
  • nginx基本使用一 ——————反向代理、负载均衡
  • 通讯录管理系统精解
  • 线上展厅表现形式 广州商迪
  • CDH 07Cloudera Manager freeIPA安装配置(markdown新版)
  • 22-09-23 西安 谷粒商城(05)CompletableFuture异步编排、nginx实现页面静态化
  • 【Javaweb】JSP标准标签库
  • (C语言)输入自定义个数的整数,打印出最大值和最小值
  • React受控组件与非受控组件详解
  • Rust(4): 字符串类型
  • [ 常用工具篇 ] POC-bomber 漏洞检测工具安装及使用详解
  • EOS是什么
  • es6(二):字符串的扩展
  • Java-详解HashMap
  • LeetCode刷题——29. Divide Two Integers(Part 1靠自己)
  • PaddlePaddle-GitHub的正确打开姿势
  • 给github项目添加CI badge
  • 解析带emoji和链接的聊天系统消息
  • 每个JavaScript开发人员应阅读的书【1】 - JavaScript: The Good Parts
  • 你真的知道 == 和 equals 的区别吗?
  • 如何利用MongoDB打造TOP榜小程序
  • 如何学习JavaEE,项目又该如何做?
  • 使用SAX解析XML
  • 算法系列——算法入门之递归分而治之思想的实现
  • 在Mac OS X上安装 Ruby运行环境
  • 分布式关系型数据库服务 DRDS 支持显示的 Prepare 及逻辑库锁功能等多项能力 ...
  • #基础#使用Jupyter进行Notebook的转换 .ipynb文件导出为.md文件
  • $Django python中使用redis, django中使用(封装了),redis开启事务(管道)
  • (1)Android开发优化---------UI优化
  • (16)Reactor的测试——响应式Spring的道法术器
  • (2/2) 为了理解 UWP 的启动流程,我从零开始创建了一个 UWP 程序
  • (编译到47%失败)to be deleted
  • (二)WCF的Binding模型
  • (附源码)spring boot基于Java的电影院售票与管理系统毕业设计 011449
  • (论文阅读11/100)Fast R-CNN
  • (免费领源码)python#django#mysql公交线路查询系统85021- 计算机毕业设计项目选题推荐
  • (牛客腾讯思维编程题)编码编码分组打印下标题目分析
  • (转)Groupon前传:从10个月的失败作品修改,1个月找到成功
  • (转)总结使用Unity 3D优化游戏运行性能的经验
  • ./indexer: error while loading shared libraries: libmysqlclient.so.18: cannot open shared object fil
  • .NET Micro Framework 4.2 beta 源码探析
  • .net开源工作流引擎ccflow表单数据返回值Pop分组模式和表格模式对比
  • .NET企业级应用架构设计系列之开场白
  • .NET使用存储过程实现对数据库的增删改查
  • @DependsOn:解析 Spring 中的依赖关系之艺术
  • [Angular 基础] - 自定义指令,深入学习 directive
  • [BUAA软工]第一次博客作业---阅读《构建之法》
  • [BZOJ1877][SDOI2009]晨跑[最大流+费用流]
  • [BZOJ4016][FJOI2014]最短路径树问题
  • [C/C++]_[初级]_[关于编译时出现有符号-无符号不匹配的警告-sizeof使用注意事项]
  • [C++随笔录] 红黑树