7-4 Counting Leaves(

7-4树叶

A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.
家庭等级通常由谱系树表示。您的工作是计算没有孩子的家庭成员。

Input Specification:

The input consists of several test cases, each 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.
输入规格:
输入由几个测试用例组成,每个用例都从包含0 <N <100(一行在树中的节点数)和M(<N)(非叶节点的数)的行开始。然后,接着M行,每行的格式为ID K ID [1] ID [2] … ID [K],其中ID是代表给定非叶节点的两位数字,K是其子代的数目,随后是其子代的两位数字ID序列。为了简单起见,让我们将根ID固定为01。

输入以N为0结尾。不得处理这种情况。

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.

For example, the first 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.
输出规格:
对于每个测试用例,应该从根开始计算每个资历级别中没有孩子的家庭成员。数字必须成行打印,并用空格隔开,并且每行末尾不得有多余的空格。

例如,第一个样本案例表示只有2个节点的树,其中01是根,而02是其唯一的子节点。因此,在根01级别上,存在0个叶节点;在下一个级别上,有1个叶节点。然后,我们应该在一行中输出0 1。

Sample Input:

2 1
01 1 02
1 0
7 4
01 2 02 03
06 1 07
02 2 04 05
03 1 06
0 0

Sample Output:

0 1
1
0 0 2 1

解析

这题的意思是每一层有几个叶节点,当输入7 4 时

DFS

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

vector<int> v[100];
int book[100], maxdepth = -1;

void dfs(int index, int depth) {
	if(v[index].size() == 0) {
		book[depth]++;
		maxdepth = max(maxdepth, depth);
		return ;
	}
	for(int i = 0; i < v[index].size(); i++){
		dfs(v[index][i], depth + 1);
		
	}
		
}
int main() {
	int n, m, k, node, c;
	scanf("%d %d", &n, &m);
	while(true){
		if(n==0&&m==0)
			return 0;
		for(int i=0;i<100;i++){
			book[i]=0;
			v[i].clear();
		}
			
		maxdepth=-1;
		
		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);
			}	
		}
		dfs(1, 0);
		printf("%d", book[0]);
		for(int i = 1; i <= maxdepth; i++)
			printf(" %d", book[i]);
		cout<<endl;
		scanf("%d %d", &n, &m);
	}
	
	return 0;
}

数组实现

#include<bits/stdc++.h>
using namespace std;
int a[105][105];
int book[105];
int maxdepth=-1;

void def(int index,int deptf){
	if(a[index][0]==0){
		book[deptf]++;
		if(deptf>=maxdepth){
			maxdepth=deptf;
		}
		return ;
	}
	for(int i=1;i<=a[index][0];i++){
		def(a[index][i],deptf+1);
	}
}
int main(){
	int n,m;
	
	cin>>n>>m;
	while(true){
		for(int i=0;i<105;i++){
			book[i]=0;
			for(int j=0;j<105;j++){
				a[i][j]=0;
			}
		}
		maxdepth=-1;
		if(n==0&&m==0)
			break;
		int x,y,z;
		for(int i=1;i<=m;i++){
			cin>>x>>y;
			a[x][0]=y;
			for(int j=1;j<=y;j++){
				cin>>z;
				a[x][j]=z;
			}		
		}
		def(1,0);
		cout<<book[0];
		for(int i=1;i<=maxdepth;i++){
			cout<<" "<<book[i];
		}
		cout<<endl;
		cin>>n>>m;
	}
	return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值