【PAT甲级】1004 Counting Leaves (30 分)(BFS)
题意:给出一棵树的点数N,输入M行,每行输入父亲节点An,儿子个数n,和a1,a2,...,an(儿子结点编号),从根节点层级向下依次输出当前层级叶子结点个数,用空格隔开。(0<N<100)
AAAAAccepted code:
#include<bits/stdc++.h>
using namespace std;
vector<int>v[];
queue<int>q;
int tmp=;
int num=;
int cnt=;
int tim=;
int tt=;
void BFS(){
int x=q.front();//队头元素调用
if(v[x].size())
for(auto it:v[x])//遍历x的所有儿子结点
if(v[it].size()==)
num++;//叶子结点++
else{
q.push(it);//非叶子结点进队列
cnt++;//非叶子结点计数器++
}
q.pop();//队头元素弹出
tim++;//处理次数++
}
int main(){
int n,m;
cin>>n>>m;
for(int i=;i<=m;++i){
string fa;
cin>>fa;
int baba=(fa[]-'')*+fa[]-'';
int x;
cin>>x;
string son;
for(int j=;j<=x;++j){
cin>>son;
int erzi=(son[]-'')*+son[]-'';
v[baba].push_back(erzi);
}
}
if(v[].size()==){
cout<<"";
return ;
}
else
cout<<"";
for(auto it:v[])
if(v[it].size()==)
num++;//叶子结点个数
else{
q.push(it);//非叶子结点进队列
tt++;//非叶子结点个数
}
cout<<" "<<num;
num=;
while(!q.empty()){
BFS();
if(tim==tt){//处理次数==非叶子结点个数,相当于这一层处理完了,开始统计这一层下一层有多少叶子结点
cout<<" "<<num;
tim=;//处理次数初始化
num=;//叶子结点个数初始化
tt=cnt;//非叶子结点个数更新为计数器
cnt=;//非叶子节点计数器初始化
}
}
return ;
}
【PAT甲级】1004 Counting Leaves (30 分)(BFS)的更多相关文章
- PAT 1004 Counting Leaves (30分)
		
1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is t ...
 - 1004 Counting Leaves (30分)  DFS
		
1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is ...
 - PAT 甲级 1049 Counting Ones (30 分)(找规律,较难,想到了一点但没有深入考虑嫌麻烦)***
		
1049 Counting Ones (30 分) The task is simple: given any positive integer N, you are supposed to co ...
 - PAT Advanced 1004 Counting Leaves (30) [BFS,DFS,树的层序遍历]
		
题目 A family hierarchy is usually presented by a pedigree tree. Your job is to count those family mem ...
 - PAT A 1004. Counting Leaves (30)【vector+dfs】
		
题目链接:https://www.patest.cn/contests/pat-a-practise/1004 大意:输出按层次输出每层无孩子结点的个数 思路:vector存储结点,dfs遍历 #in ...
 - PAT 甲级 1004 Counting Leaves
		
https://pintia.cn/problem-sets/994805342720868352/problems/994805521431773184 A family hierarchy is ...
 - PAT甲级 1004.Counting Leaves
		
参考:https://blog.csdn.net/qq278672818/article/details/54915636 首先贴上我一开始的部分正确代码: #include<bits/stdc ...
 - 1004 Counting Leaves (30 分)
		
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family member ...
 - PTA 1004 Counting Leaves (30)(30 分)(dfs或者bfs)
		
1004 Counting Leaves (30)(30 分) A family hierarchy is usually presented by a pedigree tree. Your job ...
 
随机推荐
- 【转载】Java开发中的23种设计模式
			
转自:http://zz563143188.iteye.com/blog/1847029 一.设计模式的分类 总体来说设计模式分为三大类: 创建型模式,共五种:工厂方法模式.抽象工厂模式.单例模式.建 ...
 - 我的reshape观
			
reshape(1,2)把结果分成1块,每一块2个元素 reshape(2,1)把结果分成2块,每一块1个元素 reshape(-1,1)把结果分成任意块,每一块1个元素 reshape(1,-1)把 ...
 - python 鸢尾花数据集报表展示
			
import seaborn as snsimport pandas as pdimport matplotlib.pyplot as pltsns.set_style('white',{'font. ...
 - java后台接受不到vue传的参数
			
@RequestMapping(value = "/delBelowImg") @Transactional public R delBelowFile(@RequestParam ...
 - Go第三方库之tail
			
Tail Demo // tail.TailFile()函数开启goroutine去读取文件,通过channel格式的t.lines传递内容. t, err := tail.TailFile(&quo ...
 - Codeforces Round #618 (Div. 1)B(几何,观察规律)
			
观察猜测这个图形是中心对称图形是则YES,否则NO #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace ...
 - Vue2.0 中,“渐进式框架”和“自底向上增量开发的设计”这两个概念是什么?(转)
			
https://www.zhihu.com/question/51907207?rf=55052497 徐飞 在我看来,渐进式代表的含义是:主张最少. 每个框架都不可避免会有自己的一些特点,从而会对使 ...
 - 深入理解python(四)python基础知识之函数
			
函数部分 函数部分大概想分成两个部分来讲,第一部分是关于对函数参数的介绍,第二部分是局部变量全局变量和内置变量也就是变量作用域的LGB原则 函数的参数 1.关于形参和实参的问题 第一点要注意的是pyt ...
 - php 基础 字符型转换整形
			
示例: 可以得出规律:以有效数字开头的,取有效数字.以非有效数字开头的都转换为0:
 - C#面向对象三大特性:封装
			
什么是封装 定义:把一个或多个项目封闭在一个物理的或者逻辑的包中.在面向对象程序设计方法论中,封装是为了防止对实现细节的访问. 封装的优点 1. 隔离性,安全性.被封装后的对象(这里的对象是泛指代码的 ...