PAT TEST甲级1004. Counting Leaves (30)
来源:互联网 发布:网络卖彩票会坐牢吗 编辑:程序博客网 时间:2024/06/06 04: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 childfor 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 101 1 02
Sample Output
0 1
#include<iostream>#include <algorithm>using namespace std;int tree[101]={0};int k[101]={0};int id[101][101],maxd=-1;void dfs(int index,int dep){ if(k[index]==0) { tree[dep]++; maxd=max(maxd,dep); return; } for(int i=0;i<k[index];i++) { dfs(id[index][i],dep+1); } }int main(){ int N,M,c,k0; int id0[101]; cin>>N>>M; for(int i=0;i<M;i++) { cin>>id0[i]>>k0; k[id0[i]]=k0; for(int j=0;j<k[id0[i]];j++) { cin>>id[id0[i]][j]; } } dfs(1,0); cout<<tree[0]; for(int i = 1; i <= maxd; i++) { cout<<' '<<tree[i]; } return 0;}
- PAT TEST甲级1004. Counting Leaves (30)
- 【PAT甲级】1004. Counting Leaves (30)
- 【PAT甲级】【C++】1004. Counting Leaves (30)
- 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) DFS
- PAT 甲级 1004. Counting Leaves
- [PAT-甲级]1004.Counting Leaves
- PAT甲级 1004.Counting Leaves (30) 题目翻译与答案
- [PAT甲级]1004. Counting Leaves (30)(求树各层非叶子节点数目)
- 【PAT甲级】1004. Counting Leaves (30)——JAVA实现
- PAT甲级 1004 Counting Leaves (30)
- PAT甲级1004Counting Leaves
- 1004. Counting Leaves (30)-PAT
- pat 1004. Counting Leaves (30)
- 2017年1月-关于数据库表设计、查询、统计的想法。
- PAT TEST甲级1002. A+B for Polynomials (25)
- opencv学习笔记之调整图像的亮度与对比度
- PAT TEST甲级1003. Emergency (25)
- 浅谈Linux系统运维工程师必备技能
- PAT TEST甲级1004. Counting Leaves (30)
- 安卓 动画效果 Animation
- How to become a proficient Python programmer
- shared_ptr的线程安全性分析
- pat-bl-1050
- QSS编辑预览工具
- java用spring-data-mongodb操作mongodb的时间问题
- 375. Guess Number Higher or Lower II
- PAT TEST 甲级1005. Spell It Right (20)