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, the number of nodes in a tree, and M (<), 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

DFS:因为要知道有多少层,每次进DFS先更新一下最大层数
#include <bits/stdc++.h>
using namespace std;
int n,k,id,m,k2;
vector<int> v[];
int num[];
int maxn = ;
void find(int a,int b)
{
maxn = max(maxn,b);
if(v[a].size() == ){
num[b] += ;
return ;
}
else{
for(int i=;i<v[a].size();i++){
find(v[a][i],b+);
}
}
}
int main()
{
scanf("%d %d",&n,&m);
memset(num,,sizeof(num));
for(int i=;i<=m;i++)
{
scanf("%d",&id);
scanf("%d",&k);
for(int j=;j<k;j++)
{
scanf("%d",&k2);
v[id].push_back(k2);
}
}
find(,);
for(int i=;i<=maxn;i++)
{
if(i==){
printf("%d",num[i]);
}
else{
printf(" %d",num[i]);
}
}
return ;
}


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

  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. 1004 Counting Leaves (30分) DFS

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

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

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

  4. 1004 Counting Leaves (30 分)

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

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

  6. 1004. Counting Leaves (30)

    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. PAT 1004. Counting Leaves (30)

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

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

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

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

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

随机推荐

  1. Spring源码学习(一)资源加载

    这里先从最简单的一个Spring例子开始. 下面是Spring的context的配置 <?xml version="1.0" encoding="UTF-8&quo ...

  2. 图像处理之图像增强项目---csdn去雾专栏1

    (一)高斯低通滤波去噪 高斯低通滤波器(Gaussian Low Pass Filter)是一类传递函数为高斯函数的线性平滑滤波器.又由于高斯函数是正态分布的密度函数.因此高斯低通滤波器对于去除服从正 ...

  3. 获取iOS系统版本号,慎重使用[[[UIDevice currentDevice] systemVersion] floatValue]——【sdk缺陷】

    iOS 最常见的获取系统版本的方法是: [[[UIDevice currentDevice] systemVersion] floatValue] 可是.这个floatValue是不靠谱的,这也算是i ...

  4. EasyPusher RTSP直播之RTP数据包格式解析

    -本篇由团队成员Fantasy供稿! RTP包头格式 码流总体结构 h264的功能分为两层,视频编码层(VCL)和网络提取层(NAL).H.264 的编码视频序列包括一系列的NAL 单元,每个NAL ...

  5. 程序员必知的8大排序(java实现)

    先来看看8种排序之间的关系:

  6. appium(6)-parts of appium api

    parts of appium api Lock Lock the screen.//锁屏. // java driver.lockScreen(3); // objective c [driver ...

  7. 【Windows核心编程】一个使用内存映射文件进行进程间通信的例子

    进程间通信的方式有很多种,其底层原理使用的都是内存映射文件. 本文实现了Windows核心编程第五版475页上的demo,即使用内存映射文件来在进程间通信. 进程1 按钮[Create  mappin ...

  8. leetcode 747. Largest Number At Least Twice of Others

    In a given integer array nums, there is always exactly one largest element. Find whether the largest ...

  9. 模拟等待事件:db file sequential read

    相关知识: db file sequential read  单块读 optimizer_index_cost_adj 这个初始化参数代表一个百分比,取值范围在1到10000之间.该参数表示索引扫描和 ...

  10. 常用的ES6

    1 let 和 const 作用域: 只在声明指令的块级作用域内有效.① let所声明的变量可以改变,值和类型都可以改变,没有限制. let a = 123 a = 456 // 正确,可以改变 le ...