1004 Counting Leaves

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

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

链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805521431773184

思路:类似链表存储,不过用的是vector。然后深度优先搜索,从一个节点开始不断往下。直到vector[i].size()==0表明该结点的子孩子为零,即叶子结点。

代码:

#include 
#include 
#include 
#include 
using namespace std;

int n, m,maxdepth;
int res[110];
vector tree[110];

int digi(char str[])
{
	int len = strlen(str);
	int num = 0;
	for (int i = 0; i < len; i++)
		num = num * 10 + (str[i] - '0');
	return num;
}

void dfs(int x, int depth)
{
	if (tree[x].size() == 0)
	{
		res[depth]++;
		if (maxdepth < depth) maxdepth = depth;
		return;
	}
	for (int i =0; i < tree[x].size(); i++)
		dfs(tree[x][i], depth + 1);
}

int main()
{
	while (cin >> n && n)
	{
		memset(res, 0, sizeof(res));
		maxdepth = 0;
		cin >> m;
		char id[3];
		int k;
		for (int i = 0; i < m; i++)
		{
			cin >> id >> k;
			int pare = digi(id);
			for (int i = 0; i < k; i++)
			{
				cin >> id;
				int child = digi(id);
				tree[pare].push_back(child);
			}
		}
		dfs(1, 0);
		cout << res[0];
		for (int i = 1; i <= maxdepth; i++)
			cout << " " << res[i];
		cout << endl;
	}
	return 0;
}

还有网上找的另一种解题方法,用的bfs,但是无奈一直对bfs无感啊~

来源:https://blog.csdn.net/CV_Jason/article/details/80901866

#define  _CRT_SECURE_NO_WARNINGS
#include 
#include 
#include 
#include 
using namespace std;
int level[100];// 保存当前结点的所在层数 
int book[100];// 记录每层 
int maxlevel = -1;
vector v[100];
void bfs() {
	queue q;
	q.push(1);
	level[1] = 0;
	while (!q.empty()) {
		// 获取队首元素 
		int index = q.front();
		q.pop();
		// 更新最大层数 
		maxlevel = max(level[index], maxlevel);
		if (v[index].size() == 0)
			book[level[index]]++;// 统计所在层数的叶子结点 
								 // 对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 n, m, k, node, c;
	scanf("%d %d", &n, &m);
	for (int i = 0; i < m; i++) {
		scanf("%d %d", &node, &k);
		for (int j = 0; j < k; j++) {
			scanf("%d", &c);
			v[node].push_back(c);
		}
	}
	bfs();
	printf("%d", book[0]);
	for (int i = 1; i <= maxlevel; i++)
		printf(" %d", book[i]);
	return 0;
}

 

你可能感兴趣的:(#,搜索)