题目描述
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.
题目大意
一个家庭层级通常被表示成一棵家谱树。你的任务是去计算没有孩子的家庭成员的数目。
输入说明
每一个输入文件包含一个测试用例。每个测试用例的第一行含有0<N<100,它是树中节点的数目,M(<N),它是非叶子节点的数目。接下来的M行,每一行以如下形式出现:
ID K ID[1] ID[2] ... ID[K]
ID
是一个代表非叶子节点的两位数,K是它的孩子节点的数目,接下来是一串它所有孩子的两位数ID号。为了简单起见,让我们假设根节点的ID是01。
输入以N为0结尾,不得处理这种情况。
输出说明
对于每个测试用例,你应该计算从根节点开始,每一层里没有孩子的家庭成员数目。所有的数字必须输出到一行中,数字之间用空格分隔,并且在行尾没有多余的空格。
样例表示一棵有两个节点的树,01是根节点,02是它的孩子节点。因此在根节点01这一层上,有0个叶子节点。在下一层,有1个叶子节点。然后我们应该在一行中输出0 1。
题目分析
这道题考察的知识点是树的遍历。定义一个数组int leaves[105]
,其中leaves[i]
表示第i
层的叶子节点数目。在进行树的遍历时,每遍历到一个节点,先判断它的孩子节点的数目是否为0,若是,说明这个节点是叶子节点,那么就将这个节点的深度所对应的叶子节点数加1。下面提供DFS和BFS两种写法的参考代码。
DFS
#include <bits/stdc++.h>
using namespace std;
int leaves[105] = {};
int maxLevel = 0;
vector<int> tree[105];
void DFS(int s, int level) {
if (level > maxLevel)
maxLevel = level;
if (tree[s].size() == 0) {
leaves[level]++;
return;
}
for (auto i : tree[s])
DFS(i, level + 1);
}
int main()
{
int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
int id, k, child;
cin >> id >> k;
while (k--) {
cin >> child;
tree[id].push_back(child);
}
}
DFS(1, 0);
for (int i = 0; i < maxLevel; i++)
cout << leaves[i] << " ";
cout << leaves[maxLevel] << endl;
}
BFS
#include <bits/stdc++.h>
using namespace std;
int leaves[105] = {};
struct Node {
int level;
vector<int> child;
}node[105];
int maxLevel = 0;
void BFS(int s) {
node[s].level = 0;
queue<int> q;
q.push(s);
while (!q.empty()) {
auto top = q.front();
q.pop();
if (node[top].child.size() == 0) {
maxLevel = node[top].level;
leaves[node[top].level]++;
continue;
}
for (auto i : node[top].child) {
node[i].level = node[top].level + 1;
q.push(i);
}
}
}
int main()
{
int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
int id, k, child;
cin >> id >> k;
while (k--) {
cin >> child;
node[id].child.push_back(child);
}
}
BFS(1);
for (int i = 0; i < maxLevel; i++)
cout << leaves[i] << " ";
cout << leaves[maxLevel] << endl;
}