1004. Counting Leaves (30)

时间限制
400 ms

内存限制
65536 kB

代码长度限制
16000 B

判题程序
Standard

作者
CHEN, Yue

A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.

Input

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.

Output

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


提交代码

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<stack>
 5 #include<set>
 6 #include<map>
 7 #include<queue>
 8 #include<algorithm>
 9 using namespace std;
10 vector<int> node[105];
11 int sum[105];
12 int main(){
13     //freopen("D:\\INPUT.txt","r",stdin);
14     int n,m;
15     scanf("%d %d",&n,&m);
16     int i,j,num,first,v;
17     for(i=1;i<=m;i++){
18         scanf("%d %d",&first,&num);
19         while(num--){
20             scanf("%d",&v);
21             node[first].push_back(v);
22         }
23     }
24     int last,e,cur,level=1;
25     queue<int> q;
26     if(node[1].size()){
27         last=e=1;
28         q.push(1);
29         level++;
30     }
31     else{
32         sum[level]++;
33     }
34
35     //cout<<level<<endl;
36
37     while(!q.empty()){
38         cur=q.front();
39
40         //cout<<cur<<endl;
41
42         q.pop();
43         for(j=0;j<node[cur].size();j++){
44             if(node[node[cur][j]].size()){
45                 q.push(node[cur][j]);
46                 last=node[cur][j];
47             }
48             else{
49                 sum[level]++;
50             }
51         }
52         if(e==cur){
53             e=last;
54             level++;//指向下一层
55
56             //cout<<level<<endl;
57
58         }
59     }
60
61     //cout<<level<<endl;
62
63     printf("%d",sum[1]);
64     for(i=2;i<level;i++){
65         printf(" %d",sum[i]);
66     }
67     printf("\n");
68     return 0;
69 }

转载于:https://www.cnblogs.com/Deribs4/p/4802201.html

pat1004. Counting Leaves (30)相关推荐

  1. PAT甲级1004 Counting Leaves (30分):[C++题解]树、邻接表存储树、dfs遍历树

    文章目录 题目分析 题目链接 题目分析 题意重述:一棵树,求每一层的叶子节点数目. 分析 构造树,使用邻接表来存(相当于存储有向图). 需要一个头结点数组h[N],然后每个头节点往外形成一个单链表e[ ...

  2. 1004. Counting Leaves (30)

    时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A family hierarchy is usually pr ...

  3. 1004 Counting Leaves (30分) (vector实现)

    题解: 用vector邻接表建图,两个邻接表实现层序遍历. 有注释在代码 代码: /*Keep on going Never give up*/ #pragma GCC optimize(3,&quo ...

  4. 1004 Counting Leaves (30 分)【难度: 中 / 知识点: 树的遍历】

    题目意思: 求每一层的叶子结点数 方法一: 深搜 数组模拟存储邻接表 #include<bits/stdc++.h> using namespace std; const int N=1e ...

  5. 【两种解法】1004 Counting Leaves (30 分)_27行代码AC

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 A family hierarchy is usually presented by a pedigree tree. Your ...

  6. 【PAT - 甲级1004】Counting Leaves (30分) (dfs,递归)

    题干: A family hierarchy is usually presented by a pedigree tree. Your job is to count those family me ...

  7. PAT甲级1049 Counting Ones (30 分):[C++题解]统计1的个数、数位统计

    文章目录 题目分析 题目链接 题目分析 来源:PAT网站 分析: 以数字abcdefg这个7位数字为例,说一下本题的思路. 1)数字1在每一位出现的次数. 2)以第d位为例,第d位的取值可以分为3种情 ...

  8. pat1049. Counting Ones (30)

    1049. Counting Ones (30) 时间限制 10 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The task ...

  9. 1049. Counting Ones (30)

    题目如下: The task is simple: given any positive integer N, you are supposed to count the total number o ...

最新文章

  1. Python JAVA Solutions for Leetcode
  2. 选择排序 插入排序 交换排序
  3. 使用InternalsVisibleToAttribute给assembly添加“友元assembly”特性遭遇强签名
  4. JSP自定义标签rtexprvalue属性 1
  5. Linux Shell脚本之利用mysqldump备份MySQL数据库(详细注解)
  6. Python语言解析xml文件
  7. Java SE 9:不可变Map和Map.Entry的工厂方法
  8. 发那科机器人编程软件fanuc roboguide授权补丁_工业机器人离线编程与应用:ROBOGUIDE V8.3版本的工程文件创建...
  9. EASBOS获取系统状态控制期间
  10. vb.net 遍历目录 按日期排序_之家小程序轻松玩转仿通讯录拼音首字母排序
  11. Qt图像中心旋转的两种方法
  12. •SVG g、defs、symbol和use元素详解
  13. [ECharts] DEPRECATED: ‘normal‘ hierarchy in itemStyle has been removed since 4.0. All style properti
  14. 成功解决使用import或from...import...导入文件时报错
  15. 机器视觉——VisionPro应用开发指南
  16. 你深入解析过java虚拟机:C1编译器,从HIR到LIR吗?
  17. Autolayout布局相关和UIStackView
  18. DROID-SLAM: Deep Visual SLAM for Monocular, Stereo, and RGB-D Cameras论文阅读笔记
  19. 我的NVIDIA开发者之旅-Jetson Nano 2gb教你怎么训练模型(完整的模型训练套路)
  20. 星球壁纸android,Planets pack

热门文章

  1. 同步SQL Server 2000 数据库
  2. 快排递归非递归python_Python递归神经网络终极指南
  3. 使用TensorFlow进行机器学习即服务
  4. mysql 判断日期是否在某范围内_判断时间是否在某个区间内
  5. 漫谈回溯(未完待续)
  6. CSP 202006-1 线性分类器 python实现+详解
  7. 遂宁专业计算机学校,遂宁计算机专业中职学校哪家强
  8. Java虚拟机的内存空间有几种
  9. 小扎的野心不止做社交 市政厅的上线说明了这一点
  10. Android的ToolBar