Counting Leaves

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

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 1leaf node. Then we should output 0 1 in a line.

Sample Input:

2 1
01 1 02

Sample Output:

0 1

题目解析

  本题给出一颗家族关系树第一行首先给出两个整数0<N<100为家族中人数,M<N为非叶子节点个数(有孩子的人的数量)之后跟随M行,每行都是一个有孩子的人的信息,其中包括一个两位id为其对应编号,一个整数K为其孩子的数量,之后K个两位数为其孩子的编号。家族树的根结点编号为1,要求由根结点所在层开始输出每层拥有的叶子结点个数(每层没有孩子的成员个数)。

用一个vector<int> 类型的数组child[ ]储存每个人的孩子的编号,int型数组cnt[ ]储存每层的叶子结点数量,视根结点为第0层由根结点开始dfs搜索其每一个孩子,每个点的孩子所在的层数就是其层数加一,若搜索到没有孩子的结点便将其对应层叶子结点数量加一。将搜索到的最大层数计入maxL。

最后由0到maxL输出每层的叶子结点数量即可。

 #include <bits/stdc++.h>
using namespace std;
vector<int> child[]; //储存每个id的孩子
int cnt[]; //记录每层叶子结点的个数
int n, m; //n为总人数, m为非叶子结点数量
int maxL = INT_MIN; //maxL记录最大层数
void dfs(int id, int nowlevel){
maxL = max(maxL, nowlevel); //记录最大层数
if(child[id].empty()) //如果该点没有孩子表明其为叶子结点
cnt[nowlevel]++; //其对应层的叶子结点数量加一
for(auto i : child[id]) //搜索其所有的孩子
dfs(i, nowlevel + );
}
int main()
{
scanf("%d%d", &n, &m); //输入总人数与非叶子结点数
for(int i = ; i < m; i++){ //输入所有非叶子结点信息
int id, k; //该点id与孩子数量k
scanf("%d%d", &id, &k);
for(int j = ; j < k; j++){ //输入该点所有孩子
int cid;
scanf("%d", &cid);
child[id].push_back(cid); //记录该点的孩子
}
}
dfs(, ); //1为根结点,由根结点第0层开始搜索
for(int i = ; i <= maxL; i++){
if(i != )
printf(" ");
printf("%d", cnt[i]);
}
return ;
}

PTA (Advanced Level) 1004 Counting Leaves的更多相关文章

  1. PAT (Advanced Level) 1004. Counting Leaves (30)

    简单DFS. #include<iostream> #include<cstring> #include<cmath> #include<algorithm& ...

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

  3. PAT Advanced 1004 Counting Leaves

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

  4. 1004. Counting Leaves (30)

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

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

  7. PAT 1004 Counting Leaves (30分)

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

  8. 1004 Counting Leaves (30分) DFS

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

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

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

随机推荐

  1. POJ1274 The Perfect Stall

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 25739   Accepted: 114 ...

  2. poj3061

    #include<stdio.h> #include<iostream> using namespace std; #include<algorithm> cons ...

  3. SVN代码管理发布

    1.svn的独立模式应用 2.svn钩子的应用(例如:代码提交前的文件格式限制,大小限制,代码发布svn成功后的备份等等) 3.大型企业的代码发布流程 有一些制度流程.逻辑方案 4.业务变更管理

  4. Java学习--循环语句

    1. break public class BreakDemo{ // 完成一个四则运算的功能 public static void main(String args[]){ for(int i=0; ...

  5. Linux-帮助与基本命令

    1.获取命令帮助 Linux中的命令按可分类shell内嵌命令.外部命令,获取命令帮助信息前需要区分命令类型. 1.1 type - 查看命令类型 [root@VM_0_171_centos ~]# ...

  6. NW.js安装原生node模块node-printer控制打印机

    1.安装原生node模块 #全局安装nw-gyp npm install -g nw-gyp #设置目标NW.js版本 set npm_config_target=0.31.4 #设置构建架构,ia3 ...

  7. 使用更改跟踪(ChangeTracking)来实现数据类型变更

    在现实场景中,我们经常会遇到修改数据类型的场景,尤其是自增列从INT修改为BIGINT的情况,自增列又通常作为表的主键和聚集索引键,因此修改操作需要按以下步骤来进行 1. 停止对该表的访问(通过禁用权 ...

  8. SQL SERVER锁(LOCK)知识及锁应用

    提示:这里所摘抄的关于锁的知识有的是不同sql server版本的,对应于特定版本时会有问题. 一 关于锁的基础知识 (一). 为什么要引入锁 当多个用户同时对数据库的并发操作时会带来以下数据不一致的 ...

  9. asp.net core 使用identityServer4的密码模式来进行身份认证(2) 认证授权原理

    前言:本文将会结合asp.net core 认证源码来分析起认证的原理与流程.asp.net core版本2.2 对于大部分使用asp.net core开发的人来说. 下面这几行代码应该很熟悉了. s ...

  10. [leetcode.com]算法题目 - Plus One

    Given a number represented as an array of digits, plus one to the number. class Solution { public: v ...