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甲级真题的更多相关文章

  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. Jenkins(3)拉取git仓库代码,执行python自动化脚本

    前言 python自动化的脚本开发完成后需提交到git代码仓库,接下来就是用Jenkins拉取代码去构建自动化代码了 新建项目 打开Jenkins新建一个自由风格的项目 源码管理 Repository ...

  2. 各个复位标志解析,让我们对MCU的程序的健康更有把控

    作者:良知犹存 转载授权以及围观:欢迎添加微信公众号:Conscience_Remains 总述 曾经开发的时候遇到这样情况,我们开发的设备需要长时间工作上报信息,但是我们在后台查看上报数据,发现设备 ...

  3. 克鲁斯卡尔算法(Kruskal算法)求最小生成树

    题目传送:https://loj.ac/p/10065 1.排序函数sort,任何一种排序算法都行,下面的示例代码中,我采用的是冒泡排序算法 2.寻源函数getRoot,寻找某一个点在并查集中的根,注 ...

  4. vector的几种初始化及赋值方式

    转自https://www.cnblogs.com/quyc/p/12857054.html (1)不带参数的构造函数初始化 //初始化一个size为0的vector vector<int> ...

  5. Java-Graphics类的绘图方法实现

    Java-Graphics(画图类) 就比如画一个矩形,你给出矩形左上角坐标,再给出矩形长度和宽度就可以在JFrame上画出来一个矩形 除了矩形之外,还可以画椭圆.圆.圆弧.线段.多边形.图像等 下面 ...

  6. hdu5365Shortest Path (floyd)

    Problem Description There is a path graph G=(V,E) with n vertices. Vertices are numbered from 1 to n ...

  7. hdu4325 Flowers

    Problem Description As is known to all, the blooming time and duration varies between different kind ...

  8. JavaScript——三

    任务: 其中的"options = options || {}"就代表如果options是一个真的对象,就使用它,否则就给他默认值 在Node函数中: 函数中的this指向wind ...

  9. Codeforces Round #667 (Div. 3) D. Decrease the Sum of Digits (贪心)

    题意:给你一个正整数\(n\),每次可以对\(n\)加一,问最少操作多少次是的\(n\)的所有位数之和不大于\(s\). 题解:\(n\)的某个位置上的数进位,意味这后面的位置都可以被更新为\(0\) ...

  10. Explain 索引优化分析

    Explain 语法 # 语法 explain + DQL语句 mysql> explain select * from city where countrycode ='CHN' or cou ...