【PAT甲级 - C++题解】1004 Counting Leaves

简介: 【PAT甲级 - C++题解】1004 Counting Leaves

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
• 1
• 2

Sample Output:

0 1

题意


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


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


ID K ID[1] ID[2] ... ID[K]ID 表示非叶子结点的编号,K KK 表示该结点有 K KK 个孩子结点,然后紧跟 K KK 个孩子结点的编号。


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


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


221be5f6f4654eaca2a4a30e86292d75.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;
}


目录
相关文章
|
C++
【PAT甲级 - C++题解】1040 Longest Symmetric String
【PAT甲级 - C++题解】1040 Longest Symmetric String
303 0
|
算法 C++
【PAT甲级 - C++题解】1044 Shopping in Mars
【PAT甲级 - C++题解】1044 Shopping in Mars
382 0
|
C++
【PAT甲级 - C++题解】1117 Eddington Number
【PAT甲级 - C++题解】1117 Eddington Number
363 0
|
存储 C++ 容器
【PAT甲级 - C++题解】1057 Stack
【PAT甲级 - C++题解】1057 Stack
331 0
|
存储 C++
【PAT甲级 - C++题解】1055 The World‘s Richest
【PAT甲级 - C++题解】1055 The World‘s Richest
257 0
|
C++
【PAT甲级 - C++题解】1051 Pop Sequence
【PAT甲级 - C++题解】1051 Pop Sequence
304 0
|
人工智能 BI C++
【PAT甲级 - C++题解】1148 Werewolf - Simple Version
【PAT甲级 - C++题解】1148 Werewolf - Simple Version
511 0
|
存储 定位技术 C++
【PAT甲级 - C++题解】1091 Acute Stroke
【PAT甲级 - C++题解】1091 Acute Stroke
255 0
|
编译器 C++ 开发者
【C++篇】深度解析类与对象(下)
在上一篇博客中,我们学习了C++的基础类与对象概念,包括类的定义、对象的使用和构造函数的作用。在这一篇,我们将深入探讨C++类的一些重要特性,如构造函数的高级用法、类型转换、static成员、友元、内部类、匿名对象,以及对象拷贝优化等。这些内容可以帮助你更好地理解和应用面向对象编程的核心理念,提升代码的健壮性、灵活性和可维护性。
|
编译器 C++ 容器
【c++11】c++11新特性(上)(列表初始化、右值引用和移动语义、类的新默认成员函数、lambda表达式)
C++11为C++带来了革命性变化,引入了列表初始化、右值引用、移动语义、类的新默认成员函数和lambda表达式等特性。列表初始化统一了对象初始化方式,initializer_list简化了容器多元素初始化;右值引用和移动语义优化了资源管理,减少拷贝开销;类新增移动构造和移动赋值函数提升性能;lambda表达式提供匿名函数对象,增强代码简洁性和灵活性。这些特性共同推动了现代C++编程的发展,提升了开发效率与程序性能。
758 12