【PAT甲级】1004. Counting Leaves (30)——JAVA实现

来源:互联网 发布:淘宝滥发信息怎么申诉 编辑:程序博客网 时间:2024/06/15 20:52

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数组来记录是否遍历过某个结点。

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个,所以最多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;    }}
原创粉丝点击