今天在热心网友的督促下完成了第一道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. win10系统下mysql安装

    1.官网下载压缩包 2.添加环境变量到PATH 3.在bin的同级目录下,新增my.ini,内容如下(严重注意,是斜杠,千万别写成反斜杠!!!): [mysqld] basedir = D://sof ...

  2. HTML弹窗设计二

    <!DOCTYPE html><html> <head> <title>模态框弹出层.html</title> <meta http- ...

  3. 实现分页数据请求的思路/Element UI(Plus)的分页模板(Vue3.x写法),(直接使用<script>引入vue.js)

    实现分页数据请求的思路/Element UI(Plus)的分页模板(Vue3.x写法),(直接使用<script>引入vue.js) 1. 效果图: 2.实现分页数据请求的思路:   分页 ...

  4. Java基础之包机制+JavaDoc

    包机制 为了更好地组织类,Java提供了包机制,用于区别类名的命名空间.[包的本质就是文件夹] 包语句的语法格式为: package pkg1[. pkg2[. pkg3...]]; 一般利用公司域名 ...

  5. Mysterious-GIF --- 攻防世界WP

    题目描述: 附件: ps:我重命名了一下为 cindy,gif 解题过程 1.分析该GIF (1)查看图片属性 (2)strings命令查找字符串 (3)winhex查看 (4)因为是GIF,所以可使 ...

  6. lui - pager - 分页

    pager - 分页 demo lui demo <template> <div class="app-container"> <h3>el-p ...

  7. Date 日期字符串自定义格式化

    Date.prototype.Format = function (fmt) { var o = { "M+": this.getMonth() + 1, //月份 "d ...

  8. 049_Search Lookup (二)

    其实就是 在父object中 设置,search setting 中选中 enhanced lookup, and select the dialoge & Filter  默认looukp搜 ...

  9. HCIA-ICT实战基础07-访问控制列表ACL进阶

    HCIA-ICT实战基础-访问控制列表ACL进阶 目录 二层ACL技术及配置 高级ACL的扩展使用方法及使用场景 1 二层ACL技术及配置 1.1 二层ACL概念 使用报文的以太网帧头来定义规则, 根 ...

  10. sudo: port: command not found 报错解决方案

    mac本安装graphviz的方法是: sudo port install graphviz 执行报错: sudo: port: command not found 其实是未设置环境变量,执行下面的语 ...