PAT (Advanced Level) 1004. Counting Leaves (30) 解题报告
2017-01-24 14:44
495 查看
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 <cstdio>
#include <iostream>
#include <map>
#include <vector>
#include <cstring>
#define MAXN 101
#define INF 0x3f3f3f3f
using namespace std;
map<int,vector<int> > trees;
int res[MAXN];
void dfs(int id, int level)
{
if(trees[id].empty())
{
res[level]++;
return;
}
vector<int>::iterator ite = trees[id].begin();
for(;ite != trees[id].end(); ite++)
dfs(*ite, level+1);
}
int main()
{
int n, m, p, k, son, s, cnt;
scanf("%d%d", &n, &m);
for(int i = 0; i < m; i++)
{
scanf("%d%d", &p, &k);
while(k--)
{
scanf("%d", &son);
trees[p].push_back(son);
}
}
dfs(1, 0);
printf("%d", res[0]);
s = n - m;
cnt = res[0];
for(int i = 1; cnt < s; i++)
{
printf(" %d", res[i]);
cnt += res[i];
}
printf("\n");
return 0;
}
相关文章推荐
- PAT (Advanced Level) 1103. Integer Factorization (30) 解题报告
- PAT (Advanced Level) 1038. Recover the Smallest Number (30) 解题报告
- PAT (Advanced Level) 1115. Counting Nodes in a BST (30) 解题报告
- PAT (Advanced Level) 1127. ZigZagging on a Tree (30) 解题报告
- PAT (Advanced Level) 1035. Password (20) 解题报告
- PAT (Advanced Level) 1001. A+B Format (20) 解题报告
- PAT (Advanced Level) 1036. Boys vs Girls (25) 解题报告
- PAT (Advanced Level) 1002. A+B for Polynomials (25) 解题报告
- PAT (Advanced Level) 1003. Emergency (25) 解题报告
- PAT (Advanced Level) 1037. Magic Coupon (25) 解题报告
- PAT (Basic Level) Practise(中文)----30题解题报告
- PAT(Advanced Level) 1004 Counting Leaves 解题报告
- PAT (Advanced Level) Practise 1001 解题报告
- PAT(Advanced Level) 1020 Tree Traversals 解题报告
- PAT (Advanced Level) 1005. Spell It Right (20) 解题报告
- PAT (Advanced Level) 1124. Raffle for Weibo Followers (20) 解题报告
- PAT (Advanced Level) 1006. Sign In and Sign Out (25) 解题报告
- PAT (Advanced Level) 1100. Mars Numbers (20) 解题报告
- PAT (Advanced Level) 1125. Chain the Ropes (25) 解题报告
- PAT (Advanced Level) 1007. Maximum Subsequence Sum (25) 解题报告