1004 Counting Leaves
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的更多相关文章
- 1004. Counting Leaves (30)
1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- PAT 解题报告 1004. Counting Leaves (30)
1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is t ...
- PAT甲1004 Counting Leaves【dfs】
1004 Counting Leaves (30 分) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- 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 ...
- PAT 1004 Counting Leaves (30分)
1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is t ...
- 1004 Counting Leaves (30分) DFS
1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- PAT Advanced 1004 Counting Leaves
题目与翻译 1004 Counting Leaves 数树叶 (30分) A family hierarchy is usually presented by a pedigree tree. You ...
- 1004 Counting Leaves ——PAT甲级真题
1004 Counting Leaves A family hierarchy is usually presented by a pedigree tree. Your job is to coun ...
- PTA (Advanced Level) 1004 Counting Leaves
Counting Leaves A family hierarchy is usually presented by a pedigree tree. Your job is to count tho ...
- PAT 1004. Counting Leaves (30)
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family membe ...
随机推荐
- percpu之静态变量
参考:Linux内核同步机制之(二):Per-CPU变量 CPU私有变量(per-CPU变量) 动态PCPU变量 setup_per_cpu_areas()初始化per-cpu数据. static v ...
- CodeForce:732B-Cormen — The Best Friend Of a Man
传送门:http://codeforces.com/problemset/problem/732/B Cormen - The Best Friend Of a Man time limit per ...
- 利用virt-manager,xmanager, xshell启动界面来管理虚拟机
有时候我们需要搭建一套自己的简单环境来启动一个虚拟机,验证一些问题. 1.首先我利用vmware workstation来创建centos7虚拟机,然后开启虚拟化,如下图所示. 2.其次,启动虚拟机, ...
- poj 3281 Dining(网络流+拆点)
Dining Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 20052 Accepted: 8915 Descripti ...
- python基础学习笔记——面向对象初识
面向对象初识 python中一切皆对象. 类有两种: 新式类:在py3中所有类都是新式类 经典类:在py2中只有类本身继承了object类才叫做新式类,默认是经典类 class Person: cou ...
- LayoutInflater的用法
Instantiates a layout XML file into its corresponding View objects. It is never used directly. Inste ...
- 用javascript写计算器
本人新手,如果有什么不足的地方,希望可以得到指点 今天尝试用javascript写一个计算器 首先把计算器的按钮做出来,用button做好了,这样可以不用设置太多的样式 <button valu ...
- 微信小程序的那些坑
早闻微信小程序是个坑,结果名不虚传,细数一下我开发小程序遇过到坑. 1.UI组件过度封装. 微信小程序的组件是模仿react.js或vue.js的web组件设计的,并且封装了weui.css样式. P ...
- matlab 画图进阶
matlab 画图进阶 applications of matlab in engineering 图表类型的选择 first:advanced 2d plots special plots logl ...
- Python序列化、date、random、os模块
知识点一:序列化与反序列化(json和pickple) 01 什么是序列化/反序列化 序列化就是将内存中的数据结构转换成一种中间格式存储到硬盘或者基于网络传输 发序列化就是硬盘中或者网络中 ...