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
IDis a two-digit number representing a given non-leaf node,Kis 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
01is the root and02is its only child. Hence on the root01level, there is0leaf node; and on the next level, there is1leaf node. Then we should output0 1in 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 ...
随机推荐
- 从Golang中open的实现方式看Golang的语言设计
Golang有很多优点: 开发高效:(C语言写一个hash查找很麻烦,但是go很简单) 运行高效:(Python的hash查找好写,但比Python高效很多) 很少的系统库依赖:(环境依赖少,一般不依 ...
- golang搭建一个简单的web服务器
package main import ( "io/ioutil" "log" "net/http" ) func main() { htt ...
- Pyinstaller 打包程序为可执行文件exe
Pyiinstaller打包 pyinstaller是python的一个第三方模块,使用它可以将pythnon程序打包为可执行文件,实现打包后的程序在没有python环境的机器上也可以运行.pyins ...
- 小白学 Python 数据分析(7):Pandas (六)数据导入
人生苦短,我用 Python 前文传送门: 小白学 Python 数据分析(1):数据分析基础 小白学 Python 数据分析(2):Pandas (一)概述 小白学 Python 数据分析(3):P ...
- Aliyun搭建svn服务器外网访问报错权限配置失败错误
搭建完后所有的配置如下 [root@iZuf655czz7lmtn8v15tsjZ conf]# pwd /home/SVN/conf [root@iZuf655czz7lmtn8v15tsjZ co ...
- JVM类加载器是否可以加载自定义的String
前言 曾经有一次,面试官问到类加载机制,相信大多数小伙伴都可以答上来双亲委派机制,也都知道JVM出于安全性的考虑,全限定类名相同的String是不能被加载的.但是如果加载了,会出现什么样的结果呢?异常 ...
- (四)开源C# WPF控件库《AduSkin – UI》
微信公众号:[Dotnet9的博客],网站:[Dotnet9],问题或建议:[请网站留言], 如果对您有所帮助:[欢迎赞赏]. 开源C# WPF控件库系列: (一)开源C# WPF控件库<Mat ...
- idea如何做到多模块开发项目 收藏整理
idea如何做到多模块开发项目 <packaging>pom</packaging>是什么意思? idea 快捷键汇总
- system.run
客户端开启了remotecommand后可以在server调用该命令在agent上执行一些命令 命令中有逗号 zabbix_get -s xxx.xxx.xxx.xxx -k "system ...
- PHPJN0001:phpmyadmin 允许密码为空 设置
phpmyadmin连接mysql数据库,出于安全考虑,默认不允许使用空密码连接数据库.因为数据库一般都设置密码访问. 但如果只是本机环境测试使用,每隔一段时间都需要填写密码,不是很方便. 如果没有修 ...