PAT 甲级 1004 Counting Leaves
发布日期:2021-07-01 03:08:43 浏览次数:2 分类:技术文章

本文共 2436 字,大约阅读时间需要 8 分钟。

1004 Counting Leaves (30 point(s))

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<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.

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 101 1 02

Sample Output:

0 1

Experiential Summing-up

emmmm  This question involve level order traversal and the structure of tree. The difficulty is little~So much for that~

(The purpose of using English to portray my solution is that to exercise the ability of my expression of English and accommodate PAT advanced level's style.We can make progress together by reading and comprehending it. Please forgive my basic grammar's and word's error. Of course, I would appreciated it if you can point out my grammar's and word's error in comment section.( •̀∀•́ ) Furthermore, Big Lao please don't laugh at me because I just a English beginner settle for CET6    _(:з」∠)_  )

Accepted Code

#include 
#include
#include
#include
using namespace std;const int maxn=110;const int INF=0x3fffffff;int level[maxn];struct Node{ int child[maxn]; int c_num; int level; Node() { memset(child,0,sizeof(child)); c_num=0; }}Tree[maxn];int level_order(){ memset(level,0,sizeof(level)); queue
q; Tree[1].level=0; q.push(Tree[1]); int maxlevel=-1; while(q.size()) { Node a=q.front(); q.pop(); if(a.level>maxlevel) maxlevel=a.level; if(a.c_num==0) ++level[a.level]; else for(int i=0;i

 

转载地址:https://meteorrain.blog.csdn.net/article/details/86512731 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:PAT 甲级 1005 Spell It Right
下一篇:PAT 甲级 1003 Emergency

发表评论

最新留言

逛到本站,mark一下
[***.202.152.39]2024年04月29日 17时33分45秒