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 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的更多相关文章
- 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 ...
随机推荐
- 解决maven项目创建过慢的问题以及快捷键
分别在创建项目时填入以下值: Name:archetypeCatalogValue:internal idea常用的快捷键 Alt+回车 导入包,自动修正 Ctrl+N 查找类 Ctrl+Shift+ ...
- opencv3.1.0 在控制台程序中报错:winnt.h(6464): error C2872: ACCESS_MASK: 不明确的
在winnt.h里面有一个cv的命名空间,同样定义了一个ACCESS_MASK,跟opencv的cv::ACCESS_MASK发生了冲突!!! 该冲突在MFC中没有出现,在控制台程序中才会报错!对于o ...
- amaze UI(mark)
为移动而生 Amaze UI 以移动优先(Mobile first)为理念,从小屏逐步扩展到大屏,最终实现所有屏幕适配,适应移动互联潮流. 组件丰富,模块化 Amaze UI 含近 20 个 CSS ...
- this 、typeof、false、parseInt()、this、arguments、Array和object判断
typeof typeof (undefined) 不会报错 undefined object Number boolean function String 返回值为字符串类型 false .fals ...
- 分布式唯一ID实现
ID生成的核心需求 全局唯一 趋势有序 为什么要全局唯一 避免ID冲突 著名的例子就是身份证号码,身份证号码确实是对人唯一的,然而一个人是可以办理多个身份证的,例如你身份证丢了,又重新补办了一张,号码 ...
- 调整VMware中虚拟机中Linux系统屏幕分辨率的问题
即:解决显示底部菜单栏以及安装软件中没有下一步的按钮用Tab键的问题 右击打开终端,输入以下命令: Xrandr Xrandr -s 1280x720 (自己选择一个分辨率即可) 原文:https: ...
- Windbg 查看SSDT表
SSDT HOOK 的原理其实非常简单,我们先实际看看KeServiceDescriptorTable是什么样的. lkd> dd KeServiceDescriptorTabl ...
- Pascal 排序算法
Pascal 排序 排序 排序就是将杂乱无章的数据元素,通过一定的方法按关键字顺序排列的过程.排序问题是一个十分重要的问题,并且排序的方法有很多种: 例子:输入20个数,将它们按照从高到低的次序排 ...
- HTML <body>的常用属性
bgColor : 网页背景色 如 <body bgColor="red"> Background : 网页背景图片的地址 如 <body background ...
- SpringBoot--Thymeleaf入门使用
一.概述 今天学习到了SpringBoot中的WEB开发,SpringBoot提供了spring-boot-stater-web为web开发给予支持,它里面内嵌了以下依赖: <dependenc ...