PAT (Advanced Level) Practise 1004 解题报告
问题描述
- Counting Leaves (30)
时间限制 400 ms
内存限制 65536 kB
代码长度限制 16000 B
判题程序 Standard
作者 CHEN, Yue
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.
Input
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:
ID K ID1 ID2 ... 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.
Output
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<cstdio>
using namespace std;
int main()
{
//freopen("in.txt","r",stdin);
int i,j,k,n,m,s,t,a[101]={0},b[101]={0},c[102]={0};
cin>>n>>m;
for (i=0;i<m;i++)
{
cin>>s>>t;
b[s]=1;
for (j=0;j<t;j++)
{
cin>>k;
a[k]=s;
}
}
s=0;
for (i=1;i<=n;i++)
if (b[i]==0)
{
t=a[i];
k=1;
while (t>0)
{
k++;
t=a[t];
}
c[k]++;
if (k>s) s=k;
}
cout<<c[1];
for (i=2;i<=s;i++) cout<<' '<<c[i];
return 0;
}
提交记录

PAT (Advanced Level) Practise 1004 解题报告的更多相关文章
- PAT (Advanced Level) Practise 1002 解题报告
GitHub markdownPDF 问题描述 解题思路 代码 提交记录 问题描述 A+B for Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 ...
- PAT (Advanced Level) Practise 1003 解题报告
GitHub markdownPDF 问题描述 解题思路 代码 提交记录 问题描述 Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题 ...
- PAT (Advanced Level) Practise 1001 解题报告
GiHub markdown PDF 问题描述 解题思路 代码 提交记录 问题描述 A+B Format (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判 ...
- PAT (Advanced Level) Practise - 1094. The Largest Generation (25)
http://www.patest.cn/contests/pat-a-practise/1094 A family hierarchy is usually presented by a pedig ...
- 1079. Total Sales of Supply Chain (25)【树+搜索】——PAT (Advanced Level) Practise
题目信息 1079. Total Sales of Supply Chain (25) 时间限制250 ms 内存限制65536 kB 代码长度限制16000 B A supply chain is ...
- 1076. Forwards on Weibo (30)【树+搜索】——PAT (Advanced Level) Practise
题目信息 1076. Forwards on Weibo (30) 时间限制3000 ms 内存限制65536 kB 代码长度限制16000 B Weibo is known as the Chine ...
- 1064. Complete Binary Search Tree (30)【二叉树】——PAT (Advanced Level) Practise
题目信息 1064. Complete Binary Search Tree (30) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B A Binary Search Tr ...
- 1078. Hashing (25)【Hash + 探測】——PAT (Advanced Level) Practise
题目信息 1078. Hashing (25) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B The task of this problem is simple: in ...
- 1077. Kuchiguse (20)【字符串处理】——PAT (Advanced Level) Practise
题目信息 1077. Kuchiguse (20) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B The Japanese language is notorious f ...
随机推荐
- 第十四单元 Linux网络原理及基础设置
·ifconfig命令来维护网络(详见linux系统管理P422) 1) 掌握ifconfig命令的功能:显示所有正在启动的网卡的详细信息或设定系统中网卡的IP地址.2) 灵活应用ifconfig命令 ...
- MongoDB----提升
文档之间的联系 一对一:通过文档内嵌的形式体现一对一的关系 db.user.insert({name:"xiaoming",frind:{name:"xiahua&quo ...
- js两种写法执行速度比较
记录 function test1(){ this.say = function(){} } function test2(){ this.say = function(){} return this ...
- Python推荐系统库--Surprise实战
一.使用movieLens数据集 from surprise import KNNBasic, SVD from surprise import Dataset from surprise impor ...
- 三 os模块
os模块是与操作系统交互的一个接口 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname") 改变当前脚本工作目录:相 ...
- windows环境下永久修改pip镜像源的方法(转)
一.在windows环境下修改pip镜像源的方法(以python3.7为例) (1):在windows文件管理器中,输入 %APPDATA%,cmd里面输入即可. (2):会定位到一个新的目录下,在该 ...
- zookeeper都有哪些使用场景
分布式协调 这个其实是zk很经典的一个用法,比如,A系统发送个请求到mq,然后B拿到消息消费之后处理了.那A系统如何知道B系统的处理结果? 用zk就可以实现分布式系统之间的协调工作.A系统发送请求之后 ...
- Linux环境安装Eclipse工具开发
1.官网下载maven:https://maven.apache.org/download.cgi 2.上传到虚拟机进行解压缩操作: [hadoop@slaver1 package]$ tar -zx ...
- 关于浏览器对html, js,css的解析先后顺序的理解
1.首先要了解页面的结构(包含哪些元素?哪些计算机语言能够在页面中运行 ) (1)html 不仅可以包含文字,还可以包含图片.链接,甚至音乐.程序等非文字元素的标记语言 ...
- 【bzoj1042】[HAOI2008]硬币购物 背包dp+容斥原理
题解: 计数题 首先考虑容斥 这题很明显加了限制状态就很多 考虑没有限制 显然可以直接dp 然后 我们看一下 容斥 某一个使用>=k张 那么其实就是 f[i-k*c[]] 于是这样就可以做了