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 ...
随机推荐
- [Effective Java 读书笔记] 第三章类和接口 第十六条
第十六条 复合优先于继承 如果不确定B和A的关系是,is-a的关系,B确实也是A,那么久不应该使用B继承A,否则会暴露实现细节, 你的实现都会限制在原始的实现上. 书中举的第一个例子,实现了一个类ex ...
- IP unnumbered interface,某个接口不编号,某个接口不分配IP地址
OSPFv2中,提到点到点链路可以是unnumbered,不编号,不分配IP地址 12.4.1.1. Describing point-to-point interfaces ...
- k8s 安装 prometheus 过程记录
开始以为只要安装 prometheus-operator 就行了. git clone https://github.com/coreos/prometheus-operator.git cd pro ...
- [jQuery]入口函数(一) jquery.min.js 一定要单线程下载,复制粘贴容易入坑
jQuery入口函数 等着DOM结构渲染完毕即可执行内部代码,不必等到所有外部资源加载完毕 $(function () { // 此处是页面DOM加载完成的入口 }); $(document).rea ...
- VMware vCenter Server6.0安装及群集配置介绍
在本项目中,将在VMware Workstation 模拟的Windows Server 2008 R2虚拟机中安装VMware vCenter Server ,并且使用vCenter Server捆 ...
- CSS中元素的显示模式
在CSS中,根据元素显示模式的不同元素标签被分为了两类:行内元素(inline-level).块级元素(block-level). 1,首先介绍什么是行内元素,什么又是块级元素? 1.1,行内元素就 ...
- C#_Excel数据读取与写入_自定义解析封装类_支持设置标题行位置&使用excel表达式收集数据&单元格映射&标题映射&模板文件的参数数据替换
本篇博客园是被任务所逼,而已有的使用nopi技术的文档技术经验又不支持我需要的应对各种复杂需求的苛刻要求,只能自己造轮子封装了,由于需要应对很多总类型的数据采集需求,因此有了本篇博客的代码封装,下面一 ...
- sublime中写python代码
SublimeREPL插件 这个是首先要安装的,此插件主要功能是为了实现交互,在安装后需要一些简单的配置 在Preferences--> Key Bindings--> user下添加如下 ...
- 秋水逸冰实用Linux脚本收藏
秋水逸冰的脚本非常受欢迎,奈何其本人博客已经不能访问(目前是这样,不知道别的地区是否能访问),实际上GitHub上他本人一直在维护,因为某些原因不放出他本人的GitHub地址.截止到2019年12月1 ...
- Ubuntu P40显卡配置CUDA 10.1,CUDNN 7.6,Conda 5.2.0, Tensorflow-gpu 1.8
1. 安装CUDA 禁用nouveau vim /etc/modprobe.d/blacklist.conf 最后两行加入 blacklist nouveau options nouveau mode ...