【PAT Advanced Level】1004. Counting Leaves (30)
利用广度优先搜索,找出每层的叶子节点的个数。
#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)的更多相关文章
- 【PAT甲级】1004 Counting Leaves (30 分)(BFS)
题意:给出一棵树的点数N,输入M行,每行输入父亲节点An,儿子个数n,和a1,a2,...,an(儿子结点编号),从根节点层级向下依次输出当前层级叶子结点个数,用空格隔开.(0<N<100 ...
- 【PAT Advanced Level】1008. Elevator (20)
没什么难的,简单模拟题 #include <iostream> using namespace std; int main() { int num; cin>>num; int ...
- 【PAT Advanced Level】1006. Sign In and Sign Out (25)
关键在于清空字符数组和使用scanf进行输入 #include <stdio.h> #include <string.h> #include <fstream> # ...
- 【PAT Advanced Level】1014. Waiting in Line (30)
简单模拟题,注意读懂题意就行 #include <iostream> #include <queue> using namespace std; #define CUSTOME ...
- 【PAT Advanced Level】1015. Reversible Primes (20)
转换进制&&逆序可以在一起进行,有一点技巧,不要用十进制数来表示低进制,容易溢出. #include <iostream> #include <vector> ...
- 【PAT Advanced Level】1011. World Cup Betting (20)
简单模拟题,遍历一遍即可.考察输入输出. #include <iostream> #include <string> #include <stdio.h> #inc ...
- 【PAT Advanced Level】1013. Battle Over Cities (25)
这题给定了一个图,我用DFS的思想,来求出在图中去掉某个点后还剩几个相互独立的区域(连通子图). 在DFS中,每遇到一个未访问的点,则对他进行深搜,把它能访问到的所有点标记为已访问.一共进行了多少次这 ...
- PAT甲题题解-1004. Counting Leaves (30)-统计每层叶子节点个数+dfs
统计每层的叶子节点个数建树,然后dfs即可 #include <iostream> #include <cstdio> #include <algorithm> # ...
- PAT 解题报告 1004. Counting Leaves (30)
1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is t ...
随机推荐
- ENVI Services Engine5.1 应用开发入门教程
原文地址: ENVI Services Engine5.1 应用开发入门教程_ENVI-IDL中国_新浪博客 http://blog.sina.com.cn/s/blog_764b1e9d0102uy ...
- std::remove
#include <algorithm> template< class ForwardIt, class T > ForwardIt remove( ForwardIt fi ...
- 186. Reverse Words in a String II
题目: Given an input string, reverse the string word by word. A word is defined as a sequence of non-s ...
- P140、面试题24:二叉搜索树的后序遍历序列
题目:输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则返回true,否则返回false.假设输入的数组的任意两个数字都互不相同. 测试用例: 1)功能测试(输入的后序遍历的序列 ...
- Linux下的动态连接库及其实现机制
Linux与Windows的动态连接库概念相似,但是实现机制不同.它引入了GOT表和PLT表的概念,综合使用了多种重定位项,实现了"浮动代码",达到了更好的共享性能.本文对这些技术 ...
- PHP判断日期是不是今天 判断日期是否为当天
<?php /** * PHP判断一个日期是不是今天 * 琼台博客 */ echo '<meta charset="utf-8" />'; // 拟设一个日期 $ ...
- Android开发之内容观察者
内容观察者: 当关注应用的数据库数据改变时,内容提供者会发出通知,在内容提供者的uri上注册一个内容观察者,就可以收到数据改变的通知 实现步骤: 1.假如是自定义的ContentProvider,需要 ...
- Android开发之MediaPlayer类
官网关于MediaPlayer类的使用简介:
- short s1 = 1; s1 = s1 + 1;有错而short s1 = 1; s1 += 1正确
这个问题以前碰到过,也研究过,发表一下: 如果你认为表达式(x += i)只是表达式(x = x + i)的简写方式,这并不准确.这两个表达式都被称为赋值表达式.第二个表达式使用的是简单赋值操作 ...
- 数论/the second wave
扩展欧几里得算法. void exgcd(int a,int b,int&x,int&y){ if(!b) { x=1;y=0;return ; } exgcd(b,a%b,x,y); ...