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 ...
随机推荐
- 点击手机返回键弹出Dialog对话框功能
在程序中,我们为了防止出现客户在使用程序填信息或者浏览页面时因误点返回键造成关闭界面的现象,需要添加弹出框功能,以确认客户是否要退出本界面,下面是功能实现的代码: 1.点击手机返回键的判断 publi ...
- C++——指针与数组
1.数组名不是指针,神似指针(可以将数组名直接赋值给指针) (1)数组名的内涵在于其指代实体是一种数据结构,这种数据结构就是数组:(2)数组名的外延在于其可以转换为指向其指代实体的指针,而且是一个指针 ...
- Hyperledger:常见加密算法分类列表
算法原理查询:http://mathworld.wolfram.com 加密散列函数 (消息摘要算法,消息认证码,MD算法) Keyed-hash message authentication c ...
- Git 学习第二天(一)
继续昨天的学习. 回顾一下,昨天我们安装了git 并创建了登录名及邮箱,还向git仓库提交了一个readme.txt的文本文件 下面,我们来修改下这个文件,将内容改为 Git is a distrib ...
- 剑指offer——54数组中的逆序对
题目描述 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组中的逆序对的总数P.并将P对1000000007取模的结果输出. 即输出P%1000 ...
- Jackson第一个程序
再进入学习jackson库的细节之前,让我们来看看应用程序操作功能.在这个例子中,我们创建一个Student类.将创建一个JSON字符串学生的详细信息,并将其反序列化到学生的对象,然后将其序列化到JS ...
- 20140814 explicit
1.explicit explicit 只对构造函数起作用,用来抑制隐式转换. 如: class A { A(int a); }; int Functi ...
- Day 19:面向对象【类方法】静态属性/静态属性/类方法
静态属性 @property class Mom: gender = "woman" def __init__(self,name,weight): self.name = n ...
- 从模块到python文件的两种用法
01模块的四种形式 模块 就是从逻辑上组织python代码(变量,函数,类,逻辑:实现一个功能),本质就是.py结尾的python文件(文件名是test.py的话,它的对应模块名就是test) 包 用 ...
- position:relative/static/fixed/absolute定位的区别以及使用场景
absolute是相对于自己最近的父元素来定位的,relative是相对于自己来定位的 relative 不脱离文档流,absolute 脱离文档流.也就是说:relative 的元素尽管表面上看到它 ...