1004 Counting Leaves ——PAT甲级真题
1004 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<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
题目大意:统计一颗树每一层叶子节点的数目。
大致思路:层序遍历这颗树,设置level数组,其中level(child) = level(parent) + 1。同时统计每一层叶子节点数目。
代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
vector<int> root[N];
int n, m;
int level[N], cnt[N];
void BFS(int x) {
queue<int> q;
q.push(x);
level[x] = 1;
int maxLevel = 1;
while(!q.empty()) {
int t = q.front(); q.pop();
if(root[t].size() == 0) cnt[level[t]]++;
for (int i = 0; i < root[t].size(); i++) {
q.push(root[t][i]);
level[root[t][i]] = level[t] + 1;
maxLevel = max(level[root[t][i]], maxLevel);
}
}
for (int i = 1; i <= maxLevel; i++) {
cout << cnt[i];
if (i != maxLevel) cout << " ";
else cout << endl;
}
}
int main() {
cin >> n >> m;
for (int i = 0; i < m; i++) {
int id, k;
cin >> id >> k;
for (int j = 0; j < k; j++) {
int x; cin >> x;
root[id].push_back(x);
}
}
BFS(1);
return 0;
}
1004 Counting Leaves ——PAT甲级真题的更多相关文章
- PAT 甲级真题题解(1-62)
准备每天刷两题PAT真题.(一句话题解) 1001 A+B Format 模拟输出,注意格式 #include <cstdio> #include <cstring> #in ...
- 1080 Graduate Admission——PAT甲级真题
1080 Graduate Admission--PAT甲级练习题 It is said that in 2013, there were about 100 graduate schools rea ...
- PAT甲级真题及训练集
正好这个"水水"的C4来了 先把甲级刷完吧.(开玩笑-2017.3.26) 这是一套"伪题解". wacao 刚才登出账号测试一下代码链接,原来是看不到..有空 ...
- PAT 甲级真题题解(63-120)
2019/4/3 1063 Set Similarity n个序列分别先放进集合里去重.在询问的时候,遍历A集合中每个数,判断下该数在B集合中是否存在,统计存在个数(分子),分母就是两个集合大小减去分 ...
- PAT 甲级真题
1019. General Palindromic Number 题意:求数N在b进制下其序列是否为回文串,并输出其在b进制下的表示. 思路:模拟N在2进制下的表示求法,“除b倒取余”,之后判断是否回 ...
- PAT甲级真题 A1025 PAT Ranking
题目概述:Programming Ability Test (PAT) is organized by the College of Computer Science and Technology o ...
- Count PAT's (25) PAT甲级真题
题目分析: 由于本题字符串长度有10^5所以直接暴力是不可取的,猜测最后的算法应该是先预处理一下再走一层循环就能得到答案,所以本题的关键就在于这个预处理的过程,由于本题字符串匹配的内容的固定的PAT, ...
- 1018 Public Bike Management (30分) PAT甲级真题 dijkstra + dfs
前言: 本题是我在浏览了柳神的代码后,记下的一次半转载式笔记,不经感叹柳神的强大orz,这里给出柳神的题解地址:https://blog.csdn.net/liuchuo/article/detail ...
- 1022 Digital Library——PAT甲级真题
1022 Digital Library A Digital Library contains millions of books, stored according to their titles, ...
随机推荐
- java-poi创建模板
package com.jy.demo.web; import java.io.FileOutputStream; import org.apache.poi.ss.usermodel.Cell; i ...
- (29)Vim 5
Vim显示行号方法详解1.编辑时显示行号 在命令模式下输入":set nu"即可显示每一行的行号 如果想要取消行 号,则再次输入":set nonu"即可.2. ...
- 子网划分、变长子网掩码和TCP/IP排错__散知识点
1.IP零子网(ip subnet-zero):这个命令可以允许你在自己的网络设计中使用第一个和最后一个子网.例如,C类掩码192通常只可以提供子网64和128,但在使用了ip subnet-zero ...
- springboot基础配置-->Properties配置
Spring Boot项目中的application.properties配置文件一共可以出现在如下4个位置: 项目根目录下的config文件夹中. 项目根目录下. classpath下的config ...
- 【实战】通过Python实现疫情地图可视化
目录 一. json模块 二.通过Python实现疫情地图可视化 2.将json格式的数据保存到Excel 3.应用pyecharts进行数据可视化 一. json模块 JSON(JavaScript ...
- burpsuite是用教程
1.下载burp suite工具 首先要明确,需要java环境,并且配置java环境变量 可以通过吾爱破解下载工具 工具: 双击打开后直接点next --> start burpsuite: 3 ...
- VS CODE一些常见配置操作(快捷键设置、C/C++的debug、代码路径配置)
总述 今天来一篇简单的操作文章吧,VSCODE是我们经常用的软件,我之前也写过关于VSCODE远程办公的一些的操作(有兴趣的朋友可以点击进去看看),今天我再稍微介绍一些我其他地方用到的一些操作 ...
- B - B Saruman's Army(贪心)
在一条直线上,有n个点.从这n个点中选择若干个,给他们加上标记.对于每一个点,其距离为R以内的区域里必须有一个被标记的点.问至少要有多少点被加上标记 Saruman the White must le ...
- 【LA 3487】Duopoly(图论--网络流最小割 经典题)
题意:C公司有一些资源,每种只有1个,有A.B两个公司分别对其中一些资源进行分组竞标,每组竞标对一些资源出一个总价.问C公司的最大收益. 解法:最小割.将A公司的竞标与源点相连,B公司的与汇点相连,边 ...
- 使用eclipse搭建第一个java web应用
一. 首先是eclipse得下载,你要下载Eclipse IDE for Java EE这种类型的,我之前下载的Eclipse IDE for Enterprise Java Developers是官 ...