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. CentOS7 初始化配置

    一.在安装的时候配置网卡名称的参数 . 选择“Install Centos ” . 按Tab,打开kernel启动选项后,增加 net.ifnames= biosdevname= 二.最小化安装完成之 ...

  2. 洛咕 P3961 [TJOI2013]黄金矿工

    甚至都不是树形背包= = 把每条线抠出来,这一条线就是个链的依赖关系,随便背包一下 // luogu-judger-enable-o2 #include<bits/stdc++.h> #d ...

  3. 项目中 Spring 配置文件的选型问题 (xml和注解的抉择)

    要改的东西用xml 不改的东西用注解:这句是对用不用注解或者 xml的一个不错的解决方案 xml使用场景: 1.外部jar包依赖bean配置 2.用注解无法实现,或者用注解无法轻易实现的情形 3.项目 ...

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

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

  5. 约束4:唯一约束,Check约束和null

    大家知道,关系型数据库的逻辑运算的结果是三值型的,TRUE,FALSE和UNKNOWN,特别是,NULL值和任何值都不相等,任何值和NULL的比较,返回的逻辑结果都是unknown.而NULL值在唯一 ...

  6. Java 图片处理解决方案:ImageMagick 快速入门教程

    文章首发于[博客园-陈树义],点击跳转到原文Java 图片处理解决方案:ImageMagick 快速入门教程. ImageMagick介绍 ImageMagick是一个免费的创建.编辑.合成图片的软件 ...

  7. 使用web api开发微信公众号,调用图灵机器人接口(一)

    此文将分两篇讲解,主要分为以下几步 签名校验; 首次提交验证申请; 接收消息; 被动响应消息(返回XML); 映射图灵消息及微信消息; 其实图灵机器人搭载微信公众号很简单,只需要把图灵的地址配到公众后 ...

  8. How to implement a custom PropertyEditor so that it supports Appearance rules provided by the ConditionalAppearance module

    https://www.devexpress.com/Support/Center/Question/Details/T505528/how-to-implement-a-custom-propert ...

  9. 人在囧途——Java程序猿学习Python

    引言 LZ之前其实一直对python都很好奇,只是苦于平时没有时间去了解它,因此趁着51假期这个机会,便迫不及待的开始了自己的探索.作为一个标准的Java程序猿,在了解python的过程当中,LZ遇到 ...

  10. .net 控件生命周期

    这里列举出来了11个生命周期,一般的控件生命周期会经历这11个生命周期,但是有一些特别的控件比如页面控件System.Web.UI.Page等. 具体代码参考如下: /// <summary&g ...