题意:给出一棵树的点数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)的更多相关文章

  1. PAT 1004 Counting Leaves (30分)

    1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is t ...

  2. 1004 Counting Leaves (30分) DFS

    1004 Counting Leaves (30分)   A family hierarchy is usually presented by a pedigree tree. Your job is ...

  3. PAT 甲级 1049 Counting Ones (30 分)(找规律,较难,想到了一点但没有深入考虑嫌麻烦)***

    1049 Counting Ones (30 分)   The task is simple: given any positive integer N, you are supposed to co ...

  4. 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 ...

  5. PAT A 1004. Counting Leaves (30)【vector+dfs】

    题目链接:https://www.patest.cn/contests/pat-a-practise/1004 大意:输出按层次输出每层无孩子结点的个数 思路:vector存储结点,dfs遍历 #in ...

  6. PAT 甲级 1004 Counting Leaves

    https://pintia.cn/problem-sets/994805342720868352/problems/994805521431773184 A family hierarchy is ...

  7. PAT甲级 1004.Counting Leaves

    参考:https://blog.csdn.net/qq278672818/article/details/54915636 首先贴上我一开始的部分正确代码: #include<bits/stdc ...

  8. 1004 Counting Leaves (30 分)

    A family hierarchy is usually presented by a pedigree tree. Your job is to count those family member ...

  9. 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 ...

随机推荐

  1. SDP协议介绍

    一.SDP协议介绍 SDP 完全是一种会话描述格式 ― 它不属于传输协议 ― 它只使用不同的适当的传输协议,包括会话通知协议(SAP).会话初始协议(SIP).实时流协议(RTSP).MIME 扩展协 ...

  2. IDEA 运行项目、模块的多个实例

    IDEA默认只能运行同一项目|模块的一个实例. 运行多个实例: 比如springcloud的端口设置: --server.port=9001 . 当然,也可以在项目的配置文件中修改参数. 命令行.ID ...

  3. C#中对虚拟属性和抽象属性的重写有什么异同

           public abstract class A         {             //抽象属性不能有实现代码             public abstract strin ...

  4. gbase安装教程

    一.安装前的准备工作 1.对网卡进行配置 [root@gbase8a ~]#vi /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 HWADD ...

  5. Django之form表单操作

    小白必会三板斧 from django.shortcuts import render,HttpResponse,redirect HttpRespone:返回字符串 render:返回html页面 ...

  6. js语言简介

    JS语言概述 JS语言简史 JS语言的起源 网景(Netscape Communication Corperation),1994年,推出第一款商用浏览器,网景浏览器(Netscape Navigat ...

  7. 其他 - 02. poolmon 安装

    1. 概述 遇到 win10 的内存泄露 32G 内存都能给吃光 2. 思路 rammap 对整体内存做一个诊断 主要是内存分配 用途 状态 poolmon 确认内存的用途 比 rammap 更精确 ...

  8. 学会C#可以做什么

    C#基于.NET Framework  和 .NET CORE平台 Client/Server 客户端/服务端 windows桌面应用程序 winform  2D WPF  3D Browser/Se ...

  9. 【Vue CLI】从安装到构建项目再到目录结构的说明

    目录 1. 构建我们的项目 2. 目录结构说明 2.1 build目录 2.2 config目录 2.3 src目录 2.4 static目录 "Vue CLI是一个基于Vue.js进行快速 ...

  10. 常用的vi的命令模式下的快捷命令

    知识点 在VI命令模式下:y 表示拷贝, d 表示删除,p标识粘贴 1.删除 dw 表示删除从当前光标到光标所在单词结尾的内容. d0 表示删除从当前光标到光标所在行首的内容. d$ 表示删除从当前光 ...