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, the number of nodes in a tree, and M (<), 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
import java.util.* ;

public class Main{
static class Node{
int id;
Queue<Node> children;
Node(int id){
this.id=id;
}
}
static HashMap<Integer,Node> tree;
static int N;//树的节点个数
static int M;//有孩子的节点数
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
M = sc.nextInt();
tree = new HashMap<>();
for(int i=;i<M;i++){
int id=sc.nextInt();
int K = sc.nextInt();
Node node = new Node(id);
Queue<Node> children = new LinkedList<>();
for(int j=;j<K;j++){
Node no = new Node(sc.nextInt());
children.offer(no);
}
node.children=children;
tree.put(id,node);
}
Queue<Node> que =new LinkedList<>();
que.add(tree.get());
BFS(que);
} public static void BFS(Queue<Node> que){
int len = ;
Queue<Node> NextQue = new LinkedList<>();
while(que.size()!=){
Node node = que.poll();
if(tree.size()==||!tree.containsKey(node.id)){
len++;
}else{
NextQue.addAll(tree.get(node.id).children);
}
}
if(NextQue.size()==){
System.out.print(len);
}
else {
System.out.print(len+" ");
BFS(NextQue);
}
}
}

思路为BFS。

HashMap建树。

1004 Counting Leaves的更多相关文章

  1. 1004. Counting Leaves (30)

    1004. Counting Leaves (30)   A family hierarchy is usually presented by a pedigree tree. Your job is ...

  2. PAT 解题报告 1004. Counting Leaves (30)

    1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is t ...

  3. PAT甲1004 Counting Leaves【dfs】

    1004 Counting Leaves (30 分) A family hierarchy is usually presented by a pedigree tree. Your job is ...

  4. PTA 1004 Counting Leaves (30)(30 分)(dfs或者bfs)

    1004 Counting Leaves (30)(30 分) A family hierarchy is usually presented by a pedigree tree. Your job ...

  5. PAT 1004 Counting Leaves (30分)

    1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is t ...

  6. 1004 Counting Leaves (30分) DFS

    1004 Counting Leaves (30分)   A family hierarchy is usually presented by a pedigree tree. Your job is ...

  7. PAT Advanced 1004 Counting Leaves

    题目与翻译 1004 Counting Leaves 数树叶 (30分) A family hierarchy is usually presented by a pedigree tree. You ...

  8. 1004 Counting Leaves ——PAT甲级真题

    1004 Counting Leaves A family hierarchy is usually presented by a pedigree tree. Your job is to coun ...

  9. PTA (Advanced Level) 1004 Counting Leaves

    Counting Leaves A family hierarchy is usually presented by a pedigree tree. Your job is to count tho ...

  10. PAT 1004. Counting Leaves (30)

    A family hierarchy is usually presented by a pedigree tree.  Your job is to count those family membe ...

随机推荐

  1. 使用TensorFlow的卷积神经网络识别手写数字(3)-识别篇

    from PIL import Image import numpy as np import tensorflow as tf import time bShowAccuracy = True # ...

  2. nrf51822微信开发2:[转]airkiss/airsync介绍

    "微信蓝牙"专题共分为8部分 1.airkiss/airsync介绍 2.eclipes的j2ee软件使用教程 3.微信公众号使用Dome(airkiss/airsync) 4.新 ...

  3. 剑指Offer(书):顺时针打印数组

    题目:输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1 ...

  4. $(MAKE) , make命令

    make 定义了很多默认变量,像常用的命令或者是命令选项之类的,什么CC啊,CFLAGS啊之类.$(MAKE)就是预设的 make 这个命令的名称(或者路径).make -p 可以查看所有预定义的变量 ...

  5. Monkeyrunner 简介及其环境搭建

    Monkeyrunner是通过坐标.控件ID和控件上的文字操作应用的界面元素,其测试用例是用python写的,这样就弥补了monkey只有简单命令无法执行复杂用例的缺陷.Monkeyrunner采用的 ...

  6. Socketserver详解

    Python3中的SocketServer socket并不能多并发,只能支持一个用户,socketserver 简化了编写网络服务程序的任务,socketserver是socket的在封装.sock ...

  7. Verlet Integration

        Verlet Integration Verlet 积分法是一种用于求解牛顿运动方程的数值方法,被广泛运用于动力学模拟以及视频游戏中.尔莱算法的优点在于:数值稳定性比简单的欧拉方法高很多,并保 ...

  8. js的编码函数

    js对文字进行编码,涉及3个函数:escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decodeURIComponent ...

  9. POJ 1952

    BUY LOW, BUY LOWER Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 7748   Accepted: 267 ...

  10. gcd-模板+最小公倍数

    #include<iostream> #include<cstdio> #include<algorithm> using namespace std; int G ...