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 01level, 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 <iostream>
#include <vector>
#include <queue> using namespace std; //给出一棵树,问每一层的叶子结点数量
//使用BFS或者DFS vector<vector<int>>nodes();
vector<int>depth();
int maxDepth = -; void DFS(int index, int h)
{
maxDepth = maxDepth > h ? maxDepth : h;
if (nodes[index].size() == )//data[index].size() == 0)//即为叶子结点
depth[h]++;//层数 for (int i = ; i < nodes[index].size(); ++i)
DFS(nodes[index][i], h + );
} void BFS( )
{
queue<int>q;
q.push();
vector<int>level(, );//记录节点层数
while (!q.empty())
{
int index = q.front();
q.pop();
maxDepth = maxDepth > level[index] ? maxDepth : level[index];//存储最大的层数
if (nodes[index].size() == )//此节点为叶子节点
depth[level[index]]++;//之所以要记录每个节点的层数,是因为,同一层有多个节点
for (int i = ; i < nodes[index].size(); ++i)
{
level[nodes[index][i]] = level[index] + ;//孩子结点层数比父节点多一层
q.push(nodes[index][i]);//将其孩子全部存入
}
}
} int main()
{
int N, M;//N为节点数目
cin >> N >> M;
for (int i = ; i < M; ++i)
{
int ID, k, a;
cin >> ID >> k;
for (int j = ; j < k; ++j)
{
cin >> a;
nodes[ID].push_back(a);//即为一个节点底下所挂的子节点
}
} //DFS(1,0);
BFS( );
cout << depth[];
for (int i = ; i <= maxDepth; ++i)
cout << " " << depth[i];
cout << endl; return ; }

PAT甲级——A1004 Counting Leaves的更多相关文章

  1. PAT 甲级 1004 Counting Leaves

    https://pintia.cn/problem-sets/994805342720868352/problems/994805521431773184 A family hierarchy is ...

  2. PAT甲级 1004.Counting Leaves

    参考:https://blog.csdn.net/qq278672818/article/details/54915636 首先贴上我一开始的部分正确代码: #include<bits/stdc ...

  3. PAT甲级1049. Counting Ones

    PAT甲级1049. Counting Ones 题意: 任务很简单:给定任何正整数N,你应该计算从1到N的整数的十进制形式的1的总数.例如,给定N为12,在1,10, 11和12. 思路: < ...

  4. PAT甲1004 Counting Leaves【dfs】

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

  5. PAT Advanced 1004 Counting Leaves

    题目与翻译 1004 Counting Leaves 数树叶 (30分) A family hierarchy is usually presented by a pedigree tree. You ...

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

  7. pat 甲级 1049. Counting Ones (30)

    1049. Counting Ones (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The tas ...

  8. PAT 甲级 1049 Counting Ones (30 分)(找规律,较难,想到了一点但没有深入考虑嫌麻烦)***

    1049 Counting Ones (30 分)   The task is simple: given any positive integer N, you are supposed to co ...

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

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

随机推荐

  1. leetcode 题型 数据结构 解法 分类总结

    第2章 线性表 2.1 数组 2.1.1 Remove Duplicates from Sorted Array 2.1.2 Remove Duplicates from Sorted Array I ...

  2. activemq设置后台管理用户名密码,及生产者消息密码

    activemq设置后台管理用户名密码,及生产者消息密码 1.修改conf/activemq.xml 在<broker>标签下,找到</shutdownHooks>标签.在这个 ...

  3. JS关闭当前父级div

    代码: <img src="img/diagram.png" onclick="javascript:this.parentNode.parentNode.remo ...

  4. 【笔记篇】斜率优化dp(四) ZJOI2007仓库建设

    传送门戳这里>>> \(n\leq1e6\), 显然还是\(O(n)\)的做法. 这个题有个条件是只能运往编号更大的工厂的仓库, 这也是写出朴素dp的方程的条件. 我们令\(f[i] ...

  5. 关于用Linux桌面版当工作系统这件事

    Linux稳定性好,Linux软件开放--不过等到决定把Linux当作日常工作用系统时,就一言难尽了-- 我日常工作的需求有: 笔记本扩展屏幕 Golang开发 docker/kubernetes 输 ...

  6. Android 开发 框架系列 OkHttp文件下载功能实现(含断点续传)

    前言 此篇博客只是下载功能的记录demo,如果你还不太了解okhttp可以参考我的另一篇博客https://www.cnblogs.com/guanxinjing/p/9708575.html 代码部 ...

  7. Cookie 干货

    从前端开发看Cookie Cookie是浏览器端的存储机制 存在意义: 为了解决“如何记住用户信息”而发明的: 当用户访问网页时,他的名字可以存储在cookie中 下次用户访问该页面时,cookie会 ...

  8. 尝试 zabbix 小记

    server : Ubuntu 16.04 zabbix: 2.2.23源码包 安装 gcc,curl,make,snmp 软件和zabbix依赖一些php 扩展包 sudo apt-get inst ...

  9. mysql出现You can’t specify target table for update in FROM clause

    在mysql执行下面语句时报错: You can’t specify target table for update in FROM clause UPDATE edu_grade_hgm_1 ' W ...

  10. BeanPostProcessor原理学习

    <Spring源码解析>笔记 1.自定义的BeanPostProcessor @Component public class MyBeanPostProcessor implements ...