PAT甲级——A1004 Counting Leaves
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 <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的更多相关文章
- PAT 甲级 1004 Counting Leaves
https://pintia.cn/problem-sets/994805342720868352/problems/994805521431773184 A family hierarchy is ...
- PAT甲级 1004.Counting Leaves
参考:https://blog.csdn.net/qq278672818/article/details/54915636 首先贴上我一开始的部分正确代码: #include<bits/stdc ...
- PAT甲级1049. Counting Ones
PAT甲级1049. Counting Ones 题意: 任务很简单:给定任何正整数N,你应该计算从1到N的整数的十进制形式的1的总数.例如,给定N为12,在1,10, 11和12. 思路: < ...
- PAT甲1004 Counting Leaves【dfs】
1004 Counting Leaves (30 分) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- PAT Advanced 1004 Counting Leaves
题目与翻译 1004 Counting Leaves 数树叶 (30分) A family hierarchy is usually presented by a pedigree tree. You ...
- 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 ...
- pat 甲级 1049. Counting Ones (30)
1049. Counting Ones (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The tas ...
- PAT 甲级 1049 Counting Ones (30 分)(找规律,较难,想到了一点但没有深入考虑嫌麻烦)***
1049 Counting Ones (30 分) The task is simple: given any positive integer N, you are supposed to co ...
- PAT A 1004. Counting Leaves (30)【vector+dfs】
题目链接:https://www.patest.cn/contests/pat-a-practise/1004 大意:输出按层次输出每层无孩子结点的个数 思路:vector存储结点,dfs遍历 #in ...
随机推荐
- 用PS制作APP的界面图片
今天就教大家怎么做出这种厚度的地方还不是白色的,而是根据界面内容交相呼应的图案的APP界面展示图片. 以苹果5S的尺寸为例. 步骤: 1.新建一个画布尺寸为:640*1136,然后保存,命名如:5S效 ...
- Spark Streaming的简单介绍
本文讲解Spark流数据处理之Spark Streaming.本文的写作时值Spark 1.6.2发布之际,Spark 2.0预览版也已发布,Spark发展如此迅速,请随时关注Spark Stream ...
- 2019-6-23-win10-uwp-开发-CSDN-访问量统计-源代码
title author date CreateTime categories win10 uwp 开发 CSDN 访问量统计 源代码 lindexi 2019-6-23 11:2:1 +0800 2 ...
- [JZOJ3233] 照片
题目 题目大意 有一个\(01\)序列.给你一堆区间,每个区间中有且仅有一个\(1\)点. 问最多的\(1\)点个数. 思考历程 感觉这题特别经典,似乎在哪里见过,又好像没有见过. 一开始朝贪心方面想 ...
- NOI2001 炮兵阵地 洛谷2704
题目描述 司令部的将军们打算在N*M的网格地图上部署他们的炮兵部队.一个N*M的地图由N行M列组成,地图的每一格可能是山地(用"H" 表示),也可能是平原(用"P&quo ...
- github 拷贝项目到本地
第一步,git config --global --list 验证邮箱 第二步,git config --global user.name "yourname",git confi ...
- C++ 中vector数组的使用
(1)头文件:#include<vector>.(2)创建vector对象: vector < 类型 > 名字; 例:vector<int> vec;(3) ...
- SpringBoot--springboot启动类和controller的配置
作为一个springboot初学者,在探索过程中难免遇到一些坑,边看书边动手,发现书本中的版本是1.0,而我使用的是最新版2.0,所以有些东西不能完全按照书本进行操作,因为2.0中已经不支持1.0中的 ...
- 第十篇:javaScript中的JSON总结
参考网站:json中国,MDN json 一.必知基础 JSON 是JavaScript对象文字符号的一个子集,它可以自如的在JavaScript中使用.看下这个对象: var myJSONOb ...
- Java 12 正式发布,8大新特性!
自 2 月 7 日开始,Java/JDK 12 就进入了 RC 阶段.按照发布周期,美国当地时间 3 月 19 日,也就是北京时间 20 号 Java 12 正式发布了! △ JDK 12 GA 发布 ...