1004 Counting Leaves Java
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了1004 Counting Leaves Java,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2139字,纯文字阅读大概需要4分钟。
内容图文
![1004 Counting Leaves Java](/upload/InfoBanner/zyjiaocheng/603/d237a3391b5e4937ac5b5fd3292d078a.jpg)
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 Specification:
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.
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
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int totalNode=input.nextInt();
int innerNode=input.nextInt();
Map<Integer,List<Integer>> nodeMap=new HashMap<>();;
for(int i=0;i<innerNode;i++){
int No= input.nextInt();
nodeMap.put(No,new ArrayList<>());
int number= input.nextInt();
for(int j=0;j<number;j++)
nodeMap.get(No).add(input.nextInt());
}
List<Integer> number=new ArrayList<Integer>();
List<Integer> queue=new ArrayList<Integer>();
queue.add(1);
int cnt=0;
int last=1;
while(queue.size()>0){
int temp=queue.get(0);
if(nodeMap.get(temp)==null) {
cnt++;
}else {
for(int i=0;i<nodeMap.get(temp).size();i++){
queue.add(nodeMap.get(temp).get(i));
}
}
if(temp==last){
last=queue.get(queue.size()-1);
number.add(cnt);
cnt=0;
}
queue.remove(0);
}
for(int i=0;i<number.size()-1;i++){
System.out.print(number.get(i)+" ");
}
System.out.print(number.get(number.size()-1));
}
}
内容总结
以上是互联网集市为您收集整理的1004 Counting Leaves Java全部内容,希望文章能够帮你解决1004 Counting Leaves Java所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。