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。同时统计每一层叶子节点数目。

代码:

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int N = 110;
  4. vector<int> root[N];
  5. int n, m;
  6. int level[N], cnt[N];
  7. void BFS(int x) {
  8. queue<int> q;
  9. q.push(x);
  10. level[x] = 1;
  11. int maxLevel = 1;
  12. while(!q.empty()) {
  13. int t = q.front(); q.pop();
  14. if(root[t].size() == 0) cnt[level[t]]++;
  15. for (int i = 0; i < root[t].size(); i++) {
  16. q.push(root[t][i]);
  17. level[root[t][i]] = level[t] + 1;
  18. maxLevel = max(level[root[t][i]], maxLevel);
  19. }
  20. }
  21. for (int i = 1; i <= maxLevel; i++) {
  22. cout << cnt[i];
  23. if (i != maxLevel) cout << " ";
  24. else cout << endl;
  25. }
  26. }
  27. int main() {
  28. cin >> n >> m;
  29. for (int i = 0; i < m; i++) {
  30. int id, k;
  31. cin >> id >> k;
  32. for (int j = 0; j < k; j++) {
  33. int x; cin >> x;
  34. root[id].push_back(x);
  35. }
  36. }
  37. BFS(1);
  38. return 0;
  39. }

1004 Counting Leaves ——PAT甲级真题的更多相关文章

  1. PAT 甲级真题题解(1-62)

    准备每天刷两题PAT真题.(一句话题解) 1001 A+B Format  模拟输出,注意格式 #include <cstdio> #include <cstring> #in ...

  2. 1080 Graduate Admission——PAT甲级真题

    1080 Graduate Admission--PAT甲级练习题 It is said that in 2013, there were about 100 graduate schools rea ...

  3. PAT甲级真题及训练集

    正好这个"水水"的C4来了 先把甲级刷完吧.(开玩笑-2017.3.26) 这是一套"伪题解". wacao 刚才登出账号测试一下代码链接,原来是看不到..有空 ...

  4. PAT 甲级真题题解(63-120)

    2019/4/3 1063 Set Similarity n个序列分别先放进集合里去重.在询问的时候,遍历A集合中每个数,判断下该数在B集合中是否存在,统计存在个数(分子),分母就是两个集合大小减去分 ...

  5. PAT 甲级真题

    1019. General Palindromic Number 题意:求数N在b进制下其序列是否为回文串,并输出其在b进制下的表示. 思路:模拟N在2进制下的表示求法,“除b倒取余”,之后判断是否回 ...

  6. PAT甲级真题 A1025 PAT Ranking

    题目概述:Programming Ability Test (PAT) is organized by the College of Computer Science and Technology o ...

  7. Count PAT's (25) PAT甲级真题

    题目分析: 由于本题字符串长度有10^5所以直接暴力是不可取的,猜测最后的算法应该是先预处理一下再走一层循环就能得到答案,所以本题的关键就在于这个预处理的过程,由于本题字符串匹配的内容的固定的PAT, ...

  8. 1018 Public Bike Management (30分) PAT甲级真题 dijkstra + dfs

    前言: 本题是我在浏览了柳神的代码后,记下的一次半转载式笔记,不经感叹柳神的强大orz,这里给出柳神的题解地址:https://blog.csdn.net/liuchuo/article/detail ...

  9. 1022 Digital Library——PAT甲级真题

    1022 Digital Library A Digital Library contains millions of books, stored according to their titles, ...

随机推荐

  1. Linux提权常用漏洞速查表

    漏洞列表 #CVE #Description #Kernels CVE–2018–18955 [map_write() in kernel/user_namespace.c allows privil ...

  2. jvm学习第一天

    视频教程链接 第一部分-jvm初识 0.jvm概览图 JVM由三个主要的子系统构成 类加载子系统 运行时数据区(内存结构) 执行引擎运行时数据区(内存结构) 1.什么是jvm 定义: ①. JVM 是 ...

  3. Linux上搭建https服务器

    https原理: 步骤:1.客户端浏览器向服务器发送如下信息:(1)客户端支持的SSL/TLS协议的版本号(2)密钥算法套件(3)客户端产生的随机数,用于稍后生成"会话密钥"2.服 ...

  4. Codeforces Round #594 (Div. 2) D1 - The World Is Just a Programming Task (贪心)

    思路:枚举换的位置i,j 然后我们要先判断改序列能否完全匹配 如果可以 那我们就需要把差值最大的位置换过来 然后直接判断就行

  5. CF 666E Forensic Examination 【SAM 倍增 线段树合并】

    CF 666E Forensic Examination 题意: 给出一个串\(s\)和\(n\)个串\(t_i\),\(q\)次询问,每次询问串\(s\)的子串\(s[p_l:p_r]\)在串\(t ...

  6. HDOJ 1348 基本二维凸包问题

    这次写的凸包用的是Graham scan算法 就数据结构上只是简单地运用了一个栈 #include<stdio.h>#include<cmath>#include<alg ...

  7. Codeforces Round #689 (Div. 2, based on Zed Code Competition) E. Water Level (贪心好题)

    题意:你在一家公司工作\(t\)天,负责给饮水机灌水,饮水机最初有\(k\)升水,水的范围必须要在\([l,r]\)内,同事每天白天都会喝\(x\)升水,你在每天大清早可以给饮水机灌\(y\)升水,问 ...

  8. fzu2198 快来快来数一数

    Accept: 204    Submit: 627 Time Limit: 1000 mSec    Memory Limit : 65536 KB  Problem Description n个六 ...

  9. Windows系统自带的DOS窗口

    写在前面: 整理自网络 记录学习过程,方便复习 说明 DOS全称为Disk Operating System,意思是"磁盘操作系统" DOS是个人计算机上的一类操作系统,windo ...

  10. Python内置模块(你还在pip install time?)&& apt-get install -f

    一.内置模块 之前不知道time是python自带的,还用pip安装.......还报错..... Python中有以下模块不用单独安装 1.random模块 2.sys模块 3.time模块 4.o ...