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. inux下进程的最大线程数、进程最大数、进程打开的文件数

    inux下进程的最大线程数.进程最大数.进程打开的文件数 2008-12-07 23:48 =========================    如下转载自这里. linux 系统中单个进程的最大 ...

  2. BZOJ1068_压缩_KEY

    题目传送门 区间DP,设f[i][j][0/1]为i~j区间的压缩情况,1表示在插入了一个M. code: /********************************************* ...

  3. Dataguard学习笔记

    主库和备库之间的redo log传递,可以通过如下方式实现: 在主库端: log_archive_dest_1='主库本地路径' log_archive_dest_2='备库的net service名 ...

  4. Velocity学习3

    Velocity快速入门教程 Apache的速度是一个基于Java的的模板引擎(模板引擎),它允许任何人仅仅简单的使用模板语言(模板语言)来引用由java的代码定义的对象. 官网介绍如下: Veloc ...

  5. [ATL/WTL]_[初级]_[选择目录对话框]

    场景 1.起因是创建标准选择目录对话框时使用了 SHCreateItemFromParsingName 函数, 这个函数支持vista以上系统. 之后再winxp上运行就报错: 无法定位程序输入点 S ...

  6. JAVA Eclipse 快捷键 ctrl+f 查找/替换 字符串

  7. jquery.validata1.11怎么支持metadata

    使用metadata方式这个需要使用jquery.metadata.js插件才可工作,通过在表单项中定义特殊的属性来指定验证规则 但是最新的jquery.validate 1.11没有内置metada ...

  8. 应用.NET控制台应用程序开发批量导入程序。

    一.最近一直在调整去年以及维护去年开发的项目,好久没有在进行个人的博客了.每天抽了一定的时间在研究一些开源的框架,Drapper 以及NHibernate以及当前比较流行的SqlSuper框架 并进行 ...

  9. 15-RUN vs CMD vs ENTRYPOINT

    RUN.CMD 和 ENTRYPOINT 这三个 Dockerfile 指令看上去很类似,很容易混淆.本节将通过实践详细讨论它们的区别. 简单的说: RUN 执行命令并创建新的镜像层,RUN 经常用于 ...

  10. CF刷题-Codeforces Round #481-F. Mentors

    题目链接:https://codeforces.com/contest/978/problem/F 题目大意: n个程序员,k对仇家,每个程序员有一个能力值,当甲程序员的能力值绝对大于乙程序员的能力值 ...