PAT 1004 Counting Leaves

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
时间限制: 400 ms
内存限制: 64 MB
代码长度限制: 16 KB
 

思路

使用邻接表存储孩子节点,使用BFS层序遍历判断是否叶子节点,如果该节点邻接表为空,说明是该节点是叶子节点;否则,将其所有孩子节点入队。

 

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <vector>
 4 #include <queue>
 5 #include <string.h>
 6 
 7 using namespace std;
 8 
 9 struct Node
10 {
11     int val;
12     int level = 0;
13 };
14 
15 int cnt[110];    //cnt[i]表示第i层的叶子节点数 
16 int totalLevel;
17 
18 int main()
19 {
20     int n, m, k;
21     while(scanf("%d", &n) != EOF)
22     {
23         if(n == 0)
24             break;
25         
26         vector<Node> adjList[110];  //邻接表
27         queue<Node> Q;
28         memset(cnt, 0, sizeof(cnt));
29         
30         scanf("%d", &m);
31         
32         if(n == 1)
33         {
34             //只有1个节点,直接输出1 
35             cout << 1;
36             continue;
37         }
38         for(int i = 0; i < m; ++i)
39         {
40             Node id;
41             int k;
42             scanf("%d%d", &id.val, &k);
43             if(id.val == 01)
44                 Q.push(id);    //根节点入队 
45             for(int i = 1; i <= k; ++i)
46             {
47                 Node child;
48                 scanf("%d", &child.val);
49                 //建立邻接表 
50                 adjList[id.val].push_back(child);
51             }
52         } 
53         
54         //层序遍历,BFS 
55         while(!Q.empty())
56         {
57             Node p = Q.front();
58             Q.pop();
59             
60             //存储最后的总层数,方便最后输出结果 
61             totalLevel = p.level;
62             
63             if(adjList[p.val].empty())
64             {
65                 cnt[p.level]++; //当前层的叶子节点数加一 
66             }
67             else
68             {
69                 //遍历孩子节点 
70                 for(int i = 0; i < adjList[p.val].size(); ++i)
71                 {
72                     adjList[p.val][i].level = p.level + 1;
73                     Q.push(adjList[p.val][i]); 
74                 }
75             } 
76         } 
77         
78         for(int i = 0; i <= totalLevel; ++i)
79         {
80             if(i == totalLevel)
81                 cout <<  cnt[i];
82             else
83                 cout <<  cnt[i] << ' ';
84         }
85         
86     }    
87 
88     return 0;
89 }
原文地址:https://www.cnblogs.com/FengZeng666/p/12570854.html