1004. Counting Leaves (30)

1.题目要求计算家族树中,每层叶子节点的数量(即没有后代的人的个数)。

2.可为多叉树,采用vector<string>保存子树名称。

3.采用map记录树节点。

4.采用广度遍历,输出每层的叶子节点数量。

节点代码如下:

struct node{
	vector<string> child;
	int head;
	string ID;
	node() :head(-1), child(0), ID(""){};
};

A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.

Input

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.

Output

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

AC代码:

//#include<string>
//#include <iomanip>
#include<vector>
#include <algorithm>
//#include<stack>
#include<set>
#include<queue>
#include<map>
//#include<unordered_set>
//#include<unordered_map>
//#include <sstream>
//#include "func.h"
//#include <list>
#include<stdio.h>
#include<iostream>
#include<string>
#include<memory.h>
using namespace std;
#define maxDist 999999
struct node{
	vector<string> child;
	int head;
	string ID;
	node() :head(-1), child(0), ID(""){};
};

int main(void) {

	int n, m;
	cin >> n >> m;

	map<string, node> tree;
	for (int i = 0; i < m; i++)
	{//循环输入父子关系
		string ID;
		cin >> ID;
		tree[ID].ID = ID;
		int k;
		cin >> k;
		for (int i = 0; i < k; i++)
		{
			string tmp;
			cin >> tmp;
			if (tmp.size() == 1)
				tmp = "0" + tmp;//避免出现单位的数字如1,2,3,4;题目要求是两位数,所以是01,02,03,04
			tree[ID].child.push_back(tmp);
			tree[tmp].ID = tmp;
			tree[tmp].head++;
		}
	}
	//需要建立一个前头节点,便于循环计算
	tree["superhead"].ID = "";
	tree["superhead"].child = vector<string>(0);
	tree["superhead"].head = 100;
	string headID = "";
	tree["superhead"].child.push_back("01");
	headID = "superhead";
	queue<string> q;
	q.push(headID);
	vector<int> ans(0);
	int count1 = 1;
	int count2 = 0;
	int leaf = 0;
	while (!q.empty())
	{
		for (int k = 0; k < count1; k++)
		{//上一层有count1个
			string top = q.front();
			q.pop();
			for (int i = 0; i < tree[top].child.size(); i++)
			{
				string tmp = tree[top].child[i];
				if (tree[tmp].child.size() == 0)
					leaf++;//如果没有子节点,那么就是叶子节点
				else
				{
					q.push(tmp);//有子节点,压进队列,继续遍历
					count2++;
				}
			}
		}
		ans.push_back(leaf);
		leaf = 0;
		count1 = count2;
		count2 = 0;
	}
	for (int i = 0; i < ans.size(); i++)
	{
		cout << ans[i];
		if (i != ans.size() - 1)
			cout << " ";
	}
	return 0;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注