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 ...
随机推荐
- jsp 错误处理
JSP提供了很好的错误处理能力.除了在Java代码 中可以使用try语句,还可以指定一个特殊页面.当应 用页面遇到未捕获的异常时,用户将看到一个精心设计 的网页解释发生了什么,而不是一个用户无法理解的 ...
- JSP 动作
动作是第三种类型的语法元素,它们被转换成Java 代码来执行操作,如访问一个Java对象或调用方法. 一. useBean useBean将创建一个关联Java对象的脚本变量.这 是早期分离的表示层和 ...
- java Swing组件和事件处理
1.常见的容器 JComponent是 Container 的子类,中间容器必须添加到底层容器中才能够发挥作用, JPanel 面板 :使用jPanel 创建一个面板,再通过添加组件到该面板上面,JP ...
- CF558E
非常好的一道题,是线段树的常见玩法 将字符串转化为1~26个数 对区间开一棵线段树,用两个数组分别维护区间中1~26每个数的个数以及一个区间覆盖标记,表示这个区间是否被某一个值覆盖了 在每次排序时,首 ...
- Android Studio xml文件中的布局预览视图
操作系统:Windows 10 x64 IDE:Android Studio 3.3.1 更新了Android Studio之后,xml文件中的布局预览视图变得如此简洁! 原因是没有勾选Show La ...
- Jmeter-JDBC Request参数化
一.参数化 1.选择Query Type(查询类型)为Prepared Select Statement 2.写好sql 3.在Parameter Values中输入参数,多个参数用‘,’隔开 4.P ...
- python 内置数据类型之数字
目录: 1.2. 数字 1.2.1. 数字类型 1.2.2. 浮点数 1.2.3. 进制记数 1.2.4. 设置小数精度 1.2.5. 分数 1.2.6. 除法 1.2 数字 1.2.1 数字类型 ...
- ubuntu安装界面 会出现不完整情况
解决方法: alt+鼠标左键或者win+鼠标左键拖动
- MVC开发中的常见错误-02-在应用程序配置文件中找不到名为“OAEntities”的连接字符串。
在应用程序配置文件中找不到名为“OAEntities”的连接字符串. 分析原因:由于Model类是数据库实体模型,通过从数据库中引用的方式添加实体,所以会自动产生一个数据库连接字符串,而程序运行到此, ...
- Just Oj 2017C语言程序设计竞赛高级组D: 字符串最大表示(next数组)
D: 字符串最大表示 时间限制: 1 s 内存限制: 128 MB 题目描述 有如下定义,abcnabcn表示字符串abc重复n次,例如abc2abc2表示abcabc. 给定一个字符串,求 ...