今天在热心网友的督促下完成了第一道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. SAP Process Orchestration (SAP PO): The Comprehensive Guide (2nd Edition) (SAP PRESS)

    SAP Process Orchestration (SAP PO): The Comprehensive Guide (2nd Edition) (SAP PRESS) 有需要的联系 wx :erp ...

  2. c语言学习--静态函数

    静态函数 #include<stdio.h> //这是静态函数, 静态函数只能在当前文件调用,其他文件下面的函数是没法调用到这个函数的 static void fun1() { print ...

  3. 织梦dedecms网站迁移搬家图文教程

    织梦dedecms网站迁移搬家图文教程 2014-07-31  dedecms教程  文章介绍 织梦作为国内使用最多的程序之一,难免很多新手在接触dede时不知道怎么转移也就是搬家dede的程序,而且 ...

  4. go读取excel的内容

    import "github.com/360EntSecGroup-Skylar/excelize" func SimulationDataHandler(){ f, err := ...

  5. Oracle dump文件的一些经验

    dump文件对于DBA而言再平常不过了.不过因为dump文件是二进制文件,我们平时使用中不太关注.再导入dump文件时有很多细节和技巧是值得注意的. 1.查看dump文件的一些基本信息 strings ...

  6. Google Chrome 离线安装包官方下载

    我们参照 Google Chrome帮助中的<下载和安装 Google Chrome>,在这里找到官方最新版离线安装包的下载地址:备用 Chrome 安装程序,历史版本请通过版本号的方式下 ...

  7. nginx 配置react项目 并且开启gzip压缩

    #user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #erro ...

  8. vite 路径别名 @ 配置

    vite.config.ts resolve.alias 配置 const path = require('path'); import { defineConfig } from 'vite'; i ...

  9. Windows 进程间通信 共享内存

    向内存中写数据 1 // SharedMemorySample_write_main.cpp 2 #include <SDKDDKVer.h> 3 #include <Windows ...

  10. ssh反向通信

    ##先决条件为:一个有公网IP的VPS(虚拟主机),我使用的是国内的腾讯云,您也可以选择阿里云,亚马逊等各种厂商产品.这台机器的操作系统为 centos 7.0 ,IP 为 A.A.A.A #双内网主 ...