https://pintia.cn/problem-sets/994805342720868352/problems/994805521431773184

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, the number of nodes in a tree, and M (<), the number of non-leaf nodes. Then M lines follow, each in the format:

  1. 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 1leaf node. Then we should output 0 1 in a line.

Sample Input:

  1. 2 1
  2. 01 1 02

Sample Output:

  1. 0 1

代码:

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int N, M;
  5. vector<int> v[110];
  6. int vis[110];
  7. int ans[110];
  8. int deep = INT_MIN;
  9.  
  10. void dfs(int st, int depth) {
  11. if(v[st].size() == 0) {
  12. ans[depth] ++;
  13. deep = max(deep, depth);
  14. }
  15.  
  16. for(int i = 0; i < v[st].size(); i ++)
  17. dfs(v[st][i], depth + 1);
  18. }
  19.  
  20. int main() {
  21. scanf("%d%d", &N, &M);
  22. memset(vis, 0, sizeof(vis));
  23. while(M --) {
  24. int p, k;
  25. scanf("%d%d", &p, &k);
  26. for(int i = 0; i < k; i ++) {
  27. int c;
  28. scanf("%d", &c);
  29. vis[c] = 1;
  30. v[p].push_back(c);
  31. }
  32. }
  33.  
  34. dfs(1, 0);
  35. printf("%d", ans[0]);
  36. for(int i = 1; i <= deep; i ++)
  37. printf(" %d", ans[i]);
  38. return 0;
  39. }

  dfs 

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

  1. PAT甲级 1004.Counting Leaves

    参考:https://blog.csdn.net/qq278672818/article/details/54915636 首先贴上我一开始的部分正确代码: #include<bits/stdc ...

  2. PAT甲1004 Counting Leaves【dfs】

    1004 Counting Leaves (30 分) A family hierarchy is usually presented by a pedigree tree. Your job is ...

  3. PAT Advanced 1004 Counting Leaves

    题目与翻译 1004 Counting Leaves 数树叶 (30分) A family hierarchy is usually presented by a pedigree tree. You ...

  4. PAT A 1004. Counting Leaves (30)【vector+dfs】

    题目链接:https://www.patest.cn/contests/pat-a-practise/1004 大意:输出按层次输出每层无孩子结点的个数 思路:vector存储结点,dfs遍历 #in ...

  5. PAT甲级——A1004 Counting Leaves

    A family hierarchy is usually presented by a pedigree tree. Your job is to count those family member ...

  6. PAT Advanced 1004 Counting Leaves (30) [BFS,DFS,树的层序遍历]

    题目 A family hierarchy is usually presented by a pedigree tree. Your job is to count those family mem ...

  7. 1004 Counting Leaves ——PAT甲级真题

    1004 Counting Leaves A family hierarchy is usually presented by a pedigree tree. Your job is to coun ...

  8. PAT 解题报告 1004. Counting Leaves (30)

    1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is t ...

  9. PAT 1004 Counting Leaves (30分)

    1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is t ...

随机推荐

  1. zookeeper入门实例

    package org.merit.test.zookeepertest; import java.io.IOException;import java.util.List;import java.u ...

  2. Linux下开发python django程序(模板设置和载入数据)

    1.添加templates文件夹 2.修改settings.py文件 import os #引用 os模块 BASE_DIR = os.path.dirname(os.path.dirname(os. ...

  3. easybcd删除win10启动项如何恢复?

    制作windows启动盘: u盘启动 开始安装,选择左下角的"修复计算机": 运行命令 bcdboot c:\windows /l zh-cn 从系统盘C:\Windows目录中复 ...

  4. 【LG3248】[HNOI2016]树

    [LG3248][HNOI2016]树 题面 洛谷 题解 因为每次你加入的点是原树上某一棵子树 那么我们一次加入一个点,代表一棵子树加到大树下面 那么我们要找到一个点在一个大点中用主席树在\(dfs\ ...

  5. P4427 [BJOI2018]求和

    P4427 [BJOI2018]求和 同[TJOI2018]教科书般的扭曲虚空 懒得写了(雾 #include<bits/stdc++.h> #define il inline #defi ...

  6. jQuery瀑布流详解(PC及移动端)

    前言 瀑布流布局已成为当今非常普遍的图片展示方式,无论是PC还是手机等移动设备上.这种布局图片的样式大概分为三种:等高等宽.等宽不等高.等高不等宽,接下来我们就最为普遍的等宽不等高形式来作为示例. 我 ...

  7. Netty源码分析第2章(NioEventLoop)---->第2节: NioEventLoopGroup之NioEventLoop的创建

    Netty源码分析第二章: NioEventLoop   第二节: NioEventLoopGroup之NioEventLoop的创建 回到上一小节的MultithreadEventExecutorG ...

  8. 开源ETL工具kettle系列之常见问题

    开源ETL工具kettle系列之常见问题 摘要:本文主要介绍使用kettle设计一些ETL任务时一些常见问题,这些问题大部分都不在官方FAQ上,你可以在kettle的论坛上找到一些问题的答案 1. J ...

  9. JavaScript学习(2)call&apply&bind&eval用法

    javascript学习(2)call&apply&bind&eval用法 在javascript中存在这样几种特别有用的函数,能方便我们实现各种奇技淫巧.其中,call.bi ...

  10. Windows下 搭建redis集群

    Windows下搭建redis集群教程 一,redis集群介绍 Redis cluster(redis集群)是在版本3.0后才支持的架构,和其他集群一样,都是为了解决单台服务器不够用的情况,也防止了主 ...