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方式导出EXCEL表格
最近几天做公司项目,应客户需求需要将表单的数据下载本地存成.xls文件.之前做毕设的时候,就有类似这方面的功能需 求,但是当时也没有做就搁浅了下来,这次补上. 一.业务开发描述 二.前台jsp页面及j ...
- js文字颜色闪烁
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Redis挖矿原理及防范
笔者也曾经被挖矿病毒侵袭过,灰常难受,但是其实你只要了解入侵的手段就非常好防范了,今天我们就演示一下如果通过Redis进行提权获取远程服务器的Root用户. 1.首先我们需要一些先决条件 条件一:你首 ...
- JMM和volatile
1.volatile 2.JMM 3.代码示例 package com.yanshu; class MyNmuber{ volatile int number=10; public void addT ...
- 统计单词出现次数的mapreduce
1.新建Java项目 2.导包E:\工具\大数据\大数据提升资料\01-软件资料\06-Hadoop\安装包\Java1.8环境下编译\hadoop-2.7.3\hadoop-2.7.3\share\ ...
- B - 规律题2
10 123456 1:f(y)=1+2+3+4+5+6=21 2:f(y)=3 3 3 b进制,<b; 10 123 1+2+3=6;是3的倍数 1234 =(999)*1+99 ...
- HDU 6762 Mow (2020 Multi-University Training Contest 1 1012) 半平面交
Mow 题目链接 分析 将多边形的边向内部缩 r 个单位长度,然后这些边所围成的内部区域,就是圆心的合法范围,该范围也是一个多边形,假设面积是\(a\),周长是\(b\),那么可以知道圆可以覆盖的面积 ...
- 2018-2019 ACM-ICPC Brazil Subregional Programming Contest PART (10/13)
$$2018-2019 ACM-ICPC Brazil Subregional Programming Contest$$ \(A.Slackline\ Adventure\) \(B.Marbles ...
- Windows10与虚拟机中CentOS-7.2进行telnet通信 出现在端口23处失败【解决】
(telnet服务是由xinetd守护,所以安装和启动都要用到xinetd) 1.先检查CentOS7.0是否已经安装以下几个安装包:telnet-server.telnet.xinetd.命令如下: ...
- Codeforces Round #272 (Div. 2) B. Dreamoon and WiFi (暴力二进制枚举)
题意:给你一个只含\(+\)和\(-\)的字符串,统计它的加减和,然后再给你一个包含\(+,-,?\)的字符串,其中\(?\)可以表示为\(+\)或\(-\),问有多少种情况使得第二个字符串的加减和等 ...