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 ...
随机推荐
- poj3617 best cow line(贪心题)
Best Cow Line Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 32687 Accepted: 8660 De ...
- jenkins匿名用户登录 - 安全设置
最近自己安装配置jenkins,但是跑任务时,发现是匿名账户登录,且提示: 后来发现搭建好jenkins之后,默认就是匿名用户登录的,可以在安装设置菜单里进行账户管理. 1.登录Jenkins服务器, ...
- Leetcode1--->数组中两数之和等于给定数
题目: 给定一个数组nums,目标数target.在数组中找到两数之和为target的数,返回两数的下标举例: Given nums = [2, 7, 11, 15], target = 9, Bec ...
- 第三章 802.11MAC基础 ****需要深入理解
1.mac所面临的挑战 射频链路品质 radio link 容易受到干扰 802.11采用肯定确认机制 所有传送出去的帧都必须得到响应 工作站发送请求帧 基站 ...
- 精通CSS高级Web标准解决方案(1-3 规划、组织与维护样式表)
对文档应用样式 对代码进行注释/*......*/ 结构性注释 自我提示 删除注释.优化样式表 样式指南:解释代码与站点的视觉设计是如何组织在一起的 站点结构.文件结构.命名规则 编码标准:(X)ht ...
- BZOJ2302 [HAOI2011]Problem c 【dp】
题目 给n个人安排座位,先给每个人一个1~n的编号,设第i个人的编号为ai(不同人的编号可以相同),接着从第一个人开始,大家依次入座,第i个人来了以后尝试坐到ai,如果ai被占据了,就尝试ai+1,a ...
- BZOJ1150 [CTSC2007]数据备份Backup 【堆 + 链表】
题目 你在一家 IT 公司为大型写字楼或办公楼(offices)的计算机数据做备份.然而数据备份的工作是枯燥乏味 的,因此你想设计一个系统让不同的办公楼彼此之间互相备份,而你则坐在家中尽享计算机游戏的 ...
- 刷题总结——生日礼物(bzoj1293单调队列)
题目: Description 小西有一条很长的彩带,彩带上挂着各式各样的彩珠.已知彩珠有N个,分为K种.简单的说,可以将彩带考虑为x轴,每一个彩珠有一个对应的坐标(即位置).某些坐标上可以没有彩珠, ...
- java面试题之Error和Exception的区别
从概念角度分析: Error:程序无法处理的系统错误,编译器不做检查: Exception:程序可以处理的异常,捕获后可能恢复: 总结:前者是程序无法处理的错误,后者是可以处理的异常. 从责任角度分析 ...
- Spring之BeanFactory与ApplicationConText区别
使用BeanFactory从xml配置文件加载bean: import org.springframework.beans.factory.xml.XmlBeanFactory; import org ...