PAT_A1004#Counting Leaves
Source:
Description:
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
Keys:
Code:
/*
time: 2019-06-28 16:47:33
problem: PAT_A1004#Counting Leaves
AC: 16:22 题目大意:
统计各层的叶子结点个数
输入:
第一行给出,结点数N<100,分支结点数M
接下来M行,结点id,孩子数k,孩子id(1~n,root==1)
多组输入样例 基本思路:
遍历并记录各层叶子结点数即可
*/
#include<cstdio>
#include<vector>
using namespace std;
const int M=;
vector<int> tree[M];
int leaf[M],level=; void Travel(int root, int hight)
{
if(tree[root].size()==)
{
if(hight > level)
level = hight;
leaf[hight]++;
return;
}
for(int i=; i<tree[root].size(); i++)
Travel(tree[root][i],hight+);
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE int n,m;
while(~scanf("%d", &n))
{
level=;
for(int i=; i<=n; i++){
tree[i].clear();
leaf[i]=;
}
scanf("%d", &m);
for(int i=; i<m; i++)
{
int id,k,kid;
scanf("%d%d", &id,&k);
for(int j=; j<k; j++)
{
scanf("%d", &kid);
tree[id].push_back(kid);
}
}
Travel(,);
for(int i=; i<=level; i++)
printf("%d%c", leaf[i], i==level?'\n':' '); } return ;
}
PAT_A1004#Counting Leaves的更多相关文章
- 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 ...
- PAT1004:Counting Leaves
1004. Counting Leaves (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A fam ...
- PTA (Advanced Level) 1004 Counting Leaves
Counting Leaves A family hierarchy is usually presented by a pedigree tree. Your job is to count tho ...
- PAT-1004 Counting Leaves
1004 Counting Leaves (30 分) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- PAT甲1004 Counting Leaves【dfs】
1004 Counting Leaves (30 分) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- PAT Counting Leaves[一般]
1004 Counting Leaves (30)(30 分) A family hierarchy is usually presented by a pedigree tree. Your job ...
- 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 ...
- pat1004. Counting Leaves (30)
1004. Counting Leaves (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A fam ...
随机推荐
- SCP-bzoj-1054
项目编号:bzoj-1054 项目等级:Safe 项目描述: 戳这里 特殊收容措施: 直接状压BFS即可,我实现的比较渣..复杂度O(45*216). 附录: #include <bits/st ...
- python中的缓存技术
python缓存技术 def console(a,b): print('进入函数') return (a,b) print(console(3,'a')) print(console(2,'b')) ...
- Hyperledger:常见加密算法分类列表
算法原理查询:http://mathworld.wolfram.com 加密散列函数 (消息摘要算法,消息认证码,MD算法) Keyed-hash message authentication c ...
- 剑指offer——51丑数
题目描述 把只包含质因子2.3和5的数称作丑数(Ugly Number).例如6.8都是丑数,但14不是,因为它包含质因子7. 习惯上我们把1当做是第一个丑数.求按从小到大的顺序的第N个丑数. / ...
- 常用内置模块(二)——logging模块
logging模块 一.logging作用 1. 控制日志级别 2. 控制日志格式 3. 控制输出的目标为文件 二.日志级别 logging.debug( logging.info( loggin ...
- JAVA API 实现hdfs文件操作
java api 实现hdfs 文件操作会出现错误提示: Permission denied: user=hp, access=WRITE, inode="/":hdfs:supe ...
- Dubbo面试20问!这些题你都遇到过吗?
作者:Dean Wang https://deanwang1943.github.io/bugs/2018/10/05/面试/饿了么/dubbo 面试题/ 1.dubbo是什么 dubbo是一个分布式 ...
- 看官方文档学习springcloud搭建
很多java的朋友学习新知识时候去百度,看了之后一知半解,不知道怎么操作,不知道到底什么什么东西,那么作为java码农到底该怎么学习额 一 百度是对还是错呢? 百度是一个万能的工具,当然是对也是错的 ...
- OAuth2.0实例说明
OAuth2.0 详细实列+Word文档清晰说明 实例下载地址:https://files.cnblogs.com/files/liyanbofly/OAuth2.0%E5%AE%9E%E4%BE%8 ...
- 修改Tomcat的端口号方法
(1).查找conf路径下的server.xml文件,路径如: I: \tomcat6\apache-tomcat-6.0.32\conf\server.xml (2).打开server.xml文 ...