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. Find a way HDU - 2612(bfs)

    Find a way Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  2. Persona5

    65536K   Persona5 is a famous video game. In the game, you are going to build relationship with your ...

  3. 如何锁定Android系统CPU的频率

    接触到了Android系统的Performance测试,所以有锁定CPU的需求: 由于要首先读取到此系统所支持的CPU频率,之后再所支持的频率中选取你想要的频率,之后进行锁定. 这个过程,手动也是可以 ...

  4. HBase0.94.2-cdh4.2.0需求评估测试报告1.0之四

    第二组:文件存储读过程记录 第一组:一个列,四个分区,随机ID 测试列和分区 测试程序或命令 导入文件大小(Mb) 导入文件个数(个) 是否触发flush事件(布尔) 是否触发compact事件(布尔 ...

  5. An entity cannot be annotated with both @Entity and @MappedSuperclass: com.example1.demo1.Entity.User错误

    项目问SpringDataJpa项目,在运行的过程中出现的以下错误: Caused by: org.hibernate.AnnotationException: An entity cannot be ...

  6. 树状数组 - BZOJ 1103 [POI2007]大都市

    bzoj 1103 [POI2007]大都市 描述 在经济全球化浪潮的影响下,习惯于漫步在清晨的乡间小路的邮递员 Blue Mary也开始骑着摩托车传递邮件了.不过,她经常回忆起以前在乡间漫步的情景. ...

  7. ogre3D学习基础7---材质详解

    物体着色的基础 --- 四种不同光照作用 1.环境反射 近似的模拟了场景中的全局辐射,也就是用来近似模拟所有光在场景中不断散射的结果.材质中有相应的属性来代表这种环境反射颜色. 2.漫反射 这种颜色是 ...

  8. python - 接口自动化测试实战 - case1 - 优化版

    题目: 基于以下两个接口和数据完成接口自动化测试,并生成测试报告: '''登录 login='http://47.107.168.87:8080/futureloan/mvc/api/member/l ...

  9. PHP 函数 ignore_user_abort()

    ignore_user_abort 设置与客户机断开是否会终止脚本的执行.   本函数返回 user-abort 设置的之前的值(一个布尔值). int ignore_user_abort ([ st ...

  10. DS博客作业06--图

    1.本周学习总结 1.1.思维导图 1.2.谈谈你对图结构的认识及学习体会 本章学习了图结构的相关知识,图形结构属于复杂的非线性数据结构,在实际应用中很多问题可以用图来描述.在图结构中,每个元素可以有 ...