[PAT] 1004 Counting Leaves (30 分)Java

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<N<100, the number of nodes in a tree, and M (<N), 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 1 leaf node. Then we should output 0 1 in a line.

Sample Input:

2 1
01 1 02

Sample Output:

0 1

 1 import java.util.Scanner;
 2 import java.util.ArrayList;
 3 import java.util.HashMap;
 4 import java.util.Map;
 5 public class Main{
 6   
 7   public static void main(String[] args){
 8     
 9     Scanner sc = new Scanner(System.in);
10     int n = sc.nextInt();
11     int m = sc.nextInt();
12     int i = 0,j,id,k,child;
13     ArrayList<Integer> list;
14     HashMap<Integer,ArrayList<Integer>> map = new HashMap<>();
15     int[] record = new int[n];
16     
17     while(i < m){
18       id = sc.nextInt();
19       k = sc.nextInt();
20       list = new ArrayList<>();
21       for(j = 0;j < k;j++){
22         child = sc.nextInt();
23         list.add(child);
24       }
25       map.put(id, list);
26       i++;
27     }
28     int p = DFS(map, record,1,0,0);
29     for(i = 0; i < p ;i++){
30       System.out.print(record[i] + " ");
31     }
32     System.out.print(record[i]);
33   }
34   
35   private static int DFS(Map<Integer,ArrayList<Integer>> map, int[] record, int node,int level, int height){
36     ArrayList<Integer> list = map.get(node);
37     if(list == null){
38       record[level]++;
39       return height;
40     }
41     for(Integer integer:list){
42       height = Math.max(height,DFS(map,record,integer,level+1,level+1));
43     }
44     return height;
45   }
46 }

 

转载于:https://www.cnblogs.com/PureJava/p/10497928.html

Project 2: Counting Leaves A family hierarchy等级 is usually presented by a pedigree tree系谱树. Your job is to count those family members who have no child. 立即下载

相关推荐

1004 Counting LeavesJava

本文讲一下PAT题库里面的1004 Counting Leaves

m0_74165193的博客 460

PAT练习题(甲级) 1004 Counting Leaves (30)Java实现)

PAT练习题 1004 Counting LeavesJava)题目题意解题思路代码实现 题目 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 inpu...

CoderHqf的博客 1万+

1004 Counting Leaves Java

1004 Counting Leaves (30 ) 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<N

weixin_45549192的博客 328

[PAT]1004. Counting Leaves (30)@Java

1004. Counting Leaves (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A family hierarchy is usually presented by a

zjkC050818的博客 710

javaPat刷题日记-1004 Counting Leaves30

一开始自己做的真的很乱,把题目搞错了 借鉴了一下别的大神,自己模仿着写了一遍 其实是一道考图的遍历的题目,用了DFS来做 import java.util.ArrayList; import java.util.HashMap; import java.util.Scanner; public class Main{ public static void main(String[] ...

Tog 322

PAT 甲级 1004 Counting Leaves (30 )Java

文章目录PAT 甲级 1004 Counting Leaves (30 )Java)题目题意解读解题思路解法解法一解法二 PAT 甲级 1004 Counting Leaves (30 )Java) 题目 题目链接 题意解读 这题就是给出一棵树,然后给出节点数,非叶子节点给出相应的孩子节点id,然后问每一层叶子节点的个数; 解题思路 首先,通过一些数据结构保存这棵树以及节点之间的父子关系,我这里采用了static List<List> d = new ArrayList<>(

盼你到天明的博客 288

PAT 甲级 1004 Counting Leaves (30 )Java)学习笔记

PAT 甲级 1004 Counting Leaves (30 )Java)学习笔记

xiaobai0352的博客 162

PAT甲级之路--1004 Counting Leaves

题目重述: 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...

liuchunyv711的博客 234

[PAT-Advanced] A1004 Counting Leaves (30)

文章目录A1004 Counting Leaves (30point(s))Input Specification:Output Specification:Sample Input:Sample Output:CodeAnalysis A1004 Counting Leaves (30point(s)) A family hierarchy is usually presented by a p...

Duke 309

[PAT 甲级]1004 Counting Leaves30 ) 【dfs 树的遍历】

1004 Counting Leaves30 ) 原题链接 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 ...

cp0328的博客 3364

PAT甲级 1004 Counting Leaves

题目描述&输入描述 A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child. 输入样例 2 1 01 1 02 题目清晰明确,给定一个多叉树,求每一层的叶子结点的个数。 输出描述&输出样例 For ...

qq_26073557的博客 154

PAT ()1004 Counting Leaves (30)(dfs)

PAT

taotao 的大学墓志 1434

1004. Counting Leaves (30)[bfs]

1. 原题:https://www.patest.cn/contests/pat-a-practise/1004 2. 思路: 题意就是输出树的每层叶子节点数。 无需构建树,通过DFS或者BFS遍历就好。 这里采用bfs。 已AC。 3. 源码: #include #include #include using namespace std; int N, M; vecto

坚不萌 271

pat 1004 Counting Leaves (30 )

pat 1004 Counting Leaves (30 ) 1)解题思路:dfs或者bfs //dfs方式 #include <iostream> #include <vector> using namespace std; vector<int> node[101]; int maxLayers; int leaves[101] = {0}; void dfs(int index, int layers){ if(!node[index].size()){

qq_43724644的博客 257

[JAVA/C++](PAT)1004 COUNTING LEAVES (30)

A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child. Input Each input file contains one test case. Each case starts with a line co...

贰拾壹 388

PAT 1004 Counting Leaves(BFS+模拟树)

原题地址https://www.patest.cn/contests/pat-a-practise/1004 题意:一棵有N个结点的树,其中有M个结点是非叶子结点,给出每个非叶子结点的孩子结点编号,求出每一层上的非叶子结点个数。 解题思路本题考察树的结构,这种看似树结构,但用数字编号表示节点的题目,经常可以用数组模拟,无需构造出一棵树。数据结构:   注意到结点编号最多为两位,因此100以内的

Lecholin的博客 496

PAT甲级1004 Counting Leaves//BFS运用

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<N<100, the number of nodes

冷眼看world的博客 197

PAT-Java-1004-Counting Leaves (30)

1004. Counting Leaves (30) 题目阐述 A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child. Input Each input file contains one

rebornyp的博客 360
上一篇: [PAT] 1003 Emergency (25 分)Java
下一篇: [PAT] 1005 Spell It Right (20 分)Java
Simma1101
博客等级 码龄12年 0粉丝 0原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值