Python实现Pat 1004. Counting Leaves (30)
来源:互联网 发布:python http 断点续传 编辑:程序博客网 时间:2024/06/06 00:06
题目
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.
Input
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.
Output
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
解答
"""2 101 1 024 201 1 02 0302 1 041 0"""def bfs(node): global counts,maxLevel,level level+=1 if level>maxLevel: maxLevel=level if node not in tree: counts[level]+=1 return for i in tree[node]: bfs(i) level -= 1N,M=[int(x) for x in input().split(' ')]tree={}if M==0: print(1)else: for i in range(M): line=[int(x) for x in input().split(' ')] tree[line[0]]=line[2:2+line[1]] counts=[0]*1002 maxLevel=1 level=0 bfs(1) counts=counts[1:maxLevel+1] for i,c in enumerate(counts): print (c,end='') if i!=maxLevel-1: print (end=' ')
- Python实现Pat 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)-PAT
- pat 1004. Counting Leaves (30)
- 【PAT】1004. Counting Leaves (30)
- PAT 1004. Counting Leaves (30)
- PAT 1004. Counting Leaves (30)
- pat 1004. Counting Leaves (30)
- PAT 1004. Counting Leaves (30)
- PAT:1004. Counting Leaves (30)
- PAT 1004. Counting Leaves (30)
- [PAT]1004. Counting Leaves (30)
- PAT 1004. Counting Leaves (30)
- PAT 1004. Counting Leaves (30)
- PAT 1004. Counting Leaves (30)
- PAT:1004. Counting Leaves (30)
- PAT:1004. Counting Leaves (30)
- 【PAT】1004. Counting Leaves (30)
- pat 1004. Counting Leaves (30)
- 郑少国收藏的web前端网站
- Erwin 字段加注释
- Tensorflow入门之 前言
- linux上jdk的安装配置
- 【51nod】-1001 数组中和等于K的数对(思维)
- Python实现Pat 1004. Counting Leaves (30)
- SAS--学习小结
- CentOS Redis 哨兵
- 使用opencv中detectMultiScale函数人脸检测问题及解决
- springboot数据库配置及传入中文不乱码
- ceshi
- 面向对象----浅述对象之间的关系
- 学习日记12-07
- 模拟hao123网站对图表组进行管理