1004 Counting Leaves (30)(30 分)

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

Output

For each test case, you are supposed to count those family members who have no child for every seniority levelstarting 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

参考这个:http://www.cnblogs.com/linkstar/p/5674895.html
#include <iostream>
#include <cstring>
#include <cstdio> using namespace std;
//给出节点总数和非叶节点数,并且给出非叶节点的子节点,输出每层有几个叶节点。
struct node{
int hir;
int father;
int son;
node(){
hir=;son=;father=;}
}node[];
int ceng[]; int main()
{
//首先我的问题是这个树怎么存的,用数组来存?
//关键是要表示出来层数信息。
int n,m;
//freopen("1.txt","r",stdin);
cin>>n>>m;
int id,k,idk;
node[].hir=;
for(int i=;i<m;i++){
cin>>id>>k;
node[id].son+=k;
for(int j=;j<k;j++){
cin>>idk;
node[idk].father=id;//层数+1
}
}
//接下来就是要判断每层上一共有多少个叶子节点了。
//有一个小小的疑问就是,是不是层数按序号输入的呢?还是说每次找第几层的都要把所有的给便利一下?
//需要注意的是有可能新输入的父节点等级并没有确定。
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
if(node[j].father==i)
//node[i].son++;
node[j].hir=node[i].hir+;//从第一个点也就是根节点开始遍历,那么肯定可以顺序确定。
}
} int maxs=;
for(int i=;i<=n;i++){
if(node[i].son==){
ceng[node[i].hir]++;
if(node[i].hir>maxs)
maxs=node[i].hir;
}
}
for(int i=;i<=maxs;i++){
cout<<ceng[i];
if(i!=maxs)cout<<" ";
}
return ;
}

//大意就是输入一个值表示树中节点总数,另一个数表示非叶节点总数,接下来就是输入树的层次结构。要求判断出每一层有多少个叶节点并且输出。

第一次提交0分,因为是我有中间输入忘了注释,和freopen没有注释掉。之后提交是19分,看完链接中的代码之后就发现是因为我没有考虑点输入的先后顺序,有可能父节点输入的时候它的等级并没有确定,所以就需要一个字段father来记录,最后再有一个两层循环(这个时间复杂度就上去了,不过由于题目数据比较小,所以可用)。先记录父子关系,再循环确定层次关系,最后用哈希数组判断每层的叶结点个数即可。总的来说还可以。

PAT Counting Leaves[一般]的更多相关文章

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

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

  2. PAT甲1004 Counting Leaves【dfs】

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

  3. PAT 1004 Counting Leaves (30分)

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

  4. PAT Advanced 1004 Counting Leaves

    题目与翻译 1004 Counting Leaves 数树叶 (30分) A family hierarchy is usually presented by a pedigree tree. You ...

  5. 1004 Counting Leaves ——PAT甲级真题

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

  6. PAT_A1004#Counting Leaves

    Source: PAT A1004 Counting Leaves (30 分) Description: A family hierarchy is usually presented by a p ...

  7. 1004. Counting Leaves (30)

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

  8. PAT1004:Counting Leaves

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

  9. PTA (Advanced Level) 1004 Counting Leaves

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

随机推荐

  1. Material Design系列第三篇——Using the Material Theme

    Using the Material Theme This lesson teaches you to Customize the Color Palette Customize the Status ...

  2. linux 设置pip 镜像 Pip Warning:–trusted-host 问题解决方案

    pip升级到7.0以后,在使用http镜像进行包安装及升级的时候往往会有如下提示: Collecting beautifulsoup4The repository located at mirrors ...

  3. Mavan学习之pom聚合

    所有用Maven管理的真实的项目都应该是分模块的,每个模块都对应着一个pom.xml.它们之间通过继承和聚合(也称作多模块,multi-module)相互关联.那么,为什么要这么做呢?我们明明在开发一 ...

  4. C语言程序设计--指针基础

    指针 指针是一种特殊变量(存储内存地址).当然它本身也是占用内存的,所以会带来一个问题,那就是指针存在以下概念:指针的类型(int* 一个整型指针),指针指向的类型(int* p = 5, 说明指针p ...

  5. Dockerfile创建镜像

    Dockerfile是一个文本格式的配置文件,用户可以使用Dockerfile来快速创建自定义的镜像. Dockerfile由一行行命令语句组成,并且支持易#开头的注释行. 一般而言Dockerfil ...

  6. 【咸鱼教程】TextureMerger1.6.6 三:Bitmap Font的制作和使用

    BitmapFont主要用于特殊字体在游戏中的使用   目录 一 方法1:添加字符      适合一张一张的零碎图片来制作位图字体 二 方法2:系统字体      适合使用已安装的系统字体来制作位图字 ...

  7. 【CF896E】Welcome home, Chtholly 暴力+分块+链表

    [CF896E]Welcome home, Chtholly 题意:一个长度为n的序列ai,让你支持两种操作: 1.l r x:将[l,r]中ai>x的ai都减去x.2.l r x:询问[l,r ...

  8. 配置java环境变量后没有生效的解决办法

    参考文章:https://blog.csdn.net/tooky_poom/article/details/60768458 系统安装了jdk1.7,环境变量正常,但是安装jdk1.8后,修改环境变量 ...

  9. http模拟登陆及发请求

    首先声明下,如果服务端写入的cookie属性是HttpOnly的,程序是不能自动获取cookie的,需要人工登陆网站获取cookie再把cookie写死,如下图所示: http测试工具:http:// ...

  10. Pyqt图标下载网站

    下载地址: https://www.easyicon.net/ 1.程序中图标建议使用32x32的PNG格式. 2.pyinstaller打包中图标建议使用32x32的ICO格式.