题干:

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

题目大意:

给你一棵树,有N个节点和M个非叶子结点(N,M<100),已知所有非叶子节点的编号和对应的孩子节点,让你输出每一个深度有多少个叶子节点。

解题报告:

按照输入建树,然后直接dfs预处理深度,On遍历得出答案即可。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define FF first
#define SS second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;vector<int> tr[222];
int dep[222],ok[222],ans[MAX],n,m;
void dfs(int cur,int fa) {dep[cur] = dep[fa] + 1;int up = tr[cur].size();for(int i = 0; i<up; i++) {int v = tr[cur][i];dfs(v,cur);}
}
int main()
{cin>>n>>m;for(int tmp,id,k,i = 1; i<=m; i++) {scanf("%d%d",&id,&k);ok[id] = 1;for(int j = 1; j<=k; j++) {scanf("%d",&tmp); tr[id].push_back(tmp);}}dfs(1,0);int mx = 0;for(int i = 1; i<=n; i++) {mx = max(mx,dep[i]);if(ok[i] == 1) continue;ans[dep[i]]++;}for(int i = 1; i<=mx; i++) {printf("%d%c",ans[i],i == mx ? '\n' : ' '); }return 0;
}

【PAT - 甲级1004】Counting Leaves (30分) (dfs,递归)相关推荐

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

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

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

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

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

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

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

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

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

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

  6. PAT甲级1155 Heap Paths (30 分):[C++题解]堆、堆的遍历、树的遍历、dfs输出路径、完全二叉树建树

    文章目录 题目分析 题目链接 题目分析 来源:acwing 分析: 堆首先是完全二叉树,所以先建完全二叉树,由于给定的是层序遍历的数据,所以直接用数组即可,注意数组下标从1开始,这样便满足结点u和左儿 ...

  7. PAT甲级1072 Gas Station (30 分):[C++题解]dijkstra算法、最短路

    文章目录 题目分析 题目来源 题目分析 来源:acwing 分析: 所有的dist[ ]都≤Ds:最小的dist[ ]最大; dist[ ] 总和最大. 由于加油站是字符,为了简单起见,将m个加油站编 ...

  8. PAT甲级1107 Social Clusters (30 分):[C++题解]并查集,爱好、人数

    文章目录 题目分析 题目链接 题目分析 来源:acwing 分析: 凭爱好,分人群.注意点:爱好可传递.什么意思?意思是A和B的有共同爱好1, B和C有共同爱好2,那么认为A和C也是同一群人. 按照爱 ...

  9. PAT甲级1103 Integer Factorization (30 分):[C++题解]背包问题,DP解法

    文章目录 题目分析 题目链接 题目分析 分析 把N(样例中N=169)看成背包的体积:把k(样例中k=5)看成背包能承的重量.把这道题转化为二维完全背包问题.由于数据范围给出的次幂P∈[2,7],那么 ...

  10. PAT甲级1139 First Contact (30 分):[C++题解] 图论、暴力枚举两个点、hash映射

    文章目录 题目分析 题目链接 题目分析 来源:acwing 题目分析: 图论模拟题. 给定暗恋的两个人A 和B,需要寻找一对C 和D ,满足:A和C是朋友,C和D是朋友,D和B是朋友.而且A.C同性别 ...

最新文章

  1. 巧解Android时区加载过慢的问题
  2. Mybatis查询可能为null
  3. 记-安装pillow
  4. python数据分析numpy_(转)Python数据分析之numpy学习
  5. svn add Default@2x.png的文件含有@的文件名注意事项
  6. 深入理解 ASP.NET 动态控件 (Part 5 - 编译实验)
  7. java jsp动作_Java中级—JSP九大内置对象和动作
  8. PCL之平面分割模型
  9. javaScript中简单数据类型和复杂数据类型赋值拷贝的理解
  10. sqlmap自动扫描注入点_SQLMAP使用指南[学员作品]
  11. herom2 mysql_Hero引擎 竞标员NPC
  12. 【算法与数据结构】——并查集
  13. SqlServer2008创建用户及授予权限
  14. Masimo与Penington研究所合作,提升公众对处方阿片类药物过量危害的认识
  15. 建数据库表需要注意哪些点
  16. Vdbench的校验原理
  17. 51单片机c语言led灯闪烁实验报告,实验一LED灯闪烁.doc
  18. 《十二》CSS3 Grid 网格布局
  19. Windows服务器搭建Node-Media-Server视频服务器
  20. 学习笔记-PMSM如何根据反电动势计算永磁体磁链

热门文章

  1. IBatis 映射文件 sql 中大于、小于等符号转义
  2. 树的直径,树的最长路dp思想
  3. AndroidManifest.xml文件详解(uses-sdk)
  4. sql SET DATEFORMAT对日期处理的影响
  5. iview 可以选择当天 禁用_人脸识别刚要普及,怎么就被禁用了?|人脸识别|人脸信息|世超|rekognition...
  6. python运行是哪个键_python – 如何使用回车键调用按钮命令
  7. java菜单面板设置完能关闭_用Java创建一个屏幕外框架(或者:当所有应用程序窗口关闭时,如何避免Mac上的空白菜单)?...
  8. 百度推送java_关于百度推送,请教一下大家
  9. java中sql之count,SQL COUNT() 函数--编程学习网
  10. 是人是谁_其实,我们每个人心中都有一把尺子,谁好谁歹谁心里都明白……