题意:给出一棵树的点数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. 题解 P2320 【[HNOI2006]鬼谷子的钱袋】

    P2320 [HNOI2006]鬼谷子的钱袋 挺有趣的一道题,之所以发这篇题解是因为感觉思路的更清晰一点qwq 此题主要有两种方法: 一.分治思想 例如要凑出1~20,假如我们已经能凑出1~10了,那 ...

  2. 自定义php-mysqli工具增强类,支持链式调用

    <?php /*数据库访问类,支持链式访问 *function table($table):表名 *function where($where):条件 *function field(...$f ...

  3. 第十七篇 Linux下常用命令汇总

  4. [Reversal 剧情设计] 设定

    在正式写之前,先交代一些设定,便于后续的展开.先在这里说明一点,我的文笔可能很烂(因为在写小说方面还没有得到过别人的评价),只要你们能体会我想表达的东西就行(不过也的确是这样,如果作者构筑得过于详细, ...

  5. C9300升级-TFTP

    1.操作命令:CAT9300(config)#ip tftp blocksize 8192CAT9300(config)#install add file tftp://10.1.100.37/cat ...

  6. React的React.createRef()/forwardRef()源码解析(三)

    1.refs三种使用用法 1.字符串 1.1 dom节点上使用 获取真实的dom节点 //使用步骤: 1. <input ref="stringRef" /> 2. t ...

  7. 关于Element UI中select组件中遇到的问题

    问题一:在使用select组件设置多选功能后,窗口抖动问题? 详细描述:在我使用select做多选的功能时出现了窗口抖动的问题,测试的时候发现,在有些人的电脑上抖动,有些人的电脑上不抖,找了很多文档, ...

  8. A. DZY Loves Chessboard

    DZY loves chessboard, and he enjoys playing with it. He has a chessboard of n rows and m columns. So ...

  9. Chinese Window Lattice And CSS

    谁向云端着此亭,檐前树木映窗棂.-- 释绍嵩<陪赵知府登桃岭山亭> (image from 中国窗棂) The traditional Chinese window lattice has ...

  10. OBS_Classic经典版框架

    一,简介 OBS(open boardcast server),是一个用于直播的开源软件. 官方网站:https://obsproject.com/ 代码托管地址:https://github.com ...