题目与翻译

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 non-leaf nodes. Then M lines follow, each in the format:

每个输入文件包含一个测试用例。每个案例都从一行开始,该行包含0 < n < 100、树中节点的数量和 m (< n)、非叶节点的数量。然后是 m 行,每行格式如下:

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.

其中 ID 是表示给定非叶节点的两位数,k 是其子节点的数量,后面是其子节点的两位数 ID 序列。为了简单起见,让我们将根 ID 修改为01。

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

输入结束时 n 为0。这种情况不能被处理。

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.

示例案例表示一个只有2个节点的树,其中01是根,02是它的唯一子节点。因此,在根01级上,有0个叶节点; 在下一级上,有1个叶节点。那么我们应该在一行中输出01。

Sample Input:

样本输入:

2 1
01 1 02

Sample Output:

示例输出:

0 1

理解与算法

简单地讲,这道题就是在求一棵多叉树的叶子节点的数量,并按照层的顺序打印!如果没有叶子结点就打印0,否则输出叶子结点个数。

粗略地想一想,层序遍历和前序遍历都可以完成,这里用的是深度优先算法,也就是先序遍历。

给出一个样例的示意图:

01是根节点,因为它有一个子节点02所以它不是叶子结点,而02是叶子结点,因此最后的输出为:

0 1

接下来来实现程序。

处理输入

// 全局变量
vector<int> nodes[100]; // 每个元素代表一个节点链表
int pedigree[100]; // 族谱树中每一层的叶子结点的数量
int pedigree_depth = -1; // 族谱树的最大深度 int main...(省略部分)
int N, M, node, num, child;
// 处理第一行
cin >> N >> M;
// 遍历所有的非叶节点,构建节点链表
for (int i = 0; i < M; ++i) {
cin >> node >> num;
for (int j = 0; j < num; ++j) {
cin >> child;
nodes[node].push_back(child);
}
}

这里用了一个vector的数组来存储每个节点的子节点链表。

遍历族谱树

/**
* 深度优先算法,遍历整个家族树,如果找到叶子结点就加入到全局变量数组中
* @param index 下标
* @param depth 深度
*/
void dfs(int index, int depth) {
if (nodes[index].empty()) {
// 如果这个节点没有子节点,那么就是叶子结点
pedigree[depth]++;
// 这个叶子结点的深度如果超过原本记录的最大深度,那么就更新最大深度
pedigree_depth = depth > pedigree_depth ? depth : pedigree_depth;
return;
}
// 遍历该节点的所有子节点
for (int i : nodes[index]) {
// 因为往下走了一层,所以深度加1
dfs(i, depth + 1);
}
}

为了提高效率,不用每次都遍历整个族谱叶子个数的数组,我们可以使用一个全局变量pedigree_length来确定整个数组的长度,提高最后的打印效率。

输出

// 数组默认值为0,这里输出这个数组的全部内容,长度为pedigree_length
cout << pedigree[0];
for (int i = 1; i <= pedigree_depth; ++i) {
cout << " " << pedigree[i];
}

代码实现

#include <iostream>
#include <vector> using namespace std; vector<int> nodes[100]; // 每个元素代表一个节点链表
int pedigree[100]; // 族谱树中每一层的叶子结点的数量
int pedigree_depth = -1; // 族谱树的最大深度 /**
* 深度优先算法,遍历整个家族树,如果找到叶子结点就加入到全局变量数组中
* @param index 下标
* @param depth 深度
*/
void dfs(int index, int depth) {
if (nodes[index].empty()) {
// 如果这个节点没有子节点,那么就是叶子结点
pedigree[depth]++;
// 这个叶子结点的深度如果超过原本记录的最大深度,那么就更新最大深度
pedigree_depth = depth > pedigree_depth ? depth : pedigree_depth;
return;
}
// 遍历该节点的所有子节点
for (int i : nodes[index]) {
// 因为往下走了一层,所以深度加1
dfs(i, depth + 1);
}
} int main() {
int N, M, node, num, child;
// 处理第一行
cin >> N >> M;
// 遍历所有的非叶节点,构建节点链表
for (int i = 0; i < M; ++i) {
cin >> node >> num;
for (int j = 0; j < num; ++j) {
cin >> child;
nodes[node].push_back(child);
}
}
// 对族谱树进行深度优先遍历
dfs(1, 0);
// 数组默认值为0,这里输出这个数组的全部内容,长度为pedigree_length
cout << pedigree[0];
for (int i = 1; i <= pedigree_depth; ++i) {
cout << " " << pedigree[i];
}
return 0;
}

