【PAT甲级】1004. Counting Leaves (30)——JAVA实现
2017-09-03 01:39
447 查看
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 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.
Output
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. 输入数n(这棵树的结点总数)和m(这棵树非叶子总数)
2. 对每个非叶子节点,输入他的结点编号和孩子数目,并输入每个孩子的编号
3. 要求输出每层的叶子结点数目
考察:看似是树的结构,实际考察图的遍历。
分析:根据题目表达,对每个结点都是给出相应的所有孩子,所以可以考虑孩子存储在一个容器里,就是一个邻接表结构,所以这里采用深度遍历。由于每个非叶结点连接的容器都是自己的孩子,所以可以看作是非强联通有向图,往下走每个结点只会遍历一次,不用考虑visited数组来记录是否遍历过某个结点。
Input
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.
Output
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. 输入数n(这棵树的结点总数)和m(这棵树非叶子总数)
2. 对每个非叶子节点,输入他的结点编号和孩子数目,并输入每个孩子的编号
3. 要求输出每层的叶子结点数目
考察:看似是树的结构,实际考察图的遍历。
分析:根据题目表达,对每个结点都是给出相应的所有孩子,所以可以考虑孩子存储在一个容器里,就是一个邻接表结构,所以这里采用深度遍历。由于每个非叶结点连接的容器都是自己的孩子,所以可以看作是非强联通有向图,往下走每个结点只会遍历一次,不用考虑visited数组来记录是否遍历过某个结点。
import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Problem_1004 { static boolean[] visited; public static void main(String[] args) { Scanner input = new Scanner(System.in); //输入操作 int n = input.nextInt(); int m = input.nextInt(); int i = 0,j = 0,child,id,k;//id指本结点编号,child指孩子结点编号,k为孩子结点数目 ArrayList<Integer> list; HashMap<Integer,ArrayList<Integer>> map = new HashMap<Integer, ArrayList<Integer>>();//用map来存储这个结构 int[] record = new int ;//记录每层叶子结点数目的结果数组,由于结点只有n个,所以最多n层 while(i<m){ id = input.nextInt(); k = input.nextInt(); list = new ArrayList<Integer>(); for(j=0;j<k;j++){ child = input.nextInt(); list.add(child); } map.put(id, list); i++; } int p = DFS(map,record,1,0,0); for(i=0;i<p;i++) System.out.print(record[i] + " "); System.out.print(record[i]); } //map为该树结构,record数组用于记录结果,node为当前遍历到的结点,level为当前层是第几层,height用来记录树高度,为最后输出作准备 public static int DFS(Map<Integer,ArrayList<Integer>> map,int[] record,int node,int level,int height){ ArrayList<Integer> list = map.get(node); if(list==null){ record[level]++;//每次遇到叶子结点,相应层数记录+1 return height; } for(int i=0;i<list.size();i++){ height = Math.max(height, DFS(map,record,list.get(i),level+1,level+1)); } return height; } }
相关文章推荐
- [PAT]Table Tennis (30)Java实现
- 【PAT甲级】1006 Sign In and Sign Out(25)——JAVA实现
- 【PAT】Digital Library (30)Java实现
- pat 甲级 1126. Eulerian Path java实现
- 【PAT甲级】1036. Boys vs Girls (25)——JAVA实现
- PAT1001. 害死人不偿命的(3n+1)猜想 java;python实现
- PAT1002. 写出这个数;java实现
- PAT 1005. 继续(3n+1)猜想 (25) ;JAVA实现
- PAT 1003. 我要通过!(20) java实现
- java基础(30)--Java中实现CallBack功能
- PAT 1006. 换个格式输出整数 (15);JAVA;Python实现
- 1115. Counting Nodes in a BST (30)-PAT甲级真题(二叉树的遍历,dfs)
- 1099. Build A Binary Search Tree (30)-PAT甲级真题
- 剑指Offer:面试题30——最小的k个数(java实现)
- PAT甲级练习题A1022. Digital Library (30)
- 1004. Counting Leaves (30)-PAT甲级真题(bfs,dfs,树的遍历,层序遍历)
- PAT初级题目1011A+B和C(Java实现)
- PAT Advanced Level 1030. Travel Plan (30)(Java and C++)
- PAT-JAVA-5-30 字符串的冒泡排序 (20分)
- PAT 1004. Counting Leaves (30)(Java一个测试点未通过 and C++)