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



题目大意:计算树的每一层上有多少个叶子节点并按层输出

分析:使用深度有限搜索(DFS)递归遍历树上每一个节点的孩子节点,如果这个节点没有孩子节点,就逐层返回。child[i]集合记录每个节点的孩子节点,leaf[i]数组记录每一层上的叶子节点,max_h记录最大层数,层数从1开始。也可以使用广度优先搜索(BFS),不同之处是DFS使用集合,BFS使用队列,且BFS不用使用递归,只需要第一个节点入队列后判断队列非空即可。

//DFS求叶子节点
#include <iostream>
#include <vector>
using namespace std;
int leaf[100],max_h=1;
vector<int> child[100];
void DFS(int id_num,int h)
{
if(max_h<h) max_h=h;
int k=child[id_num].size();
if(k==0){
leaf[h]+=1;
return;
}
for(int i=0;i<k;i++)
{
DFS(child[id_num][i],h+1);
}
}
int main() {
int n,m;
scanf("%d %d",&n,&m);
int id_num,k,id;
for(int i=0;i<m;i++){
scanf("%d %d",&id_num,&k);
for(int j=0;j<k;j++){
scanf("%d",&id);
child[id_num].push_back(id);
}
}
DFS(1,1);
for(int i=1;i<=max_h;i++){
if(i!=1) printf(" ");
printf("%d",leaf[i]);
}
printf("\n");
return 0;
}

1004. Counting Leaves(30)—PAT 甲级的更多相关文章

  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 (30分)

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

  3. 1004. Counting Leaves (30)

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

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

  5. 1004 Counting Leaves (30分) DFS

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

  6. 【PAT甲级】1004 Counting Leaves (30 分)(BFS)

    题意:给出一棵树的点数N,输入M行,每行输入父亲节点An,儿子个数n,和a1,a2,...,an(儿子结点编号),从根节点层级向下依次输出当前层级叶子结点个数,用空格隔开.(0<N<100 ...

  7. PAT 1004. Counting Leaves (30)

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

  8. PAT A 1004. Counting Leaves (30)【vector+dfs】

    题目链接:https://www.patest.cn/contests/pat-a-practise/1004 大意:输出按层次输出每层无孩子结点的个数 思路:vector存储结点,dfs遍历 #in ...

  9. 【PAT Advanced Level】1004. Counting Leaves (30)

    利用广度优先搜索,找出每层的叶子节点的个数. #include <iostream> #include <vector> #include <queue> #inc ...

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

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

随机推荐

  1. Example of assigning attributes directly to an object name

    Student类 package com.itheima_05; /* * 学生类 * * 通过对象直接访问成员变量,会存在数据安全问题 * 这个时候,我们就想能不能不让外界的对象直接访问成员变量呢? ...

  2. MUI框架-09-MUI 与后台数据交互

    MUI框架-09-MUI 与后台数据交互 本篇介绍使用 art-template 和原生 MUI 的数据交互 mui.ajax 来实现 我们大家都知道,想要数据交互就要有数据,每次当我们发送请求,我们 ...

  3. Socket(套接字) IP TCP UDP HTTP

    Socket(套接字) 阮老师的微博 (转)什么是套接字(Socket)? 应用层通过传输层进行数据通信时,TCP和UDP会遇到同时为多个应用程序进程提供并发服务的问题.多个TCP连接或多个应用程序进 ...

  4. json 对象里面含有 =的解决办法

    今天通过restful 调用接口的时候,遇到这样的问题,通过接口返回的数据如下: { "code": 0, "message": "成功", ...

  5. ActiveMQ5.8.0安装及启动

    一.下载 官网地址:http://activemq.apache.org/download.html Windows版本:apache-activemq-5.8.0-bin.zip Linux版本:a ...

  6. mongodb节点配置指南

    修改复制集节点的优先级 复制集节点的 priority 参数的值决定了选举中该节点的优先级.值越高,优先级越高. 我们可以通过修改复制集配置参数中 members 数组位置的优先级来修改对应机器的优先 ...

  7. [翻译] VICMAImageView

    VICMAImageView https://github.com/vitoziv/VICMAImageView Change image view's content mode with your ...

  8. Python学习---Model拾遗[1]180318

    Model: 强大的数据库操作,弱小的数据验证 Form:  强大的数据验证 ModelForm: 强大的数据验证 + 弱小的数据库操作 Model拾遗 Model基本操作 1. 创建数据库表2. 修 ...

  9. 申请MVP奖励时的小Tips

    大家新年好,今天MSPrecious为大家带来一些申请MVP奖励时的小Tips.   本文分为三个部分 MVP是什么 如何申请MVP 申请MVP需要注意的事项 MVP是什么? 我想,点进来看这篇文章的 ...

  10. php测试工具

    如果是测压力有apache的ab如果要看性能则有xdebug和xhprof.还有linux的strace命令来跟踪程序的执行时的系统调用