题目

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


题解

#include<iostream>
#include<vector>
using namespace std;
vector<int> v[100];
int book[100],maxlevel=-1;
void dfs(int root,int level){if(level>maxlevel){maxlevel=level;}if(v[root].size()==0){book[level]++;}else {for(int i=0;i<v[root].size();i++)dfs(v[root][i],level+1);}}int main(){int n,m,parent,k,child;scanf("%d %d",&n,&m);for(int i=0;i<m;i++){scanf("%d %d",&parent,&k);//少了一个%d导致错误for(int j=0;j<k;j++){scanf("%d",&child);v[parent].push_back(child);}}dfs(1,0);printf("%d",book[0]);for(int i=1;i<=maxlevel;i++)printf(" %d",book[i]);return 0;
}

不太懂DFS中的return写法,具体注释稍后加上。

PAT甲级1004 (DFS,树的父子节点)相关推荐

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

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

  2. 关于树的父子节点的图形化展示

    功能需求是实现树的父子节点的图形展示关系,且可以点击节点的时候,会相应地切换显示.因此网上寻找相关的插件来看看是不是满足需求.参考了许多的博客,感觉最有价值的就是这篇博客http://www.cnbl ...

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

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

  4. PAT 甲级 1004

    家谱统计 题目思路: 本题其实不用建立树的结构,可以把这个题目看成一个使用std::vector的DFS练习.在输入的时候,把每一层的编号看成以该层的根的编号,该层vector存储以该层为根,孩子的编 ...

  5. PAT甲级1115 DFS和BST

    题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following proper ...

  6. PAT甲级真题目录(按题型整理)(转自柳神)

    转载自:https://www.liuchuo.net/archives/2502?tdsourcetag=s_pcqq_aiomsg 最短路径 1003. Emergency (25)-PAT甲级真 ...

  7. 仙人掌相关问题的解法(1)-DFS树解决仙人掌DP问题,圆方树

    前言 有难度的仙人掌题在近几年也只是在国家集训队水平的比赛里才会出现. 不过,这不是说仙人掌对国集水平以下的选手意义不大: 首先,仙人掌暴力 DP 问题难度并不大,在省选. NOI 甚至 NOIP 中 ...

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

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

  9. PAT甲级1021 Deepest Root :[C++题解]树的最大深度、并查集、dfs求树的深度

    文章目录 题目分析 题目链接 题目分析 分析: 考察知识点:并查集.dfs.树的深度 给定n个结点,n-1条边,只要能保证只有1个连通分量,就是一棵树.否则的话就不是树,它是不连通的. 用并查集来看是 ...

最新文章

  1. pip install可能遇到的一些问题
  2. MVC中session创建并获取问题
  3. linux 视频转gif,GitHub - alinuxsa/VideoToGIF: video to gif 视频转gif制作表情包插件(可以添加文字)...
  4. python线程状态_Python线程
  5. java 输入16进制_尝试使用十六进制输入来使用小端和大端
  6. Trident State译文
  7. HTTPS-客户端与服务器三次握手过程(含wireshark分析)
  8. 服务器打包运营级H5商城源码
  9. STM32基础定时器详解
  10. 银行存款都有哪些误区,你都有踩坑吗?
  11. HashMap keyset()方法
  12. mysql 升级高可用_MySQL高可用方案升级规划
  13. 第五次作业:Excel制作英文课程表
  14. SV宏定义中反斜杠(\),反引号(`),双反引号(``)的作用
  15. 计算机显示器屏幕什么也不显示,电脑显示器黑屏(显示屏不显示)四种解决方法...
  16. 253短信发送(国内短信)
  17. 2018微信公开课:微信小游戏的精华内容要点分享!
  18. 误删的文件怎么寻回?
  19. “本地资源检测” 上手指南,玩转最前沿的优化黑科技!
  20. webstrom怎么配置git并提交

热门文章

  1. python实现高精度加法_蓝桥杯-Python-高精度加法
  2. 机器视觉光源学习总结——平行背光源
  3. android 3d侧拉抽屉,iOS动画指南 - 4.右拉的3D抽屉效果
  4. 终于找到了。 图标搜索、UI设计、移动开发集中导航
  5. c++全局变量的声明和使用
  6. NGINX上PHP header问题
  7. RFC2616中文版(8)连接
  8. 系出名门Android(9) - 数据库支持(SQLite), 内容提供器(ContentProvider)
  9. DIY购车入门5步法则
  10. 华为机试HJ62:查找输入整数二进制中1的个数