1004. Counting Leaves (30) 解析和记录

原创 2018年04月15日 01:58:19

题目就不贴了,思路如下:

1.定义一个类:主要有vector<int> child 和 colour组成;child 用来存储该节点的子代,colour 用来标记叶子(WHITE)和中间节点(BLACK)。

2.由于 0 < N < 100, 为了简化计算定义类数组来进行寻址,注意数组大小可以定义成101,方便直接从1开始寻址

3.输入数据,用类数组的数据结构来表示这颗树,并对节点进行着色,将子代放入vector

4.通过BFS广度优先思想来遍历这颗树,用队列queue来实现广度优先算法,其算法复杂度为O(N)

坑点:

第三个测试用例,如果不注意1 0的输入会判错的,因为只输入一个根节点,所以应该输出 1;

代码如下:

#include <iostream>
#include <vector>
#include <sstream>
#include <queue>
#include <cstring>

#define WHITE 0
#define BLACK 1
#define MAX_NODE 500
using namespace std;

struct node{
	vector<int> child;
	int colour;
	node(): colour(WHITE) {}
};

queue<int> list;
int n_n;
int m_n;
node array[MAX_NODE];

int main(void)
{
	string line;
	while(getline(cin, line))
	{
		/*input*/
		int record;	
		stringstream sss(line);
		sss >> n_n;
		if(n_n == 1)
		{
			cout << 1 << "\n";
			continue;
		}
		sss >> m_n;
		memset(array, 0, sizeof(node) * MAX_NODE);
		for(int i = 0; i < m_n; i++)
		{
			int id;
			int k;
			getline(cin, line);
			stringstream ss(line);
			ss >> id;
			ss >> k;
			for(int j = 0; j < k; j++)
			{
				int temp;
				ss >> temp;
				array[id].child.push_back(temp);
				array[id].colour = BLACK;
			}

		}

		/*process*/
		record = 1;
		list.push(1);
		cout << 0;
		while(!list.empty())
		{
			int times = 0;
			int next_record = 0;
			for(int j = 0; j < record; j++)
			{
				int temp;
				temp = list.front();
				list.pop();
				for(unsigned int i = 0; i < array[temp].child.size(); i++)
				{
					if(array[array[temp].child[i]].colour ==  BLACK)
					{
						list.push(array[temp].child[i]);
						next_record++;
					}
					else
						times++;
				}
			}
			record  = next_record;
			cout << " " << times;
		}
		cout << "\n";
	}

	return 0;
}




pat1004Counting Leaves (30)

题意分析: (1)给出树中节点的总数以及非叶子节点的总数,并给出各非叶子节点的编号、其孩子节点的个数、其孩子节点的编号,求出每一层叶子节点的个数 (2)题意是求出每一层的叶子结点数,其次给出的节点的父...
  • LostStephon
  • LostStephon
  • 2015-10-02 14:32:01
  • 742

1004. Counting Leaves (30)

  • 2017年01月04日 15:57
  • 2KB
  • 下载

PAT甲级 1004.Counting Leaves (30) 题目翻译与答案

1004.计算叶子个数 一个家庭的层级结构经常被表现为一个家谱树。你的任务是统计这些家庭成员中谁没有孩子。 输入 每个输入文件包含一个测试实例。每个实例开始的一行包含N和M,N指树中的结点个数(0...
  • qq278672818
  • qq278672818
  • 2017-02-07 20:29:53
  • 972

PAT Advanced 1004. Counting Leaves (30) (C语言实现)

题目 A family hierarchy is usually presented by a pedigree tree. Your job is to count those family me...
  • Oliver__Lew
  • Oliver__Lew
  • 2017-05-18 23:47:41
  • 542

PAT 1004. Counting Leaves (30)(Java一个测试点未通过 and C++)

1004. Counting Leaves (30) Java代码:diwu
  • u010388829
  • u010388829
  • 2014-09-10 09:14:06
  • 870

pat 1004 Counting Leaves (30)

A family hierarchy is usually presented by a pedigree tree. Your job is to count those family member...
  • hyf20144055065
  • hyf20144055065
  • 2016-07-19 16:17:26
  • 359

PAT-A 1004. Counting Leaves (30) (树)

题目链接:https://www.patest.cn/contests/pat-a-practise/1004 题意: 样例 输入: 2 1 01 1 02 输出: 0 ...
  • Strokess
  • Strokess
  • 2016-07-29 21:42:50
  • 370

PAT (Advanced Level) 1004. Counting Leaves (30)

1004. Counting Leaves (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard...
  • UNuomo
  • UNuomo
  • 2017-03-08 21:05:34
  • 156

浙大PAT 1004 Counting Leaves (简单深搜)

1004. Counting Leaves (30) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard...
  • IAccepted
  • IAccepted
  • 2014-03-15 16:32:24
  • 5378

PAT1004. Counting Leaves (30)

题目如下: A family hierarchy is usually presented by a pedigree tree. Your job is to count those family...
  • wtrnash
  • wtrnash
  • 2017-03-30 19:45:10
  • 115
收藏助手
不良信息举报
您举报文章:1004. Counting Leaves (30) 解析和记录
举报原因:
原因补充:

(最多只允许输入30个字)