【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 ...
随机推荐
- sublime 神一样的插件
专属配置 // 主题 "theme": "Boxy Tomorrow.sublime-theme", "theme_grid_border_size_ ...
- 对C#单例模式的理解
2018年11月6日 小雨 一.单例模式的定义 确保一个类只有一个实例,并提供一个全局访问点来访问这个唯一的实例,是一种对象创建型模式,有如下3个要点: 只能有一个实例 必须是自行创建这个 ...
- MongoDB-2 安装与配置
安装建议 MongoDB 可以在mac/win/linux上安装,我个人建议在linux上安装会更好,这样测试起来更方便.一般来说本地只装mysql或者MariaDB,其余的中间件一律放linux,让 ...
- ToolStripComboBox的DataSource和DataTable使用技巧
可以使用Items属性private void GetData() //一下数据均为测试{toolStripComboBox1.Items.Clear(); DataTabl ...
- 交换机的MAC地址?
该示例中记录了Cat2960的MAC地址情况: Cat2960#sho mac address-table Mac Address Table----------------------------- ...
- Linux shell sed 命令详解
详细的sed命令详解,请参考https://my.oschina.net/u/3908182/blog/1921761 sed命令常见用途 查找关键词做全局替换 查找某行的关键词做替换 查找关键字所在 ...
- 【Python爬虫程序】抓取MM131美女图片,并将这些图片下载到本地指定文件夹。
一.项目名称 抓取MM131美女写真图片,并将这些图片下载到本地指定文件夹. 共有6种类型的美女图片: 性感美女 清纯美眉 美女校花 性感车模 旗袍美女 明星写真 抓取后的效果图如下,每个图集是一个独 ...
- Java 前加加和后加加 总结
public class Test { public static void main(String[] args) { int age = 6; //先自加,再使用(age先自加1,然后再打印age ...
- Postman的使用和测试
1.输入认证的IP,获取headers 2.输入用户名及密码 3.带着headers去访问网址 4.传参
- Unity生成的WebGL如何在浏览器中运行
前言:以为在学完了COMP30019后,应该不会再接触Unity了,没想到之后实习让我去做把一个Unity项目转到WebGL,而关于Unity的WebGL资料很少,基本除了Unity的Manual就只 ...