今天在热心网友的督促下完成了第一道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. Python用telnet设置,抓UDP抓采样点并显示

    ====main.bat==== echo off rem "d:\Program\WiresharkPortable64\App\Wireshark\tshark.exe" -- ...

  2. Docker的常见使用

    一.Docker的常见使用 1.docker的使用 1.1 查看docker版本号信息 docker version docker info 1.2 启动docker systemctl start ...

  3. 当前工程中typescritpt依赖包与依赖包中依赖包类型不一致如何解决

    在开发中,遇到文件中引入webpack,但是webpack.ICompiler不一致的情况 //import webpack from 'webpack'; import webpackHot fro ...

  4. unity笔记001

    熟悉场景和物体的基本操作,q移动场景,e旋转物体,放大缩小,V键顶点吸附,相比快捷键,还是喜欢用xyz参数控制

  5. linux 安装 vsftpd ftp 服务端

    安装 yum install vsftpd 修改:/etc/vsftpd/vsftpd.conf anonymous_enable=NO local_enable=YES write_enable=Y ...

  6. 渲染杂谈:early-z、z-culling、hi-z、z-perpass到底是什么?

    之前一直被这几个和深度缓存(z-buffer)相关的概念搞得神魂颠倒.今天在翻阅<Real-Time Rendering>时碰巧碰巧看到了这部分的讲解.硬着头皮看了看,姑且算是讲几个概念分 ...

  7. 85、使用Vue.js实现列表选中效果

      实际项目中,我们会遇到很多类似的需求,一个列表,需要点击其中一条高亮显示.熟悉JQuery的同学说这个太简单了.可以给这个选中的element设置一个active的class.配合Css样式,让a ...

  8. Django 知识点总结

    知识点总结 一.URL: 1.在python 正则表达式中,正则表达式命名组的语法是(?P<name>pattern),其中命名组中的命名就是name,并且pattern 是某些匹配的模式 ...

  9. Python打包时包含静态文件处理方法

    Python打包时包含静态文件处理方法 使用场景 已搭建了PyPI私有库,上传公共库包含静态文件,如需要使用sql静态文件初始化数据库. 打包python包,给其他人使用,但项目中包含静态文件,如ht ...

  10. TCP三次握手和四次挥手的原因所在

    报文从运用层传送到运输层,运输层通过TCP三次握手和服务器建立连接,四次挥手释放连接. 为什么需要三次握手呢?为了防止已失效的连接请求报文段突然又传送到了服务端,因而产生错误. 比如:client发出 ...