zoukankan      html  css  js  c++  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

    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

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

    思路:用一个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 }
  • 相关阅读:
    学习总结:CSS(二)块级与行级元素特性、盒模型、层模型、BUG与BFC、浮动模型
    学习总结:CSS(一)定义方式、选择器、选择器权重
    html基础知识总结
    js学习总结:DOM节点二(dom基本操作)
    Javascript的作用域和闭包(一)
    js学习总结:DOM节点一(选择器,节点类型)
    jQuery源码解析对象实例化与jQuery原型及整体构建模型分析(一)
    正则表达式基于JavaScript的入门详解
    JavaScript深度克隆(递归)
    电子警察【思想】
  • 原文地址:https://www.cnblogs.com/BJUT-2010/p/5569325.html
Copyright © 2011-2022 走看看