1004 Counting Leaves (30分)
今天在热心网友的督促下完成了第一道PAT编程题。
太久没有保持训练了,整个人都很懵。
解题方法:
1.读懂题意
2.分析重点
3.确定算法
4.代码实现
该题需要计算每层的叶子节点个数,所以选用BFS
还有一个关键问题是 如何记录一层的开始和结束
另外,对于新手来说,图的存储也是一个知识点
容易忽略特殊取值情况下的答案:
当非叶节点个数为0,只有根节点一个点,所以直接输出1
而其他情况下,第一层叶子节点数为0
```
/**/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
typedef long long LL;
typedef unsigned long long ULL;
using namespace std;
bool Sqrt(LL n) { return (LL)sqrt(n) * sqrt(n) == n; }
const double PI = acos(-1.0), ESP = 1e-10;
const LL INF = 99999999999999;
const int inf = 999999999, maxN = 100 + 24;
int N, M;
int ans[maxN], d[maxN];
vector< vector > E(maxN);
int main()
{
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
scanf("%d%d", &N, &M);
for(int i = 0; i < M; i++) {
int u, v, k; scanf("%d%d", &u, &k);
d[u] = k;
for(int j = 0; j < k; j++) {
scanf("%d", &v);
E[u].push_back(v);
}
}
memset(ans, 0, sizeof ans);
queue Q, W;
Q.push(1);
if(!M) { printf("1"); return 0; }
printf("0");
int h = 1;
while(!Q.empty()) {
int x = Q.front(), e = d[x], count = 0;
Q.pop();
for(auto u : E[x]) {
if(d[u] > 0) { W.push(u); count++; }
}
ans[h] += e - count;
if(Q.empty()) {
Q = W;
while(W.size()) W.pop();
h++;
}
}
for(int i = 1; i < h; i++) printf(" %d", ans[i]);
return 0;
}
/*
input:
output:
modeling:
methods:
complexity:
summary:
*/
1004 Counting Leaves (30分)的更多相关文章
- PAT 1004 Counting Leaves (30分)
1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is t ...
- 1004 Counting Leaves (30分) DFS
1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- 【PAT甲级】1004 Counting Leaves (30 分)(BFS)
题意:给出一棵树的点数N,输入M行,每行输入父亲节点An,儿子个数n,和a1,a2,...,an(儿子结点编号),从根节点层级向下依次输出当前层级叶子结点个数,用空格隔开.(0<N<100 ...
- 1004 Counting Leaves (30 分)
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family member ...
- 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 ...
- 1004. Counting Leaves (30)
1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- PAT 解题报告 1004. Counting Leaves (30)
1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is t ...
- PAT 1004. Counting Leaves (30)
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family membe ...
- PAT A 1004. Counting Leaves (30)【vector+dfs】
题目链接:https://www.patest.cn/contests/pat-a-practise/1004 大意:输出按层次输出每层无孩子结点的个数 思路:vector存储结点,dfs遍历 #in ...
- 【PAT Advanced Level】1004. Counting Leaves (30)
利用广度优先搜索,找出每层的叶子节点的个数. #include <iostream> #include <vector> #include <queue> #inc ...
随机推荐
- 从create-react-app 学点东西1:web-vitals
导言 市场中流行的框架有很多地方是值得我们深入的去探究或学习的,<从create-react-app学点东西>这系列文章从create-react-app创建的项目中找出一些重要或者容易忽 ...
- WebAPI :Get、Post、Delete、Put
public class RestClientMethod { /// <summary> /// 请求服务器地址 /// </summary> public string B ...
- 【C学习笔记】【分享】day2-2 不允许创建临时变量,交换两个数的内容(附加题)
加法实现: #include <stdio.h> int main() { int a = 30; int b = 20; a = a + b; b = a - b; a = a - b; ...
- Redis缓存问题排查
一.缓存穿透 概念:缓存穿透是指查询的数据不存在,redis和mysql(或其他持久存储的数据库)都不能命中.工作中出于容错的考虑,如果从数据库内不能查到数据则不会写入缓存,缓存穿透将导致不存在的数据 ...
- HTML clean input cache
.html <input type="search" autocomplete ="off" />
- Unity生成AB包和加载AB包
unity生成AB包 生产AB包,编辑器脚本放在Editor文件夹下(切记) 如果你是PC包 BuildTarget.WebGL 后面要改成PC (BuildTarget.StandaloneWin ...
- BASE64编码作业
BASE64编码作业 什么是BASE64编码 ase64是网络上最常见的用于传输8Bit字节码的编码方式之一,Base64就是一种基于64个可打印字符来表示二进制数据的方法.可查看RFC2045-RF ...
- Qt实现collapsePanel(折叠)功能
实践过程中,看到C#实现的CollapsePanel功能,比一般的TabWidget更加直观,充分利用了页面空间,遂感到很有兴趣,也查阅了很多资料搜索Qt在这方面的实现. 目前来说,比较常见的作法就是 ...
- DCS-WORLD 数据获取
任务: 获取DCS-World的姿态数据,发送到6自由度平台. 过程: 1.获取dcs-bios https://github.com/DCSFlightpanels/dcs-bios 2.复制DCS ...
- window.location.href 下载文件页面出现跳转 (重定向失效的问题)
页面出现跳转的话,要去检查请求的接口是不是出现了问题. 解决方法:URL前面加 "/" ${process.env.VUE_APP_BASE_API} / ${url}