根据家谱树从根结点开始输出每一层的叶子结点数量。使用BFS来解决。因为不会重复访问结点,所以不需要vis数组来标记是否访问过该结点。

 //#include "stdafx.h"
#include <iostream>
#include <vector>
#include <queue> using namespace std; vector<int> node[]; // Storing children's dynamic arrays
queue<int> que; // que for bfs
bool first = true; // first output flag void bfs() { // traverse by layer
int queSize = que.size(), cur, nodeSize, count = , i;
while (queSize--) { // traverse all nodes at the current level
cur = que.front();
que.pop(); nodeSize = node[cur].size();
if (nodeSize == ) { // if it is a leaf node
count++;
} else {
for (i = ; i < nodeSize; i++) { // if not, add child node
que.push(node[cur][i]);
}
}
} // output
if (first) {
first = false;
} else {
printf(" ");
}
printf("%d", count); if (que.size() > ) { // if que has elements, traverse
bfs();
}
} int main() {
int n, m;
scanf("%d%d", &n, &m); int i, id, k, child, j;
for (i = ; i < m; i++) {
scanf("%d%d", &id, &k); for (j = ; j <= k; j++) {
scanf("%d", &child);
node[id].push_back(child);
}
} que.push(); // add root node
bfs();
printf("\n"); system("pause");
return ;
}

PAT-A1004. 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. PAT A1004 Counting Leaves (30 分)——树,DFS,BFS

    A family hierarchy is usually presented by a pedigree tree. Your job is to count those family member ...

  3. PAT 1004. Counting Leaves (30)

    A family hierarchy is usually presented by a pedigree tree.  Your job is to count those family membe ...

  4. PAT 解题报告 1004. Counting Leaves (30)

    1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is t ...

  5. 1004. Counting Leaves (30)

    1004. Counting Leaves (30)   A family hierarchy is usually presented by a pedigree tree. Your job is ...

  6. 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 ...

  7. pat1004. Counting Leaves (30)

    1004. Counting Leaves (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A fam ...

  8. 1004 Counting Leaves (30分) DFS

    1004 Counting Leaves (30分)   A family hierarchy is usually presented by a pedigree tree. Your job is ...

  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. 封装cuda/cudnn写卷积网络前向计算程序

    目录 基本编译配置 一些常识 BN层的坑 cuda基础 向cuda核函数传入结构体指针? 参考:http://galoisplusplus.coding.me/blog/2018/05/22/cuda ...

  2. asp.net core 中间件应用

    中间件是一种装配到应用管道以处理请求和响应的软件. 每个组件: 选择是否将请求传递到管道中的下一个组件. 可在调用管道中的下一个组件前后执行工作. 请求委托(Request delegates)用于生 ...

  3. win10 64 使用 visual studio 2017 搭建汇编开发环境

    转自http://blog.csdn.net/sinat_27382047/article/details/70339455 插件 vs2015的汇编语法高亮插件(安装就行)这玩意找了我很久= = h ...

  4. babelrc

    .babelrc文件 // 简单版 { "presets": ["es2015", "stage-2"], // 使用 es2015 npm ...

  5. ubuntud安装Adobe Flash Player / Plugin

    1.https://get.adobe.com/flashplayer/ , select tar.gz for other Linux, download 2.Unpack the tar.gz f ...

  6. PrintDocument打印、预览、打印机设置和打印属性的方法

    WindowsForm 使用 PrintDocument打印.预览.打印机设置和打印属性的方法. private void Form1_Load(object sender, System.Event ...

  7. C# 之 HttpRequest 类

          Request对象派生自HttpRequest类,使 ASP.NET 能够读取客户端在 Web 请求期间发送的 HTTP 值,从客户端获取信息,浏览器的种类,用户输入表单的数据,Cooki ...

  8. 【技巧汇总】eclipse中如何跳转到指定行

    技巧汇总 持续更新ing eclipse中如何跳转到指定行 ctrl+L

  9. Linux 记录所有用户登录和操作的详细日志

    1.起因 最近Linux服务器上一些文件呗篡改,想追查已经查不到记录了,所以得想个办法记录下所有用户的操作记录. 一般大家通常会采用history来记录,但是history有个缺陷就是默认是1000行 ...

  10. 牛客 Wannafly 挑战赛26D 禁书目录 排列组合 概率期望

    原文链接https://www.cnblogs.com/zhouzhendong/p/9781060.html 题目传送门 - NowCoder Wannafly 26D 题意 放一放这一题原先的题面 ...