1004 Counting Leaves (30分)
今天在热心网友的督促下完成了第一道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分)的更多相关文章
- PAT 1004 Counting Leaves (30分)
1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is t ...
- 1004 Counting Leaves (30分) DFS
1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- 【PAT甲级】1004 Counting Leaves (30 分)(BFS)
题意:给出一棵树的点数N,输入M行,每行输入父亲节点An,儿子个数n,和a1,a2,...,an(儿子结点编号),从根节点层级向下依次输出当前层级叶子结点个数,用空格隔开.(0<N<100 ...
- 1004 Counting Leaves (30 分)
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family member ...
- 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 ...
- 1004. Counting Leaves (30)
1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- PAT 解题报告 1004. Counting Leaves (30)
1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is t ...
- PAT 1004. Counting Leaves (30)
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family membe ...
- PAT A 1004. Counting Leaves (30)【vector+dfs】
题目链接:https://www.patest.cn/contests/pat-a-practise/1004 大意:输出按层次输出每层无孩子结点的个数 思路:vector存储结点,dfs遍历 #in ...
- 【PAT Advanced Level】1004. Counting Leaves (30)
利用广度优先搜索,找出每层的叶子节点的个数. #include <iostream> #include <vector> #include <queue> #inc ...
随机推荐
- Anaconda配置环境变量
环境变量里写自己的Anaconda下载目录,找到对应的文件位置,然后把这三个上移到最上面,点击确定即可配置完成 出现这个环境变量就成功啦 在输入conda 出现这个就表明你的Anaconda已经好了
- java 实现读取本地日志文件列表并在浏览器上显示
import org.springframework.core.io.Resource; import org.springframework.core.io.UrlResource; import ...
- struts2 显示表格
<%@ taglib uri="/struts-tags" prefix="s"%> <h3>All Records:</h3&g ...
- HDLbits——Shift18
// Build a 64-bit arithmetic shift register, // with synchronous load. The shifter can shift both le ...
- Retrofit简要分析
Retrofit是对网络请求实现了一套请求架构封装,屏蔽底层网络实现,使网络请求像调用本地接口一样 基本使用例子 public interface GitHubService {//定义request ...
- 94、springboot+minio实现分片上传(超大文件快速上传)
设计由来 在实际的项目开发中常遇到超大附件上传的情况,有时候客户会上传GB大小的文件,如果按照普通的MultipartFile方式来接收上传的文件,那么无疑会把服务器给干崩溃,更别说并发操作了.于是笔 ...
- Onur Mutlu 18-447 Lecture9 分支预测-1
=============== 第一部分:branch prediction =========== 1. 最简单的分支预测:总是预测下一条指令的地址在 PC+4 如何让这种分支预测更加有效呢? Id ...
- 041_Record Type
应用场景:在使用Apex新建记录时,有时需要根据具体需求为用户匹配对应的记录类型,这个时候就需要用到记录类型Id的情况. 获取RecordTypeId的方法有2种,一种是根据soql进行查询,另一种是 ...
- CSS3-transform缩放
缩放:transform:scale(倍数); 实现hover的时候加载播放图标,具体效果如下: 首先需要创建一个大盒子,盒子上面部分用一个div来呈放图片,下面部分直接书写文字.观察发现播放图标是存 ...
- java 图片BASE64 转字节流 byte[]
String base64Url = base64UrlArray.get(0)+"";BASE64Decoder decoder = new BASE64Decoder();re ...