【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 ...
随机推荐
- 使用maven构建项目时,SSM和springboot项目的打包与云服务器部署
下面讲讲如何打包SSM和springboot项目,并部署到云服务器上. 由于使用的IDE不同,有的使用eclipse,有的使用idea,所以如果在IDE中按照 maven clean 再 maven ...
- zookeeper linux分布式部署
安装包下载地址:http://mirror.bit.edu.cn/apache/zookeeper,记住要下载那个bin的不要下tar.gz包不然即使你安装了也会报错误: 找不到或无法加载主类org. ...
- iview修改table组件实现循环向上滚屏
前提,最近项目中需要实现table的滚屏效果,并且使用的是iview的table组件,踩坑,填坑如下. 1.首先找到Table组件中的table,就是这个class:ivu-table-body te ...
- 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 ...
- ubuuntu截图
方法1: 按 print screen sysrq 方法2: 系统设置 选择键盘 选择快捷键窗口 选择截图 按照自己的习惯更改快捷键即可.
- drf的序列化和反序列化
序列化器--Serializer 选项参数: max_length 最大长度 min_length 最小长度 allow_blank 是否允许为空 trim_whitespace 是否截断空白字符 m ...
- js 判断数组中是否包含某个元素
vuex中结合使用v-if: 链接:https://www.cnblogs.com/hao-1234-1234/p/10980102.html
- gulp 搭建静态服务器
步骤: 安装依赖:npm i browser-sync --save-dev 导入browser-sync,通过create创建 设置Sass和Js任务,将其压缩重命名并引入页面,任务结束时reloa ...
- 查询数据操作:limit
1.作用: 在查看数据时用于限制获得的记录数量,一般放在最后. 2.语法: limit offset,row_count; 解析: offset:偏移量,索引值默认从0开始,可以省略 row_coun ...
- select2多选框初始化默认值和获得值
select2多选自带手动输入搜索功能,可怜我还查寻半天api 获得值: //chang函数获取选择的option $(".js-example").change(function ...