题意:给出一棵树的点数N,输入M行,每行输入父亲节点An,儿子个数n,和a1,a2,...,an(儿子结点编号),从根节点层级向下依次输出当前层级叶子结点个数,用空格隔开。(0<N<100)

AAAAAccepted code:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. vector<int>v[];
  4. queue<int>q;
  5. int tmp=;
  6. int num=;
  7. int cnt=;
  8. int tim=;
  9. int tt=;
  10. void BFS(){
  11. int x=q.front();//队头元素调用
  12. if(v[x].size())
  13. for(auto it:v[x])//遍历x的所有儿子结点
  14. if(v[it].size()==)
  15. num++;//叶子结点++
  16. else{
  17. q.push(it);//非叶子结点进队列
  18. cnt++;//非叶子结点计数器++
  19. }
  20. q.pop();//队头元素弹出
  21. tim++;//处理次数++
  22. }
  23. int main(){
  24. int n,m;
  25. cin>>n>>m;
  26. for(int i=;i<=m;++i){
  27. string fa;
  28. cin>>fa;
  29. int baba=(fa[]-'')*+fa[]-'';
  30. int x;
  31. cin>>x;
  32. string son;
  33. for(int j=;j<=x;++j){
  34. cin>>son;
  35. int erzi=(son[]-'')*+son[]-'';
  36. v[baba].push_back(erzi);
  37. }
  38. }
  39. if(v[].size()==){
  40. cout<<"";
  41. return ;
  42. }
  43. else
  44. cout<<"";
  45. for(auto it:v[])
  46. if(v[it].size()==)
  47. num++;//叶子结点个数
  48. else{
  49. q.push(it);//非叶子结点进队列
  50. tt++;//非叶子结点个数
  51. }
  52. cout<<" "<<num;
  53. num=;
  54. while(!q.empty()){
  55. BFS();
  56. if(tim==tt){//处理次数==非叶子结点个数,相当于这一层处理完了,开始统计这一层下一层有多少叶子结点
  57. cout<<" "<<num;
  58. tim=;//处理次数初始化
  59. num=;//叶子结点个数初始化
  60. tt=cnt;//非叶子结点个数更新为计数器
  61. cnt=;//非叶子节点计数器初始化
  62. }
  63. }
  64. return ;
  65. }

【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. 使用maven构建项目时,SSM和springboot项目的打包与云服务器部署

    下面讲讲如何打包SSM和springboot项目,并部署到云服务器上. 由于使用的IDE不同,有的使用eclipse,有的使用idea,所以如果在IDE中按照 maven clean 再 maven ...

  2. zookeeper linux分布式部署

    安装包下载地址:http://mirror.bit.edu.cn/apache/zookeeper,记住要下载那个bin的不要下tar.gz包不然即使你安装了也会报错误: 找不到或无法加载主类org. ...

  3. iview修改table组件实现循环向上滚屏

    前提,最近项目中需要实现table的滚屏效果,并且使用的是iview的table组件,踩坑,填坑如下. 1.首先找到Table组件中的table,就是这个class:ivu-table-body te ...

  4. numpy (1.16.4) has the default value of allow_pickle as False.

    My version of numpy (1.16.4) has the default value of allow_pickle as False. numpy版本是1.16.4,allow_pi ...

  5. ubuuntu截图

    方法1: 按 print screen sysrq 方法2: 系统设置 选择键盘 选择快捷键窗口 选择截图 按照自己的习惯更改快捷键即可.

  6. drf的序列化和反序列化

    序列化器--Serializer 选项参数: max_length 最大长度 min_length 最小长度 allow_blank 是否允许为空 trim_whitespace 是否截断空白字符 m ...

  7. js 判断数组中是否包含某个元素

    vuex中结合使用v-if: 链接:https://www.cnblogs.com/hao-1234-1234/p/10980102.html

  8. gulp 搭建静态服务器

    步骤: 安装依赖:npm i browser-sync --save-dev 导入browser-sync,通过create创建 设置Sass和Js任务,将其压缩重命名并引入页面,任务结束时reloa ...

  9. 查询数据操作:limit

    1.作用: 在查看数据时用于限制获得的记录数量,一般放在最后. 2.语法: limit offset,row_count; 解析: offset:偏移量,索引值默认从0开始,可以省略 row_coun ...

  10. select2多选框初始化默认值和获得值

    select2多选自带手动输入搜索功能,可怜我还查寻半天api 获得值: //chang函数获取选择的option $(".js-example").change(function ...