题目描述:

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

这题没什么难的,可能存父节点不存子节点难想一点,但事实上存子节点也不难,总之随便水水就好。

代码:
 #include <iostream>
#include <string>
using namespace std; struct NODE{
int father,depth;
bool nochild;
}; int main(){
int n,m,father_id,son_id,son_number,res[]={},depth_max;
NODE tree[];
for (int i=;i<=;i++){
tree[i].father=tree[i].depth=;
tree[i].nochild=true;
}
cin >> n >> m;
for (int i=;i<m;i++){
cin >> father_id >> son_number;
if (son_number!=) tree[father_id].nochild=false;
for (int j=;j<son_number;j++){
cin >> son_id;
tree[son_id].father=father_id;
}
}
for (int i=;i<=n;i++){
int now=i,level=;
for (;tree[now].father!=;){
now=tree[now].father;
level++;
}
tree[i].depth=level;
depth_max=(depth_max>level)?depth_max:level;
}
for (int i=;i<=n;i++){
if (tree[i].nochild) res[tree[i].depth]++;
}
for (int i=;i<depth_max;i++)
cout << res[i] << " ";
cout << res[depth_max];
return ;
}

 

PTA 1004 Counting Leaves的更多相关文章

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

  2. 1004. Counting Leaves (30)

    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甲1004 Counting Leaves【dfs】

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

  5. PAT 1004 Counting Leaves (30分)

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

  6. 1004 Counting Leaves (30分) DFS

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

  7. PAT Advanced 1004 Counting Leaves

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

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

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

  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. HessianSharp如何部署到IIS7上?

    第一:添加映射 第二:选择经典

  2. Linux系统之LVS+Keepalived实现

    1.简述lvs四种集群特点及使用场景 LVS集群有4种类型,分别是NAT.DR.TUN.FULLNAT 从工作方式来讲,NAT和FULLNAT都要修改请求报文的目标IP和目标端口(NAT)或源IP目标 ...

  3. Rust入坑指南:步步为营

    俗话说:"测试写得好,奖金少不了." 有经验的开发人员通常会通过单元测试来保证代码基本逻辑的正确性.如果你是一名新手开发者,并且还没体会到单元测试的好处,那么建议你先读一下我之前的 ...

  4. sys.stdout.write和print和sys.stdout.flush

    1. 先看下官方文档 """ sys.stdout.write(string) Write string to stream. Returns the number of ...

  5. java String hashCode遇到的坑

    在进行数据交换时,如果主键不是整型,需要对字符串,或联合主键拼接为字符串,进行hash,再进行取模分片,使用的是String自带的hashCode()方法,本来是件很方便的事,但是有些字符串取hash ...

  6. Vscode开发Python环境安装

    VSCode 开发 Python 使用python,主要是做一些工具和爬虫的操作,语法简单,功能复杂,入手很快. 我们通过在 VSCode 中搜索 Python 插件,发现,开发 python 的话, ...

  7. 设置ssh远程其他主机登录显示提示信息

    文件及路径: /etc/motd 实例: [root@A-client ~]# ssh -p 22 test@10.0.0.2 test@10.0.0.2's password: Last login ...

  8. 上周 GitHub 热点速览 vol.08:系统设计必看 The System Design Primer

    作者:HelloGitHub-小鱼干 摘要:GitHub Trending 上周看点,老项目依旧抢眼,系统设计必看 Repo:The System Design Primer 周获 1k+ star, ...

  9. Axer-使用Msfvenom自动创建有效负载的工具

    Axer-使用Msfvenom自动创建有效负载的工具 该axer将替换为有效的手动操作msfvenom,设置更容易和快了很多. AXER可以让您选择平台,格式,编码器,与另一个文件绑定以及许多其他功能 ...

  10. React中使用 PropTypes 进行类型检查

    官方文档学习链接:https://zh-hans.reactjs.org/docs/typechecking-with-proptypes.html import React, { Component ...