PTA 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, the number of nodes in a tree, and M (<), 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-digitID
's of its children. For the sake of simplicity, let us fix the root ID to be01
.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 and02
is its only child. Hence on the root01
level, there is0
leaf node; and on the next level, there is1
leaf node. Then we should output0 1
in a line.Sample Input:
2 1
01 1 02
Sample Output:
0 1
这题没什么难的,可能存父节点不存子节点难想一点,但事实上存子节点也不难,总之随便水水就好。
代码:
#include <iostream>
#include <string>
using namespace std; struct NODE{
int father,depth;
bool nochild;
}; int main(){
int n,m,father_id,son_id,son_number,res[]={},depth_max;
NODE tree[];
for (int i=;i<=;i++){
tree[i].father=tree[i].depth=;
tree[i].nochild=true;
}
cin >> n >> m;
for (int i=;i<m;i++){
cin >> father_id >> son_number;
if (son_number!=) tree[father_id].nochild=false;
for (int j=;j<son_number;j++){
cin >> son_id;
tree[son_id].father=father_id;
}
}
for (int i=;i<=n;i++){
int now=i,level=;
for (;tree[now].father!=;){
now=tree[now].father;
level++;
}
tree[i].depth=level;
depth_max=(depth_max>level)?depth_max:level;
}
for (int i=;i<=n;i++){
if (tree[i].nochild) res[tree[i].depth]++;
}
for (int i=;i<depth_max;i++)
cout << res[i] << " ";
cout << res[depth_max];
return ;
}
PTA 1004 Counting Leaves的更多相关文章
- PTA 1004 Counting Leaves (30)(30 分)(dfs或者bfs)
1004 Counting Leaves (30)(30 分) A family hierarchy is usually presented by a pedigree tree. Your job ...
- 1004. Counting Leaves (30)
1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- PAT 解题报告 1004. Counting Leaves (30)
1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is t ...
- PAT甲1004 Counting Leaves【dfs】
1004 Counting Leaves (30 分) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- PAT 1004 Counting Leaves (30分)
1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is t ...
- 1004 Counting Leaves (30分) DFS
1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- PAT Advanced 1004 Counting Leaves
题目与翻译 1004 Counting Leaves 数树叶 (30分) A family hierarchy is usually presented by a pedigree tree. You ...
- 1004 Counting Leaves ——PAT甲级真题
1004 Counting Leaves A family hierarchy is usually presented by a pedigree tree. Your job is to coun ...
- PTA (Advanced Level) 1004 Counting Leaves
Counting Leaves A family hierarchy is usually presented by a pedigree tree. Your job is to count tho ...
随机推荐
- GitBook安装部署实操手册
前言 GitBook是一个基于Node.js的命令行工具,可使用Git和Markdown来编写文档,赞誉太多,不再赘述. Node.js 下载安装包 cd /tmp wget https://node ...
- 在.NET Core中使用MachineKey
在.NET Core中使用MachineKey 姐妹篇:<ASP.NET Cookie是怎么生成的> 姐妹篇:<.NET Core验证ASP.NET密码> 在上篇文章中,我介绍 ...
- Python3(九) 闭包
一. 一切皆对象 函数式编程并没有标准定义,如果代码非常繁琐则考虑使用. 学习闭包的概念,不是python独有的. 其他大多数语言中的函数只是一段可执行的代码,并不是对象. python中的函数是对象 ...
- node - MongoDB数据库
mongod 安装配置 在Mongodb官网下载最新版本的Mongodb下载地址 下载msi的window安装包,可以装到C盘或者D盘目录下 配置 由于我是安装在D盘的环境下 D:\Program F ...
- 20200110--python学习第八天
今日内容 进制 对于计算机而言无论是计算机存储或是网络传输输入的本质都是:二进制:例如电脑上存储的视频/图形/文件/微信/qq的表情包/小视频都是二进制. 二进制:计算机内部 八进制: 十进制: 十六 ...
- 内网客户 通过 公网域名/ip 访问内网web服务器 出错
在一内部局域网中, client 内网地址为 10.0.0.2 web 服务器内网地址为 10.0.0.1 外网地址为 211.6.15.1 域名为 xx.love.com ...
- 简单ts文件结构
一.ts文件结构 DEMO{ 1. .vscode:特有文件夹,调试的配置文件,启动浏览器 2. Js:放ts编译后的文件,不管 3. Ts:放ts文件,敲代码 4. tsconfig.json:ts ...
- 【重新整理】log4j 2的使用
一 概述 1.1 日志框架 日志接口(slf4j) slf4j是对所有日志框架制定的一种规范.标准.接口,并不是一个框架的具体的实现,因为接口并不能独立使用,需要和具体的日志框架实现配合使用(如log ...
- drf序列化大总结
目录 一.APIView的请求生命周期 二.序列化组件 视图类中使用序列化 Meta配置类中的配置 自定义校验规则 入库方法 自定义字段 如果有群改操作 重(难)点 三.视图家族 四.路由组件 五.权 ...
- 关于存储最近N次数据的问题的实现
需求描述: 需要实现采集中,始终保持最近10次的数据 描述分析: 当采集第一次数据的存储的时候,开辟一个长度为11的list,和一个标记为来记录当前的采集轮询次数(记录1-10,第11次数值归1) 数 ...