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. Oracle单节点_Grid_Infrastructure_DB_安装过程图解(三/三)

    接上文: Oracle单节点_Grid_Infrastructure_DB_安装过程图解(二/三)

  2. 仙人掌&圆方树

    仙人掌&圆方树 Tags:图论 [x] [luogu4320]道路相遇 https://www.luogu.org/problemnew/show/P4320 [ ] [SDOI2018]战略 ...

  3. jQuery学习-事件绑定

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  4. 四分历python实现

    根据一个新加坡人的c代码改写成python代码 ''' 四分历''' # zq = 0 month = 0 def main(): global month year = 1 rb_year = 0 ...

  5. P3592 [POI2015]MYJ

    P3592 [POI2015]MYJ 一道比较烦的区间dp.. 昨天上课讲到了这题,然后就在lg翻到了 然后调了很久很久..... 设\(f[l][r][k]\)为区间\([l,r]\)中,最小值\( ...

  6. eclipse项目转移至IDEA与IDEA tomcat报错(idea自带tomcat版本太高)与war包部署到win服务器与idea提交git的总结

    eclipse导出项目到idea时,不要导出target: idea打开eclipse项目后,出现junit找不到的问题,原因是jar包缺失,而maven配置的低版本的junit也显示找不到,解决办法 ...

  7. while、for循环控制之if、else

    if # score=99 # if score>90: # print('优秀') # elif score<60: # print('不及格') # else: # print('良好 ...

  8. mysql mtr写入数据

    BEGIN; --disable_query_log --let $rows= 100 WHILE($rows) { --eval INSERT INTO t1 (a) VALUES ( $rows ...

  9. node.js学习笔记——前序

    一.什么是node.js 简单的说 Node.js 就是运行在服务端的 JavaScript. Node.js 是一个基于Chrome JavaScript 运行时建立的一个平台. Node.js是一 ...

  10. django 部署一个简单的博客系统

    转:https://www.cnblogs.com/fnng/p/3737964.html 写的目的, 加深影响,熟悉开发流程, 开发都是练出来的. 环境 python3.5 windows 7 1. ...