今天在热心网友的督促下完成了第一道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. fatal error: openssl/ssl.h: No such file or director

    $ sudo apt-get install libssl-dev

  2. redis常用命令之Hash

    redis常用命令之Hash Hash常用命令 redis 可以理解为一个全局的大字典,key就是数据的唯一标识符.对应的key不同,value也不同.redis有5个基本的数据类型. 1 redis ...

  3. PHP开启缓存加速

     PHP默认会将Operate Code文件丢弃,缓存加速是将其保存下来,放置共享内存中,以便在下次调用该PHP页面时重用,避免相同代码的重复编译 __________________________ ...

  4. HTML-background-image

    background-image:用于设置需要显示背景图片: 例如-->background-image:url(demo.jpg); 大家知道ulr()中是填写路径:(现在假设背景图片名字叫做 ...

  5. 结构体struct

    在 C# 中,结构体是值类型数据结构.它使得一个单一变量可以存储各种数据类型的相关数据.struct 关键字用于创建结构体. 结构体是用来代表一个记录.假设您想跟踪图书馆中书的动态.您可能想跟踪每本书 ...

  6. java字符串和图片相互转换

    package com.thinkgem.jeesite.modules.api.wechat; import sun.misc.BASE64Decoder; import sun.misc.BASE ...

  7. Java流程控制之while循环详解

    while循环 while循环 do...while循环 for循环 在Java5中引入了一种主要用于数组的增强型for循环 while循环 while循环是最基本的循环,它的结构为 while(布尔 ...

  8. C# 读取Xml文件中的中文

    这是.xml中的内容 <?xml version="1.0" encoding="utf-8"?> <resources> <!- ...

  9. Java8:LocalDate/ LocalDateTime与String、Date、TimeStamp的互转

    LocalDate与String.Date.TimeStamp的互转: LocalDateTime与String.Date.TimeStamp的互转: 结果如下: 附代码: public static ...

  10. linux火狐添加flash插件

    从官网上 adobe flash player 全版本通用安装  找到 linux 版本的 .gz 包, 复制下载链接 linux命令 su - cd /tmp wget -P /tmp https: ...