题意:给出一棵树的点数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. Codeforces Global Round 3:B. Born This Way

    Born This Way原文链接:[传送门] 题目大意:潇洒哥想乘坐飞机从A地到达C地,但是没有直达的航班,在A地和B地之间有一个可以中转的航班B,潇洒哥想早点到达C地(有航班就坐),但是很不幸他得 ...

  2. 第二十五篇 玩转数据结构——链表(Linked List)

          1.. 链表的重要性 我们之前实现的动态数组.栈.队列,底层都是依托静态数组,靠resize来解决固定容量的问题,而"链表"则是一种真正的动态数据结构,不需要处理固定容 ...

  3. 线性筛-euler,强大O(n)

    欧拉函数是少于或等于n的数中与n互质的数的数目 φ(1)=1(定义) 类似与莫比乌斯函数,基于欧拉函数的积性 φ(xy)=φ(x)φ(y) 由唯一分解定理展开显然,得证 精髓在于对于积性的应用: ){ ...

  4. 每天进步一点点------如何实现Sobel Edge Detector? (Image Processing) (C/C++)

    使用C與C++/CLI實現Sobel Edge Detector. http://www.cnblogs.com/oomusou/archive/2008/07/23/sobel_edge_detec ...

  5. 在linux里面ps -ef | grep tomcat 什么意思

    linux中的查看和tomcat有关系的进程的命令 例如 [root@localhost bin]# ps -eaf | grep tomcat root 5091 1 0 23:04 pts/0 0 ...

  6. acm数论之旅--组合数(转载)

    随笔 - 20  文章 - 0  评论 - 73 ACM数论之旅8---组合数(组合大法好(,,• ₃ •,,) )  补充:全错排公式:https://blog.csdn.net/Carey_Lu/ ...

  7. wordpress 支持上传中文名称文件

    添加文章难免要传个图.文件啥的,可是呢,上传中文名称的文件竟然不行,找了半天,中文乱码,脑残了,竟然忘了这个事,哎 修改其实很简单,只需要两步 1./wp-admin/includes/file.ph ...

  8. JAVA常量池、栈、堆的比较(转载)

    今天在学JAVA的数据存储位置的时候,看到了一篇博文感觉不错,特此转载: http://www.cnblogs.com/Eason-S/p/5658230.html JAVA中,有六个不同的地方可以存 ...

  9. js正则验证表达式验证

    /* 合法uri */ export function validateURL(textval) {  const urlregex = /^(?:http(s)?:\/\/)?[\w.-]+(?:\ ...

  10. JavaScript学习笔记之二

    一 js与json数据格式的转换:序列号与反序列化 JSON.stringify(jsobj, '  ');//将js的obj转换为json对象: JSON.parse()把json对象变成一个Jav ...