【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 ...
随机推荐
- 【mysql的设计与优化专题(2)】数据中设计中的范式与反范式
设计关系数据库时,遵从不同的规范要求,设计出合理的关系型数据库,这些不同的规范要求被称为不同的范式,各种范式呈递次规范,越高的范式数据库冗余越小.但是有些时候一昧的追求范式减少冗余,反而会降低数据读写 ...
- laravel Authentication and Security
Creating the user modelFirst of all, we need to define the model that is going to be used to represe ...
- JavaScript DOM高级程序设计 5动态修改样式和层叠样式表1(源代码)--我要坚持到底!
W3C DOM2样式规范 现在这边贴出本章要的源代码,注意要结合前面用到的ADS库http://vdisk.weibo.com/s/Dq8NU CSSStyleSheet对象属性: type :始终是 ...
- Weak Event Patterns
https://msdn.microsoft.com/en-US/library/aa970850(v=vs.100).aspx In applications, it is possible tha ...
- C语言中指针数组和数组指针的区别
指针数组:首先它是一个数组,数组的元素都是指针,数组占多少个字节由数组本身决定.它是“储存指针的数组”的简称. 数组指针:首先它是一个指针,它指向一个数组.在32 位系统下永远是占4 个字节,至于它指 ...
- naotu.baidu.com 非常棒的脑图在线工具
1.png 2.txt 短租 前台功能 房源查看 房源搜索 城市房源 注册登录 预定房源 房源退订 在线支付 评价房源 个人中心 我的订单 我的账户 我的收藏 消息通知 管理员后台 房源发布 会员管理 ...
- innodb 悲观锁,乐观锁
转 http://www.cnblogs.com/chenwenbiao/archive/2012/06/06/2537508.html CREATE TABLE `products` ( `id` ...
- windows8安装xna4.0不能开发Xbox和PC端游戏的解决办法
vs2012安装wp8后,只能开发手机端的xna游戏程序,没有xbox和pc端的,看来官方是不打算更新了,不过我们还是有办法的. 前提条件下,您得安装了vs2010和xna4.0 game studi ...
- 在线API,桌面版,jquery,css,Android中文开发文档,JScript,SQL掌用实例
学习帮助文档大全 jquery,css,Android中文开发文档,JScript,SQL掌用实例 http://api.jq-school.com/
- [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.2.9
(1). When $A$ is normal, the set $W(A)$ is the convex hull of the eigenvalues of $A$. For nonnormal ...