PAT 1004

题目描述

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

题目大意

在第一行,给出树的总结点数和非叶节点数。
在第二行给出所有的叶节点和他们的自节点数目与子节点的标号。
默认了树根节点是01,这样的话就节省了找根节点的步骤,降低了coding难度。
输出则按照从上层到下层的顺序依次输出每一层的叶子结点。

思路

  1. 重构整棵树,然后遍历,找每一层的叶节点。但实际操作起来太过复杂,题目没有保证这是一棵二叉树。
  2. 使用DFS,题目中的焦点在于没有子节点和层数,而不是父节点是什么与整棵树的前后继关系。
  3. 使用BFS

DFS

先贴代码

#include<cstdio>
#include<vector>
using namespace std;
vector<int> v[100];
int num[100];
int n;
int maxdepth = 0;

int CountLeaves(int index, int depth) {
	if (v[index].size() == 0) {
		num[depth]++;
		maxdepth = maxdepth > depth ? maxdepth : depth;
		return 0;
	}
	for (int i = 0; i < v[index].size(); i++) {
		CountLeaves(v[index][i], depth + 1);
	}
}

int main() {
	int m;
	int id, numleaves, temp;
	scanf("%d %d", &n, &m);
	for (int i = 1; i <= m; i++) {
		scanf("%d %d", &id, &numleaves);
		for (int j = 0; j < numleaves; j++) {
			scanf("%d", &temp);
			v[id].push_back(temp);
		}
	}
	CountLeaves(1, 0);
	printf("%d", num[0]);
	for (int i = 1; i <= maxdepth; i++) {
		printf(" %d", num[i]);
	}
	return 0;
}

具体操作和思路

  1. 代码中使用了vector这样的动态二维数组,是因为不知道每个节点的子节点数目,为了节省空间所以使用了vector。
  2. 对于数据输入处理比较直观,只需要对每个子节点压入数组中即可。
  3. 在CountLeaves中,其实就是一个DFS。使用树的递归通解:
    1. 递归出口,也就是找到叶子结点
    2. 设计递归函数,有了递归出口,则按照DFS思想设计递归即可。
    3. 对于最大深度的处理:只有碰到了叶子结点才有可能更新最大深度,所以在递归出口处加一个对最大深度的处理,处理最大深度的目的在于在输出的时候找到数组的界。
  4. 根据题目信息,初始传入函数的数据就是(1,0)。

BFS

先贴代码

#include<cstdio>
#include<vector>
#include<queue>
using namespace std;
int n;
int level[100], book[100], maxlevel = 0;
vector<int> v[100];

int CountLeaves() {
	queue<int> q;
	q.push(1);
	while (!q.empty()) {
		int index = q.front();
		q.pop();
		maxlevel = maxlevel > level[index] ? maxlevel : level[index];
		if (v[index].size() == 0) {
			book[level[index]]++;
		}
		for (int i = 0; i < v[index].size(); i++) {
			q.push(v[index][i]);
			level[v[index][i]] = level[index] + 1;
		}
	}
}

int main() {
	int m;
	int id, numleaves, temp;
	scanf_s("%d %d", &n, &m);
	for (int i = 1; i <= m; i++) {
		scanf_s("%d %d", &id, &numleaves);
		for (int j = 0; j < numleaves; j++) {
			scanf_s("%d", &temp);
			v[id].push_back(temp);
		}
	}
	CountLeaves();
	printf("%d", book[0]);
	for (int i = 1; i <= maxlevel; i++) {
		printf(" %d", book[i]);
	}
	return 0;
}

具体操作和思路

  1. 对于输入输出的处理和DFS方法一模一样
  2. 在核心代码CountLeaves中,使用了queue结构。因为BFS使用思想是层序遍历,需要使用队列来帮助完成层序遍历。
  3. 使用了两个数组,分别是book[], level[], book[i]表示第i层的叶子结点个数,level[i]表示第i个结点的层数。
  4. 和上面的思路一样,题目本身并不注重于父子结点的具体关系,只需要通过父节点的层数得到子节点所在层数即可。
  5. 函数本身就是对思路和的直接翻译,并未涉及到特殊的技巧和操作。

参考:https://blog.csdn.net/liuchuo/article/details/52214833?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值