PAT Advanced 1004 Counting Leaves的更多相关文章

  1. PAT Advanced 1004 Counting Leaves (30) [BFS,DFS,树的层序遍历]

    题目 A family hierarchy is usually presented by a pedigree tree. Your job is to count those family mem ...

  2. PAT甲1004 Counting Leaves【dfs】

    1004 Counting Leaves (30 分) A family hierarchy is usually presented by a pedigree tree. Your job is ...

  3. PAT A 1004. Counting Leaves (30)【vector+dfs】

    题目链接:https://www.patest.cn/contests/pat-a-practise/1004 大意:输出按层次输出每层无孩子结点的个数 思路:vector存储结点,dfs遍历 #in ...

  4. PAT 甲级 1004 Counting Leaves

    https://pintia.cn/problem-sets/994805342720868352/problems/994805521431773184 A family hierarchy is ...

  5. PAT甲级 1004.Counting Leaves

    参考:https://blog.csdn.net/qq278672818/article/details/54915636 首先贴上我一开始的部分正确代码: #include<bits/stdc ...

  6. PAT 解题报告 1004. Counting Leaves (30)

    1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is t ...

  7. PAT 1004 Counting Leaves (30分)

    1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is t ...

  8. 1004 Counting Leaves ——PAT甲级真题

    1004 Counting Leaves A family hierarchy is usually presented by a pedigree tree. Your job is to coun ...

  9. 1004. Counting Leaves (30)

    1004. Counting Leaves (30)   A family hierarchy is usually presented by a pedigree tree. Your job is ...

随机推荐

  1. babel 与 ast

    什么是 babel Babel 是一个工具链,主要用于将 ECMAScript 2015+ 版本的代码转换为向后兼容的 JavaScript 语法,以便能够运行在当前和旧版本的浏览器或其他环境中. 什 ...

  2. 用burp爆破tomcat的过程

    首先burp抓包,将抓到的包放到intruder中 通过burp中自带的解码得知账号密码中有个":"号 所以我们选择的数据类型为Custom iterator 第二条输入" ...

  3. vue封装API接口

    第一步: 首先引入axios 然后创建两个文件夹api和http http.js 里面的 1 import axios from 'axios';//引入axios 2 3 //环境的切换 开发环境( ...

  4. 5. 穿过拥挤的人潮,Spring已为你制作好高级赛道

    目录 ✍前言 版本约定 ✍正文 默认转换器注册情况 StreamConverter 代码示例 使用场景 兜底转换器 ObjectToObjectConverter part1:快速返回流程 part2 ...

  5. 如何在visual studio中,更改删除团队资源管理器的tfs地址 不能弹出来

    C:\Users\Administrator\AppData\Roaming\Microsoft\VisualStudio\16.0_8c6724b7\Team Explorer 进入文件夹:AppD ...

  6. MVC 微信网页授权 获取 OpenId

    最近开发微信公众平台,做下记录,以前也开发过,这次开发又给忘了,搞了半天,还是做个笔记为好. 注意框架为MVC 开发微信公众平台.场景为,在模板页中获取用户openid,想要进行验证的页面,集成模板页 ...

  7. Java生产环境下性能监控与调优详解视频教程 百度云 网盘

    集数合计:9章Java视频教程详情描述:A0193<Java生产环境下性能监控与调优详解视频教程>软件开发只是第一步,上线后的性能监控与调优才是更为重要的一步本课程将为你讲解如何在生产环境 ...

  8. 运行jar提示“没有主清单属性”的解决方法

    以下记录的是我export jar包后运行遇到问题的解决方法,如有错误,欢迎批评指正. 1.运行导出jar包,提示"没有主清单属性" 2.回想自己导出jar的操作是否有误,重新ex ...

  9. MRP物料需求计划

    1.重订货点的采购计划. 计算方式:再订货点的库存数量 = 安全库存 + 采购提前期 * 每天消耗的数量 一旦库存数量触及再订货点的库存数量,需触发采购订单订购物料,理想的情况下 ,下次到采购订单收货 ...

  10. 总结JAVA语言的十大特性

    JAVA语言的十大特性 1.简单 Java语言的语法简单明了,容易掌握从,而且Java语言是纯面向对象的语言. Java语言的语法规则和C++类似,从某种意义上来讲,Java原因是由C语言和C++语言 ...