今天在热心网友的督促下完成了第一道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分)的更多相关文章

  1. PAT 1004 Counting Leaves (30分)

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

  2. 1004 Counting Leaves (30分) DFS

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

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

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

  4. 1004 Counting Leaves (30 分)

    A family hierarchy is usually presented by a pedigree tree. Your job is to count those family member ...

  5. 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 ...

  6. 1004. Counting Leaves (30)

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

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

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

  8. PAT 1004. Counting Leaves (30)

    A family hierarchy is usually presented by a pedigree tree.  Your job is to count those family membe ...

  9. PAT A 1004. Counting Leaves (30)【vector+dfs】

    题目链接:https://www.patest.cn/contests/pat-a-practise/1004 大意:输出按层次输出每层无孩子结点的个数 思路:vector存储结点,dfs遍历 #in ...

  10. 【PAT Advanced Level】1004. Counting Leaves (30)

    利用广度优先搜索,找出每层的叶子节点的个数. #include <iostream> #include <vector> #include <queue> #inc ...

随机推荐

  1. abap sql中进行除法操作

    在abap 得sql中进行除法操作要用division,不能用 " / "这个符号 SELECT vbeln, 100 * CAST( 10 + DIVISION( ZMENG, ...

  2. SpringBoot配置阿里云https提示端口占用问题

    1.因为要配置https,所以去网上找了一些资料,然后按照步骤,依次完成了以下步骤 ①在application.yml中加入配置 http: port: 12000 #原本的端口号server: po ...

  3. 记D365开发的最佳实践

    前端JS 不同的需求应该划分模块,以便日后修改,也是为了职责分离,模块分离,日后如果想分离到单独的JS文件里面也是比较方便: 对于公共的查询函数,应该做缓存,优先使用sessionStorage. 多 ...

  4. 解决idea不能自动下载maven配置文件pom.xml下的jar包依赖的问题

    表现:无法下载pom配置文件中的依赖包,或只能下载少数包,各项配置都正确的情况 理由未知: 百度了很长一段时间,网上给出比较精准的解决之一是 setting>>maven>>去 ...

  5. Web Dynpro for ABAP(15):Print

    3.20 Print WDA调用浏览器打印界面 1.创建Print按钮,绑定事件PRINT; 2.实现ONACTIONPRINT事件: method ONACTIONPRINT. DATA:l_api ...

  6. 使用react脚手架创建项目报错-You are running `create-react-app` 5.0.0, which is behind the latest release (5.0.1).

    创建项目报错: You are running `create-react-app` 5.0.0, which is behind the latest release (5.0.1). We no ...

  7. linux下nginx的(启动、重启、关闭)

    1. 首先利用配置文件启动nginx. 命令: nginx -c /usr/local/nginx/conf/nginx.conf 重启服务: service nginx restart 2. 快速停 ...

  8. memoのls

    memoのls 测试环境是Big Sur 11.2.2 在windows下都不知道cd /D E:\xxx\xxx可以直接切换盘符.今天才发现,ls命令我也不会用-- ls命令是真强大啊,之前只知道l ...

  9. 5G智慧灯杆系统在智慧街区的应用

    智慧化的路灯作为一个高度集成的项目,是智慧城市在城市公共空间的落地载体,是一个自上而下的体系,有外延.可扩展.能适配智慧城市的建设要求.在商业街开展智慧灯杆建设,同期开展5G应用技术试点,有利于商业街 ...

  10. 什么是spring框架

    一bai.概念:1. spring是开源的轻量级框架2 spring核心主要两部分:(1)aop:面向切面编程,扩展功能不是修改源代码实现(2)ioc:控制反转,- 比如有一个类,在类里面有方法(不是 ...