PAT 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 containing0<N<100, the number of nodes in a tree, andM(<N), the number of non-leaf nodes. ThenMlines follow, each in the format:
ID K ID[1] ID[2] ... ID[K]
whereID
is a two-digit number representing a given non-leaf node,K
is the number of its children, followed by a sequence of two-digitID
's of its children. For the sake of simplicity, let us fix the root ID to be01
.
The input ends withNbeing 0. That case must NOT be processed.
Output Specification:
For each test case, you are supposed to count those family members who have no childfor every seniority levelstarting 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, where01
is the root and02
is its only child. Hence on the root01
level, there is0
leaf node; and on the next level, there is1
leaf node. Then we should output0 1
in a line.
Sample Input:
2 1
01 1 02
Sample Output:
0 1
注意点
- 本题的意思就是输出每一层叶子结点的个数。
- 注意最大深度
max_h
的记录,在dfs内部直接去最大值即可,便于后面输出。 - 输出时由于格式问题,先输出第一个(特殊),在输出之后几个(一般)更加简便。
- dfs相对于bfs比较简单一点,无需新开一个数组记录结点的高度。
代码
- 深度优先遍历(dfs)
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn = 110;
struct node
{
vector<int> child;
}Node[maxn];
int n, m;
int num[maxn];
int max_h = 0;
void dfs(int index, int depth)
{
max_h = max(depth, max_h);
if (Node[index].child.size() == 0)
{
num[depth] ++;
return;
}
for (int i = 0; i < Node[index].child.size(); i ++)
{
dfs(Node[index].child[i], depth + 1);
}
}
int main()
{
scanf("%d %d", &n, &m);
int child;
int id, k;
for (int i = 1; i <= m; i ++)
{
scanf("%d %d", &id, &k);
for (int j = 0; j < k; j ++)
{
scanf("%d", &child);
Node[id].child.push_back(child);
}
}
dfs(1, 1);
printf("%d", num[1]);
for (int i = 2; i <= max_h; i ++)
{
printf(" %d", num[i]);
}
return 0;
}
- 广度优先遍历(bfs)
#include <cstdio>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 110;
struct node
{
vector<int> child;
}Node[maxn];
int n, m;
int num[maxn];
int h[maxn]; //记录结点的高度
int max_h = 0;
void bfs(int root)
{
queue<int> q;
q.push(root);
while (!q.empty())
{
int now = q.front();
q.pop();
max_h = max(max_h, h[now]);
if (Node[now].child.size() == 0)
{
num[h[now]] ++;
}
for (int i = 0; i < Node[now].child.size(); i ++)
{
int child = Node[now].child[i];
h[child] = h[now] + 1;
q.push(child);
}
}
}
int main()
{
scanf("%d %d", &n, &m);
int child;
int id, k;
for (int i = 1; i <= m; i ++)
{
scanf("%d %d", &id, &k);
for (int j = 0; j < k; j ++)
{
scanf("%d", &child);
Node[id].child.push_back(child);
}
}
h[1] = 1;
bfs(1);
printf("%d", num[1]);
for (int i = 2; i <= max_h; i ++)
{
printf(" %d", num[i]);
}
return 0;
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。