Source:

PAT A1004 Counting Leaves (30 分)

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 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.

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 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

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的更多相关文章

  1. 1004. Counting Leaves (30)

    1004. Counting Leaves (30)   A family hierarchy is usually presented by a pedigree tree. Your job is ...

  2. PAT 解题报告 1004. Counting Leaves (30)

    1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is t ...

  3. PAT1004:Counting Leaves

    1004. Counting Leaves (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A fam ...

  4. PTA (Advanced Level) 1004 Counting Leaves

    Counting Leaves A family hierarchy is usually presented by a pedigree tree. Your job is to count tho ...

  5. PAT-1004 Counting Leaves

    1004 Counting Leaves (30 分) A family hierarchy is usually presented by a pedigree tree. Your job is ...

  6. PAT甲1004 Counting Leaves【dfs】

    1004 Counting Leaves (30 分) A family hierarchy is usually presented by a pedigree tree. Your job is ...

  7. PAT Counting Leaves[一般]

    1004 Counting Leaves (30)(30 分) A family hierarchy is usually presented by a pedigree tree. Your job ...

  8. 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 ...

  9. pat1004. Counting Leaves (30)

    1004. Counting Leaves (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A fam ...

随机推荐

  1. undefined reference to `mysql_init'解决办法

    命令行后面加入 -l mysqlclient 例如: 对mysqlQuery.c编译,使用gcc mysqlQuery.c -o mysqlQuery -l mysqlclient,即可编译成功.

  2. wampServer2.2 You don't have permission to access /phpmyadmin/ on this server.

    You don't have permission to access /phpmyadmin/ on this server. 打开 然后

  3. 资源-.Net-ASP.NET:ASP.NET资源列表

    ylbtech-资源-.Net-ASP.NET:ASP.NET资源列表 ASP.NETFree. Cross-platform. Open source.A framework for buildin ...

  4. URAL 1748 The Most Complex Number

    题目链接:https://vjudge.net/problem/11177 题目大意: 求小于等于 n 的最大反素数. 分析: n <= 10^18,而前20个素数的乘积早超过10^18,因此可 ...

  5. 剑指offer——44连续子数组的最大和

    题目描述 HZ偶尔会拿些专业问题来忽悠那些非计算机专业的同学.今天测试组开完会后,他又发话了:在古老的一维模式识别中,常常需要计算连续子向量的最大和,当向量全为正数的时候,问题很好解决.但是,如果向量 ...

  6. 记一次批量修改SQLServer表数据

    前提: 1.数据有上百万条,分成10几张excel表,从数据库中导出,由业务部门修改: 2.数据没什么规律: 3.和数据库DB商量后决定先把从excel导进数据库中,再通过关联查询修改数据 将 Exc ...

  7. background-size的值cover、contain和100%

    图1 给一个宽600px,高600px的div添加一张宽480px,高360px的背景图片.不重复显示的情况下,默认显示为图1. 1.background-size: 100% 100%; 会将图片的 ...

  8. VS2017 打包(详细)

    1.安装打包插件:Microsoft Visual Studio 2017安装程序项目 ​ 2.联机查找下面的组件,然后安装,重启VS,进行插件安装 3.新建安装项目,另外,有些人可能会想这么多安装类 ...

  9. 用C#简单实现的36进制转换代码

    private const string initValue = "A0000001"; private static string cs = "0123456789AB ...

  10. vue 学习四 了解组件

    1组件的注册 全局注册 import Vue from 'vue'; import com from './component1'; Vue.component("com_name" ...