Counting Leaves
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.
Input
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.
Output
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,层数为1。
思路
常规做法是读入数据后,用BFS确定每一层的层数,并判断当前结点是否是叶结点,如果是则使相应层数中的叶结点数+1;
给出的第二种方法是我第一次提交时用的诡异方法,用多组数组分别记录结点i是否为叶结点、结点i父结点、结点i层数、每一层叶结点个数,本质上方法与BFS类似。
代码实现——BFS
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
const int maxn = 101;
struct node // 定义结点
{
int id;
int level;
};
vector<int> tree[maxn]; // 存放子结点编号
int leaf[maxn] = {0}; // 存放每层叶结点个数
int maxLevel = 0; // 记录最大层数
void BFS()
{
queue<node> q;
node x;
x.id = 1;
x.level = 1;
q.push(x); // 根节点入队
while (!q.empty())
{
node now = q.front();
q.pop();
if (now.level > maxLevel)
maxLevel = now.level; // 更新最大层数
if (tree[now.id].size() == 0) // 如果为叶结点
leaf[now.level]++; // 相应层数叶结点数量+1
else // 存在子结点
{
for (int i = 0; i < tree[now.id].size(); i++) // 所有子结点入队
{
x.id = tree[now.id][i];
x.level = now.level + 1;
q.push(x);
}
}
}
}
int main()
{
int n, m;
int id, k, child;
scanf("%d%d", &n, &m);
for (int i = 0; i < m; i++)
{
scanf("%d%d", &id, &k);
for (int j = 0; j < k; j++)
{
scanf("%d", &child);
tree[id].push_back(child); // 记录每个子结点编号
}
}
BFS();
for (int i = 1; i <= maxLevel; i++)
{
if (i > 1)
printf(" ");
printf("%d", leaf[i]); // 输出每层叶结点个数
}
return 0;
}
代码实现——多数组
#include <cstdio>
#include <vector>
using namespace std;
const int maxn = 101;
bool isFather[maxn] = {false}; // 判断是否为存在子结点,默认否
int father[maxn] = {0}; // 记录父结点
int level[maxn] = {0}; // 记录结点i的层数
int leaf[maxn] = {0}; // 记录每层叶结点个数
int maxLevel = 0; // 记录最大层数
int findLevel(int v) // 递归更新每个结点的层数
{
if (v == 1) // 根节点层数为1
return 1;
if (level[v] > 0) // 如果已有记录则直接返回相应值,避免重复递归
return level[v];
level[v] = findLevel(father[v]) + 1; // 递归处理
return level[v];
}
int main()
{
int n, m;
int id, k, c;
scanf("%d%d", &n, &m);
for (int i = 0; i < m; i++)
{
scanf("%d%d", &id, &k);
isFather[id] = true; // 存在子结点,进行标记
for (int j = 0; j < k; j++)
{
scanf("%d", &c);
father[c] = id; // 记录父结点信息
}
}
for (int i = 1; i <= n; i++) // 更新结点层数
{
level[i] = findLevel(i);
if (level[i] > maxLevel)
maxLevel = level[i]; // 更新最大层数
}
for (int i = 1; i <= n; i++) // 更新每层叶结点个数
{
if (isFather[i] == false)
leaf[level[i]]++;
}
for (int i = 1; i <= maxLevel; i++) // 输出
{
if (i > 1)
printf(" ");
printf("%d", leaf[i]);
}
return 0;
}