文章目录
总题解目录
[PAT- Advanced Level] 甲级题解目录(Advanced Level)
A1004 Counting Leaves (30point(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 1
01 1 02
Sample Output:
0 1
Analysis
- 已知结点数n和非叶结点数m,以及每一个非叶结点有那些孩子。本题指出root的ID为01。
- 求每一层的叶结点数。
- 注意,本题的树是普通树,不是二叉树。
- 对原算法仅做细微修改
C++ Code1
#include <stdio.h>
#include <stdlib.h>
#include <vector>
using namespace std;
const int maxn = 100010;
int n, m, k, num[maxn], id, cnt[maxn], maxlevel = 0;
vector<int> far[maxn]; // 存某一个父结点的所有孩子
void tri(int index, int level){
if (far[index].size() == 0)// 如果是叶结点(无孩子)
cnt[level]++; // 对应层叶结点计数+1
for (int i = 0; i < far[index].size(); i++) // 递归遍历树
tri(far[index][i], level + 1);
if (level > maxlevel) maxlevel = level; // 记录最多有几层,用于最后输出
}
int main(){
scanf("%d %d", &n, &m);
for (int i = 1; i <= m; i++){
scanf("%d %d", &num[i], &k);
for (int j = 0; j < k; j++){
scanf("%d", &id);
far[num[i]].push_back(id); // 加入对应父结点的孩子中
}
}
tri(1, 1);
for (int i = 0; i < maxlevel; i++){
if (i) printf(" ");
printf("%d", cnt[i+1]);
}
printf("\n");
return 0;
}
C++ Code2
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#define MAX_N 100
using namespace std;
void tree_traverse(vector<int> node[], int index, int level, int each_level_leaves[], int &max_level){
if (node[index].size() == 0)// 如果是叶结点(无孩子)
each_level_leaves[level]++; // 对应层叶结点计数+1
for (int i = 0; i < node[index].size(); i++) // 递归遍历树
tree_traverse(node, node[index][i], level + 1, each_level_leaves, max_level);
if (level > max_level) max_level = level; // 记录最多有几层,用于最后输出
}
int main(){
int n, m, k, id, father_id;
vector<int> node[MAX_N]; // 存某一个结点的所有孩子的id
scanf("%d %d", &n, &m);
for (int i = 1; i <= m; i++){
scanf("%d %d", &father_id, &k);
for (int j = 0; j < k; j++){
scanf("%d", &id);
node[father_id].push_back(id); // 加入对应结点的孩子中
}
}
int each_level_leaves[MAX_N];
int max_level = 0;
memset(each_level_leaves, 0, sizeof(each_level_leaves));
tree_traverse(node, 1, 1, each_level_leaves, max_level);
for (int i = 0; i < max_level; i++){
if (i) printf(" ");
printf("%d", each_level_leaves[i+1]);
}
printf("\n");
return 0;
}