PAT甲级1004 Counting Leaves//BFS运用

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 N(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.

翻译:每个输入文件包含一个测试样例,每个样例都以包含N(树中结点个数)和M(非叶结点的个数)的一行开始。接着有M行,每行都是
ID K ID[1] ID[2] ... ID[K]
的形式。其中ID是一个两位数的数字,代表一个给定的非叶节点,K是它的子节点的数目,后面是它的子节点的两位数ID序列。
为了简单起见,让我们将根节点的ID定义为01。

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.

翻译:对每个测试节点,你应该从根节点数出每一辈中那些没有孩子的家庭成员。数字必须打印成一行,用空格隔开,并且每行末尾不能有多余的空格。
样例代表了一个只有两个结点的树,其中01是根结点且02是他唯一的子结点。所以在01这层中没有叶结点;并且在下一层中有一个叶结点。我们就应该在一行中输出0 1

Sample Input:

2 1
01 1 02

Sample Output:

0 1

思路

题目想了很久没看懂,其实只要一句话就能概括:给定一棵树,输出每一层中叶结点的个数。因为有关键字每一层,所以我首先想到了BFS,这里我用vector<int> tree[]的方法来存储树。比如某一结点n,子结点为p,q,则直接将pq放入tree[n]中,表示n的两个子结点为p和q。然后利用队列,一行一行的遍历整棵树的同时数这行中的叶结点个数(tree[n].size() == 0表面是叶结点)

#include <iostream>
#include <vector>
#include <cstdlib>
#include <queue>
using namespace std;
int leaf_node[102] = {0}, level = 0; //这里用数组记录每层叶结点个数
vector<int> tree[102];
int main()
{
	int n, m;
	cin >> n >> m;
	for (int i = 0; i < m; i++)
	{
		int id, k;
		cin >> id >> k;
		for (int j = 0; j < k; j++)
		{
			int node; //node代表一个子结点
			cin >> node;
			tree[id].push_back(node); //将k个子结点全部放入对应父结点的数组中
		}
	}
	queue<int> que;
	if (tree[1].size() == 0) //如果根结点没有子结点,直接输出1结束
	{
		cout << "1" << endl;
		return 0;
	}
	else
	{
		level = 1;
		que.push(1); //根结点入队
		while (!que.empty())
		{
			int size = que.size();
			for (int i = 0; i < size; i++)
			{
				int temp = que.front();
				if (tree[temp].size() == 0) //如果是叶结点,该层叶结点数+1
					leaf_node[level]++;
				for (int j = 0; j < tree[temp].size(); j++) //把该结点的子结点全部入队
					que.push(tree[temp][j]);
				que.pop(); //弹出队列
			}
			if (!que.empty()) //这里一定要加if,如果不加,当最后一个元素弹出队列时,这里level还是会加1
				level++;
		}
	}
	int flag = 0;
	for (int i = 1; i <= level; i++)
	{
		if (flag == 1)
			cout << ' ';
		cout << leaf_node[i];
		flag = 1;
	}
	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值