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
思路#
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 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南