利用广度优先搜索,找出每层的叶子节点的个数。

#include <iostream>
#include <vector>
#include <queue>
#include <fstream>
using namespace std; vector<vector<int>> tree;
vector<int> ans; void BFS(int s)
{
queue<pair<int, int>> q;
q.push(make_pair(s, 0));
int cur_step = 0;
int cnt = 0; while (!q.empty())
{
int id = q.front().first;
int step = q.front().second;
q.pop();
if(step > cur_step)
{
cur_step = step;
ans.push_back(cnt);
cnt = 0;
}
if(tree[id].size() == 0) cnt++;
for(int i = 0; i < tree[id].size(); i++)
q.push(make_pair(tree[id][i], step + 1));
}
ans.push_back(cnt);
return;
} int main()
{
//fstream cin("a.txt"); int N, M;
cin>>N>>M;
tree.resize(N + 1);
for(int i = 0; i < M; i++)
{
int id, num;
cin>>id>>num;
while (num--)
{
int tmp;
cin>>tmp;
tree[id].push_back(tmp);
}
}
BFS(1);
for(int i = 0; i < ans.size(); i++)
{
if(i == 0)
cout<<ans[i];
else
cout<<" "<<ans[i];
}
cout<<endl;
}

【PAT Advanced Level】1004. Counting Leaves (30)的更多相关文章

  1. 【PAT甲级】1004 Counting Leaves (30 分)(BFS)

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

  2. 【PAT Advanced Level】1008. Elevator (20)

    没什么难的,简单模拟题 #include <iostream> using namespace std; int main() { int num; cin>>num; int ...

  3. 【PAT Advanced Level】1006. Sign In and Sign Out (25)

    关键在于清空字符数组和使用scanf进行输入 #include <stdio.h> #include <string.h> #include <fstream> # ...

  4. 【PAT Advanced Level】1014. Waiting in Line (30)

    简单模拟题,注意读懂题意就行 #include <iostream> #include <queue> using namespace std; #define CUSTOME ...

  5. 【PAT Advanced Level】1015. Reversible Primes (20)

    转换进制&&逆序可以在一起进行,有一点技巧,不要用十进制数来表示低进制,容易溢出. #include <iostream> #include <vector> ...

  6. 【PAT Advanced Level】1011. World Cup Betting (20)

    简单模拟题,遍历一遍即可.考察输入输出. #include <iostream> #include <string> #include <stdio.h> #inc ...

  7. 【PAT Advanced Level】1013. Battle Over Cities (25)

    这题给定了一个图,我用DFS的思想,来求出在图中去掉某个点后还剩几个相互独立的区域(连通子图). 在DFS中,每遇到一个未访问的点,则对他进行深搜,把它能访问到的所有点标记为已访问.一共进行了多少次这 ...

  8. PAT甲题题解-1004. Counting Leaves (30)-统计每层叶子节点个数+dfs

    统计每层的叶子节点个数建树,然后dfs即可 #include <iostream> #include <cstdio> #include <algorithm> # ...

  9. PAT 解题报告 1004. Counting Leaves (30)

    1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is t ...

随机推荐

  1. PHP基础语法2

    数组 PHP有两种数组:索引数组.关联数组. 函数 自定义函数 自定义函数通过将一组代码封装起来,使代码进行复用,程序结构与逻辑更加清晰 返回值 使用return关键字可以使函数返回值,可以返回包括数 ...

  2. dynamic_cast 用法

    dynamic_cast < type-id > ( expression ) 该运算符把expression转换成type-id类型的对象.Type-id必须是类的指针.类的引用或者vo ...

  3. 221. Maximal Square

    题目: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ...

  4. 安装Hadoop系列 — eclipse plugin插件编译安装配置

    [一].环境参数 eclipse-java-kepler-SR2-linux-gtk-x86_64.tar.gz //现在改为eclipse-jee-kepler-SR2-linux-gtk-x86_ ...

  5. No bean named 'transactionManager' is defined

    2016-10-20 23:27:17.771 INFO 7096 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped &quo ...

  6. R语言学习笔记:向量化

    R语言最强大的方面之一就是函数的向量化,这些函数可以直接对向量的每个元素进行操作.例如: 对每个元素进行开方 > v<-c(4,3,8,16,7.3) > v [1]  4.0  3 ...

  7. node.js模块之fs文件系统

    fs 模块是文件操作的封装,它提供了文件的读取.写入.更名.删除.遍历目录.链接等 POSIX 文件系统操作.与其他模块不同的是,fs 模块中所有的操作都提供了异步的和同步的两个版本, 例如读取文件内 ...

  8. uva 993 Product of digits (贪心 + 分解因子)

      Product of digits  For a given non-negative integer number N , find the minimal natural Q such tha ...

  9. DIV内英文或者数字不换行的问题 解决办法

    word-wrap:break-word; word-break:break-all;

  10. Android 开发之 ---- 底层驱动开发(一)

    驱动概述 说到 android 驱动是离不开 Linux 驱动的.Android 内核采用的是 Linux2.6 内核 (最近Linux 3.3 已经包含了一些 Android 代码).但 Andro ...