甲级PAT1004 输出每层的叶节点数

  1. 题目大意:给出一个树,输出每层的叶节点个数。
  2. 易错点:根据题目要求,末尾不能出现多余的空格。
  3. 不认识的单词(本英语渣渣的专属板块):
    hierarchy 原意是等级制,这里指整个家族
    pedigree tree 谱系树 族谱树
    *non-leaf nodes 是非叶节点不是没有叶的节点!
    For the sake(目的) of simplicity(简便), let us fix the root ID to be 01.为了简便,让我们把根节点设为1。
    seniority 原意是资历,这里指每层。
  4. 代码
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
struct Node{
	vector<int> child;
	int floor;
	bool flag;
}node[110];
int Max=0;
int ans[110];
void get_floor(int root,int floor){
	node[root].floor=floor;
	if (Max<floor) Max=floor;
	for(int i=0;i<node[root].child.size();i++){
		get_floor(node[root].child[i],floor+1);
	}
	return;
}
int main(){
	//freopen("D:\\PAT\\in.txt","r",stdin);
	int M,N,tempa,tempb,n;cin>>M>>N;
	for(int i=0;i<M;i++){
		cin>>tempa>>n;
		for(int j=0;j<n;j++){
			cin>>tempb;
			node[tempa].child.push_back(tempb);
			node[tempa].flag=true;
		}
	}
	get_floor(1,0);
	for(int i=1;i<=M;i++) {
		if(node[i].flag==false)  ans[node[i].floor]++;
	}
	for(int i=0;i<Max;i++) cout<<ans[i]<<" ";
	cout<<ans[Max];
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值