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:

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:

2 1
01 1 02

Sample Output:

0 1

代码:

#include <bits/stdc++.h>
using namespace std; int N, M;
vector<int> v[110];
int vis[110];
int ans[110];
int deep = INT_MIN; void dfs(int st, int depth) {
if(v[st].size() == 0) {
ans[depth] ++;
deep = max(deep, depth);
} for(int i = 0; i < v[st].size(); i ++)
dfs(v[st][i], depth + 1);
} int main() {
scanf("%d%d", &N, &M);
memset(vis, 0, sizeof(vis));
while(M --) {
int p, k;
scanf("%d%d", &p, &k);
for(int i = 0; i < k; i ++) {
int c;
scanf("%d", &c);
vis[c] = 1;
v[p].push_back(c);
}
} dfs(1, 0);
printf("%d", ans[0]);
for(int i = 1; i <= deep; i ++)
printf(" %d", ans[i]);
return 0;
}

  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. 补交20145226蓝墨云班课 -- MyOD

    蓝墨云班课 -- MyOD.java 具体描述: 编写MyOD.java 用java MyOD XXX实现Linux下od -tx -tc XXX的功能. 提交测试代码和运行结果截图,加上学号水印,提 ...

  2. WPF 扩大,回弹效果

    原文:WPF 扩大,回弹效果 <Window x:Class="Fish.AccountBook.View.Test.PanelWindow" xmlns="htt ...

  3. 学习笔记:Oracle的trace文件可见性

    隐藏参数: _trace_files_public 参数 trace文件的默认权限: - r w - r - - - - - 如果设定  trace_files_public参数为 true, 则 t ...

  4. 2 vue 。js

    元素option el element    混淆 webpack 项目依赖 save 开发依赖 虚拟dom的问题 在js的基础上封装的,不是jquery 整个网页就一个实例化对象 v 下一张 怎么做 ...

  5. 第二章:走进shell

    2.2 通过Linux控制台终端访问CLI(command line interface) 打开CLI:control+alt+t   用于设置前景色和背景色的setterm选项 选项 参数 描述 - ...

  6. 模拟UNIX(linux)文件系统

    操作系统课程设计 一.实验内容 1. 题目:模拟UNIX(linux)文件系统                [问题描述] 在任一OS下,建立一个大文件,把它假象成一张盘,在其中实现一个简单的 模拟U ...

  7. 大话 .Net 之内存管理

    在一次偶然的机会中,我来到了恒生的大家庭.又在一次偶然的机会中,我很荣幸的被勇哥信任并让我写一篇季刊的文章.可能人生之中充满了无数次的偶然机会,我们只有抓住眼前的“偶然”,才可以创建人生.当我接到这个 ...

  8. hdu2795 Billboard(线段树单点修改)

    传送门 结点中的l和r表示层数,maxx表示这层最多还剩下多少宽度.根据公告的宽度取找到可以放的那一层 找到后返回层数,并修改maxx #include<bits/stdc++.h> us ...

  9. 转载Linux下开启MySQL日志

    转载https://blog.csdn.net/weixin_38187469/article/details/79273962 开启mysql日志   1.查看日志是否启用 mysql> sh ...

  10. SICP读书笔记 2.5

    SICP CONCLUSION 让我们举起杯,祝福那些将他们的思想镶嵌在重重括号之间的Lisp程序员 ! 祝我能够突破层层代码,找到住在里计算机的神灵! 目录 1. 构造过程抽象 2. 构造数据抽象 ...