PAT甲级 1004. Counting Leaves (30)
2017-07-04 08:25
316 查看
1004. Counting Leaves (30)
时间限制400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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
题意:给你一个树,让你输出每一层的叶子节点数目。
思路:递归搜索
#include <iostream> #include<queue> #include<cstdio> #include<algorithm> #include<cmath> #include<set> #include<cstring> using namespace std; #define LL long long int n,m,a,b,c,e,f,g,x,y; struct node { int cnt; int son[150]; }N[150]; int num[150]; int len; void dfs(int deep,int rt) { if(N[rt].cnt==0) num[deep]++; for(int i=0;i<N[rt].cnt;i++) { dfs(deep+1,N[rt].son[i]); } len=max(deep,len); } int main() { cin>>n>>m; for(int i=1;i<=m;i++) { cin>>a; cin>>N[a].cnt; for(int j=0;j<N[a].cnt;j++) { cin>>b; N[a].son[j]=b; } } dfs(1,1); for(int i=1;i<=len;i++) { if(i==1) cout<<num[i]; else cout<<" "<<num[i]; } return 0; }
相关文章推荐
- PAT甲级练习题A1018. Public Bike Management (30)
- pat甲级1026. Table Tennis (30)
- PAT甲级练习题A1022. Digital Library (30)
- PAT 甲级 1107. Social Clusters (30)
- PAT 甲级 1123. Is It a Complete AVL Tree (30)
- 【PAT甲级】1004. Counting Leaves (30)
- PAT - 甲级 - 1022. Digital Library (30)(STL-map,set)
- 1045. Favorite Color Stripe (30)-PAT甲级真题
- PAT甲级 1115. Counting Nodes in a BST (30)
- PAT甲级 1139. First Contact (30)
- PAT 甲级 1018. Public Bike Management (30)
- PAT 甲级 1022. Digital Library (30)
- 1004. Counting Leaves (30)-PAT甲级真题(bfs,dfs,树的遍历,层序遍历)
- 1022. Digital Library (30)-PAT甲级真题(map映射)
- PAT 甲级 1053. Path of Equal Weight (30)
- 浙大pat | 浙大pat 牛客网甲级 1002 All Roads Lead to Rome (30)迪杰斯特拉算法改进
- 1030. Travel Plan (30)-PAT甲级真题(Dijkstra + DFS,输出路径,边权)
- PAT 甲级 1099. Build A Binary Search Tree (30)
- PAT 1131. Subway Map (30) -甲级
- PAT 甲级 1127. ZigZagging on a Tree (30)