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 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<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
typedef struct NODE{
vector<int>child;
int layer;
}node;
node tree[];
int N, M, cnt[] = {,}, depth = -;
void levelOrder(int root){
queue<int> Q;
tree[root].layer = ;
Q.push(root);
while(Q.empty() == false){
int temp = Q.front();
if(tree[temp].layer > depth)
depth = tree[temp].layer;
Q.pop();
if(tree[temp].child.size() == ){
cnt[tree[temp].layer]++;
}
int len = tree[temp].child.size();
for(int i = ; i < len; i++){
tree[tree[temp].child[i]].layer = tree[temp].layer + ;
Q.push(tree[temp].child[i]);
}
}
}
int main(){
int tempc, tempd, tempe;
scanf("%d%d", &N, &M);
for(int i = ; i < M; i++){
scanf("%d%d", &tempc, &tempd);
for(int j = ; j < tempd; j++){
scanf("%d",&tempe);
tree[tempc].child.push_back(tempe);
}
}
levelOrder();
for(int i = ; i <= depth; i++){
if(i != depth)
printf("%d ", cnt[i]);
else printf("%d", cnt[i]);
}
cin >> N;
return ;
}

总结:

1、题意:题目要求计算从根开始每一层的没有孩子的家庭成员。其实从题目上就知道,就是计算每一层的叶节点个数。

2、使用一个hash数组,以层数为下标索引。使用层序遍历来计算出每一个节点的层数。每次访问节点时,如果该节点没有子树,则将其所在layer的hash数组加一。

A1004. Counting Leaves的更多相关文章

  1. PAT A1004 Counting Leaves (30 分)——树,DFS,BFS

    A family hierarchy is usually presented by a pedigree tree. Your job is to count those family member ...

  2. PAT甲级——A1004 Counting Leaves

    A family hierarchy is usually presented by a pedigree tree. Your job is to count those family member ...

  3. PAT_A1004#Counting Leaves

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

  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. PAT1004:Counting Leaves

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

  7. PTA (Advanced Level) 1004 Counting Leaves

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

  8. PAT-1004 Counting Leaves

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

  9. PAT甲1004 Counting Leaves【dfs】

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

随机推荐

  1. Flutter的输入框TextField

    TextFiled组件的API 先来看一下TextFiled的构造方法: const TextField({ Key key, this.controller, this.focusNode, thi ...

  2. Leetcode SingleNumber I & II & III 136/137/260

    SingleNumber I: 题目链接:https://leetcode-cn.com/problems/single-number/ 题意: 给定一个非空整数数组,除了某个元素只出现一次以外,其余 ...

  3. Java Annotation详解 理解和使用Annotation

    系统中用到了java注解: 查了一下如何使用注解,到底注解是什么: (1)创建方法:MsgTrace Java Class==> 在Create New Class中: name:输入MsgTr ...

  4. vue & @on-change !== on-change @on-change === @change

    vue & @on-change !== on-change @on-change === @change https://jsfiddle.net/Lasx1fod/ i-switch ht ...

  5. 日志与python日志组件logging

    1. 日志的相关概念: (1)日志的作用: a. 开发人员进行程序调试 b. 开发人员定位程序故障的位置 c. 运维人员观察应用运行是否正常 (2)日志的等级 a. DEBUG 最详细的日志,用于问题 ...

  6. dw擴展jquery

    https://jingyan.baidu.com/article/90895e0fbbb65764ec6b0bd1.html

  7. MySQL——基础操作

    参考博客:http://www.cnblogs.com/wupeiqi/articles/5713315.html 1.创建用户.授权(默认root,密码为空) 创建: create user 'al ...

  8. Maven依赖范围及传递

    .Maven因为执行一系列编译.测试和部署运行等操作,在不同的操作下使用的classpath不同,依赖范围就是用来控制依赖与三种 classpath(编译classpath.测试classpath.运 ...

  9. 想要配置文件生效 需要通过添加到web.xml加载到内存中

    想要配置文件生效 需要通过添加到web.xml加载到内存中

  10. 基于junit的单元测试类编写

    首先定义抽象类BaseTest package com.geostar.gfstack.operationcenter.common.util; import com.google.gson.Gson ...