1004 Counting Leaves (30分)

 

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

Sample Input:

2 1
01 1 02

Sample Output:

0 1

题意:
  给一棵树,求这棵树中每一层中,没有子节点的节点个数。第一行输入两个数n,m;分别表示这棵树节点和叶子节点个数,第二行依次输入节点编号和这个节点子节点个数 题解:
  用vector数组保存每个节点的儿子节点,然后用DFS依次遍历每个深度的每个节点,数组记录每层子节点数为0的节点即可
#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#define MAX 1000000
using namespace std;
int num[],vis[];
vector<int>v[];
int cnt=;
void dfs(int root,int dep)
{
vis[root]=;
if(v[root].size()==)
{
num[dep]++;
cnt=cnt<dep?dep:cnt;//记录最大深度
}
else
{
for(int i=;i<v[root].size();i++)//遍历儿子节点
{
if(vis[v[root][i]]==)
dfs(v[root][i],dep+);
}
}
}
int main()
{
int n,m,id,k;
cin>>n>>m;
for(int i=;i<m;i++)
{
cin>>id>>k;
for(int i=;i<k;i++)
{
int chi;//儿子节点
cin>>chi;
v[id].push_back(chi);
}
}
dfs(,);
for(int i=;i<=cnt;i++)
{
if(i==)
cout<<num[i];
else
cout<<' '<<num[i];
}
cout<<endl;
return ;
}

  

1004 Counting Leaves (30分) DFS的更多相关文章

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

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

  3. 1004 Counting Leaves (30 分)

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

  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)

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

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

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

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

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

  8. PAT甲题题解-1004. Counting Leaves (30)-统计每层叶子节点个数+dfs

    统计每层的叶子节点个数建树,然后dfs即可 #include <iostream> #include <cstdio> #include <algorithm> # ...

  9. PAT Advanced 1004 Counting Leaves (30) [BFS,DFS,树的层序遍历]

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

随机推荐

  1. 【Python】使用socketserver建立一个异步TCP服务器

    概述 这篇文章是讲解如何使用socketserver建立一个异步TCP服务器,其中Python版本为3.5.1. socketserver主要的类 socketserver模块中的类主要有以下几个:1 ...

  2. windows安装jenkins及ant/maven/jdk配置

    一.jenkins安装 下载地址:https://jenkins.io/download/,下载下来为一个war文件 (1)第一种启动方式,电脑一启动,jenkins会自动运行 命运行运行 java  ...

  3. centos平台搭建Oracle11g数据库+远程连接

    经过了几天的摸爬滚打,终于成功的能在宿主机上(window10上的Plsql)去成功的连上虚拟机上的centos数据库 下面将自己的经验分享给大家: 具体的centos7.centos6上安装Orac ...

  4. aarch64环境下,搭建并配置服务器tomcat:

    aarch64环境下,搭建并配置服务器tomcat: 环境说明及下载相关文件: 1. ARM环境:aarch64开发板 2.JDK安装包: jdk-8u231-linux-arm64-vfp-hflt ...

  5. pyqt:布局删除小部件

    参照:https://stackoverflow.com/questions/5899826/pyqt-how-to-remove-a-widget import sip layout.removeW ...

  6. springboot去除内嵌tomcat和打包在tomcat中运行需要做的步骤

    去除内嵌tomcat和添加jsp依赖 去除内嵌tomcat 在springboot启动依赖中去除内嵌tomcat <dependency> <groupId>org.sprin ...

  7. 网络辅助北斗/GPS位置服务平台业务量突破10亿次

    导读 北斗卫星导航系统日渐成熟,相关服务也在逐步丰富.深入.为了推动北斗定位功能在手机中的普及,中国信息通信研究院2017年就发布了网络辅助北斗/GPS位置服务平台,支持95%以上商用芯片及终端的北斗 ...

  8. 【C语言】创建一个函数,利用该函数将字符串中的小写字母转换为大写字母

    原理: 这类题目主要通过ASCII(美国信息交换标准代码)码差值实现,A对应ASCII码十进制数字是65,a对应ASCII码十进制数字是97,即大小写字母之间ASCII码差值为32,想要将大写字母转换 ...

  9. python manage.py shell

    启动python有两种方式:python manage.py shell和python. 这两个命令 都会启动交互解释器,但是manage.py shell命令有一个重要的不同: 在启动解释器之前,它 ...

  10. SpringBoot与Shiro整合

    一.数据库设计 ​ 这里主要涉及到五张表:用户表,角色表(用户所拥有的角色),权限表(角色所涉及到的权限),用户-角色表(用户和角色是多对多的),角色-权限表(角色和权限是多对多的).表结构建立的sq ...