PAT甲级 1004. Counting Leaves(30分)

简介: PAT甲级 1004. Counting Leaves(30分)

1004. Counting Leaves(30分)


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 . 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
结尾无空行


#include <iostream>
#include <vector>
using namespace std;
int maxlevel = 1;
int leaf[100] = {0};
vector<int> P[100];
void DFS(int parent, int level)
{
    maxlevel = max(maxlevel, level);
    // 如果该点没有孩子结点
    if (!P[parent].size())
    {
        leaf[level]++;
        return;
    }
    // 依次搜索该点的每一个孩子结点
    for (int i = 0; i < P[parent].size(); i++)
    {
        DFS(P[parent][i], level + 1);
    }
}
int main()
{
    int N, M;
    cin >> N >> M;
    for (int i = 0; i < M; i++)
    {
        int parent, k, id;
        cin >> parent >> k;
        for (int j = 0; j < k; j++)
        {
            cin >> id;
            P[parent].push_back(id);
        }
    }
    DFS(1, 1);
    cout << leaf[1];
    for (int i = 2; i <= maxlevel; i++)
    {
        cout << " " << leaf[i];
    }
    return 0;
}

简单的深度优先搜索,需要注意的是数组存放孩子节点。每次递归要更新树的最大深度。

目录
相关文章
|
前端开发 Java 数据库连接
Spring Boot 3 整合 Mybatis-Plus 动态数据源实现多数据源切换
Spring Boot 3 整合 Mybatis-Plus 动态数据源实现多数据源切换
|
7月前
|
安全 前端开发 Java
Spring Boot 项目中触发 Circular View Path 错误的原理与解决方案
在Spring Boot开发中,**Circular View Path**错误常因视图解析与Controller路径重名引发。当视图名称(如`login`)与请求路径相同,Spring MVC无法区分,导致无限循环调用。解决方法包括:1) 明确指定视图路径,避免重名;2) 将视图文件移至子目录;3) 确保Spring Security配置与Controller路径一致。通过合理设定视图和路径,可有效避免该问题,确保系统稳定运行。
505 0
|
负载均衡 安全 网络协议
使用高防ip后源站IP暴露的解决办法
使用高防ip后源站IP暴露的解决办法
186 3
|
5月前
|
存储 安全 API
超实用!高效使用电商API,提升运营效率就靠它!
本文探讨了如何高效使用电商API以提升运营效能。电商API作为连接平台与外部系统的关键技术,可优化业务流程、增强用户体验、拓展渠道和驱动数据决策。文章从定义与价值出发,分析了前期准备、关键步骤及实战应用,并指出数据整合、接口安全和技术门槛等挑战的应对方法,展望了API在智能购物等领域的未来潜力,助力电商企业在竞争中脱颖而出。
在Linux中,ps aux 中的 VSZ 代表什么意思?RSS 代表什么意思?
在Linux中,ps aux 中的 VSZ 代表什么意思?RSS 代表什么意思?
|
前端开发
layui-form
`layui-form`是一个前端表单组件,提供多种表单类型如单行、多行排列的输入框,下拉选择框(支持模糊搜索),单选框和复选框。表单元素可通过`required`和`lay-verify`属性进行必填和验证设置。编辑器如`layedit`可用于富文本输入,表单监听事件如提交和单选器可绑定回调函数进行处理。此外,能动态更新渲染表单,初始化表单数据并进行数据校验,包括自定义验证规则。
437 1
|
前端开发 JavaScript 开发者
Bootstrap 3.x 版本基础引入指南
Bootstrap 3.x 版本基础引入指南
475 0
|
人工智能 JavaScript 前端开发
高效方便管理多版本Node(windows方式)
高效方便管理多版本Node(windows方式)
490 2
高效方便管理多版本Node(windows方式)
|
测试技术 数据库 UED
【白盒测试】单元测试的理论基础及用例设计技术(6种)详解
【白盒测试】单元测试的理论基础及用例设计技术(6种)详解
1781 1
|
算法
评价模型:CRITIC客观赋权法
评价模型:CRITIC客观赋权法
2246 0