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. Spring - MVC - 修改 Java 类后, 触发重启

    1. 概述 学习 Spring MVC 下, 如何可控的触发重启 2. 背景 学习 Spring 场景 有些时候, 改完类, 需要重启 之前有听说, Spring MVC 可以自动重启 于是想, 尝试 ...

  2. Java 前加加和后加加 总结

    public class Test { public static void main(String[] args) { int age = 6; //先自加,再使用(age先自加1,然后再打印age ...

  3. 关于windows nginx不能启动问题的解决,史上最坑系列之一(原文)

    我是直接在官方网址下载windows1.6稳定版的nginx,之所以下载它是因为在window下方便学习,更好的在linux安装和学习nginx. 下载到D:\nginx学习\,解压它,并进入启动它 ...

  4. 浅谈hover用法

    在前端页面制作中,我们时常要用到移动显示.隐藏的动态效果,我们一般采用js来实现此效果.不过在大部分情况下,我们也可以使用hover来实现此动态效果. 在此,我谈一谈我对hover的用法,请看以下代码 ...

  5. python 创建虚拟环境:bat实现一键

    1.New a python project 2.cd %project.home%切换到项目根目录3.运行setup.bat创建venv虚拟环境 (注意内网运行setup.bat需要手动将requi ...

  6. DL4J之CNN对今日头条文本分类

    一.数据集介绍 数据来源:今日头条客户端 数据格式如下: 6551700932705387022_!_101_!_news_culture_!_京城最值得你来场文化之旅的博物馆_!_保利集团,马未都, ...

  7. Python 爬取 热词并进行分类数据分析-[App制作]

    日期:2020.02.14 博客期:154 星期五 [本博客的代码如若要使用,请在下方评论区留言,之后再用(就是跟我说一声)] 所有相关跳转: a.[简单准备] b.[云图制作+数据导入] c.[拓扑 ...

  8. laravel qq邮件配置

  9. 创业学习--《预判行业机会》--B-2.预判模块---HHR计划--以太一堂

    一,<开始学习> 1,行业机会的判断,是可以通过不断地训练提高自己的判准的概率的,要科学思考创业. 2,创业者在行业机会上的三个问题: a. 对市场变化,敏感性太弱,没有洞察行业的意识. ...

  10. 洛谷P1073最优贸易(跑两遍dij)

    题目描述 CC C国有n n n个大城市和m mm 条道路,每条道路连接这 nnn个城市中的某两个城市.任意两个城市之间最多只有一条道路直接相连.这 mmm 条道路中有一部分为单向通行的道路,一部分为 ...