首页 > 其他 > 详细

1004 Counting Leaves

时间:2020-04-09 18:34:55      阅读:59      评论:0      收藏:0      [点我收藏+]

1004 Counting Leaves (30分)

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 1
01 1 02
?
     
       
     
   

Sample Output:

0 1

审题

题目给出一棵树,我们需要求出该树每层的叶子结点。

思路

这里用BFS的方法把每层的叶结点求出存值数组,然后按层序输出。

参考代码

 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<vector>
 4 #include<queue>
 5 using namespace std;
 6 ?
 7 typedef struct {
 8     vector<int> child;
 9 }Node;
10 ?
11 Node node[100];
12 int level[100];
13 int n,m;
14 ?
15 void BFS(int root)
16 {
17     queue<int> que;
18     que.push(root);
19     int last=1;
20     int count=0;
21     int l=0;
22     while(!que.empty())
23     {
24         int front=que.front();
25         que.pop();
26         if(node[front].child.size()==0){
27             count++;
28         } else {
29             for(int i=0;i<node[front].child.size();i++){
30                 que.push(node[front].child[i]);
31             }
32         }
33         //第一次提交的代码,逻辑混乱!!
34         //int num=node[front].child.size();
35         //  if(num==0){
36         //    level[l]=count;
37         //      count=0;
38         //      l++;
39         //  } else { 
40         //      level[l]=count;
41         //      last=que.back();
42         //      count=0;
43         //      l++;
44         //  }
45         if(front==last){
46                 level[l]=count;
47                 last=que.back();
48                 count=0;
49                 l++; 
50         }
51     }
52     cout<<level[0];
53     for(int i=1;i<l;i++)
54     {
55         cout<<" "<<level[i];
56     }
57 }
58 ?
59 int main()
60 {
61     while(cin>>n)
62     {
63         if(n!=0){
64             cin>>m;
65             for(int i=0;i<m;i++)
66                 {
67                     int id;
68                     int num;
69                     cin>>id>>num;
70                     for(int j=0;j<num;j++)
71                     {
72                         int c;
73                         cin>>c;
74                         node[id].child.push_back(c); 
75                     }
76                 }
77                 BFS(1);
78         } else {
79             break;
80         }
81     }
82     
83 }

 

总结

本题是层序遍历的模板题。第一次的代码逻辑混乱,花了好长时间才检查出来,看来写代码时的专注度不高,导致了逻辑漏洞,同时我发现逻辑越混乱检查难度也越大。

1004 Counting Leaves

原文:https://www.cnblogs.com/TreBienBA/p/12668436.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!