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.java

 

Inputnode

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:git

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.測試

 

Outputspa

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.code

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.orm

Sample Inputblog

2 1
01 1 02

Sample Output隊列

0 1內存

**************************************************************************************************

思路:用一個HashSet存儲全部的非葉子節點,即每一行的第一個節點。HashMap存儲全部的節點和它對應的level(01 root是0),遍歷HashMap,若是HashSet不包含該節點就說明該節點是葉子節點。再根據該節點的level值逐層累計葉子節點的個數。

存儲節點和它對應的level值時是根據該行第一個非葉子節點的level+1實現的,可是可能會出現非葉子節點的level在讀入當前行時還未知的狀況,因此咱們能夠先把該行存在一個隊列的末尾,先處理其餘能獲取到Level值的行,再處理該行。舉個例子:

9 4

01 2 02 03

04 3 07 08 09

03 2 05 06

02 1 04

讀入04 3 07 08 09時,04獲取不到它的level。此時咱們先把該行存起來,先處理03行和02行,以後再處理04行就能夠獲得它的level是2了。

 

PS:測試點4提示內存超限,其他的測試點都佔用1萬K左右,題目限制是65536K。還但願巨巨能指出程序中的問題。

 1 import java.util.*;
 2 
 3 public class Main {
 4     private static int maxLevel = 0;
 5 
 6     private static void dealCurLine(String[] dealing, Map<String, Integer> allNode) {
 7         int level = allNode.get(dealing[0]) + 1;
 8         maxLevel = Math.max(level, maxLevel);
 9         for (int i = 2; i < dealing.length; i++) {
10             allNode.put(dealing[i], level);
11         }
12     }
13 
14     public static void main(String[] args) {
15         Scanner in = new Scanner(System.in);
16         String[] line0 = in.nextLine().split(" ");
17         // int N = Integer.parseInt(line0[0]);
18         int M = Integer.parseInt(line0[1]);
19         Set<String> nonLeaf = new HashSet<String>();
20         Map<String, Integer> allNode = new HashMap<String, Integer>();
21         allNode.put("01", 0);
22         Queue<String[]> input = new LinkedList<String[]>();
23 
24         while (M-- > 0) {
25             String[] cur = in.nextLine().split(" ");
26             nonLeaf.add(cur[0]);
27             if (allNode.get(cur[0]) == null) {
28                 input.add(cur);
29                 continue;
30             }
31             dealCurLine(cur, allNode);
32         }
33 
34         while (!input.isEmpty()) {
35             String[] dealing = input.poll();
36             //If we can get its level ,then put it in the end of queue.
37             if (allNode.get(dealing[0]) == null) {
38                 input.add(dealing);
39                 continue;
40             }
41             dealCurLine(dealing, allNode);
42         }
43         int[] levels = new int[maxLevel + 1];
44         for (Map.Entry<String, Integer> entry : allNode.entrySet()) {
45             if (!nonLeaf.contains(entry.getKey())) {
46                 levels[entry.getValue()]++;
47             }
48         }
49         for (int i = 0; i < maxLevel; i++) {
50             System.out.print(levels[i] + " ");
51         }
52         System.out.println(levels[maxLevel]);
53     }
54 }
相關文章
相關標籤/搜索