【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 ...
随机推荐
- 网上图书商城项目学习笔记-014购物车模块页面javascrip
一.流程分析 二.代码 1.view层 (1)list.jsp <%@ page language="java" import="java.util.*" ...
- Android:通过Intent访问一个网页
Intent(意图)主要是解决Android应用的各项组件之间的通讯. 小实例 package com.example.testopen; import android.app.Activity; i ...
- Python之函数篇
函数形参 函数取得的参数是你提供给函数的值,这样函数就可以利用这些值 做 一些事情.这些参数就像变量一样,只不过它们的值是在我们调用函数的时候定义的,而非在函数本身内赋值. 参数在函数定义的圆括号对内 ...
- JS中访问对象的属性
方式一: 对象名.属性名; 方式二: 对象名["属性名"]; ★注意:方式二中,属性名以字符串的形式出现在方括号中,这意味着通过方式二访问属性的话,可以实现“动态访问对象的 ...
- [译]Atomic VS. Non-Atomic 操作
原文链接:atomic-vs-non-atomic-operations 在网上已经写了很多关于原子操作的文章,但是通常都集中在原子的读-修改-写(RMW. read-modify-write)操作. ...
- nagios&pnp4nagios--yum 安装
一. 环境: 1. centos 6.4 2. 设置hostname 并且安装好apache 3. 关闭selinux及iptables 二. 安装nagios服务器端: 1. rpm -Uvh ht ...
- git源码中的Makefile
https://github.com/chucklu/GitStudy 这链接里面的第一次提交 [chucklu@localhost GitStudy]$ cat Makefile CFLAGS= ...
- ubuntu终端颜色配置
对于刚接触ubuntu的同学们,打开终端(ctrl+alt+T),会发现里面都是一个颜色,不管是用户名.主机名还是命令都是白色,当然,用 ls 列出文件的时候是会多一种颜色的.即使这样,对开发人员来说 ...
- JS 动态显示 获取下拉框的多个值
<script type="text/javascript"> function GetProcessVal(i, t) { document.getElementsB ...
- java jvm学习笔记一
欢迎装载请说明出处:http://blog.csdn.net/yfqnihao java的class只在需要的时候才内转载入内存,并由java虚拟机的执行引擎来执行,而执行引擎从总的来说主要的执行方式 ...