根据家谱树从根结点开始输出每一层的叶子结点数量。使用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. 捷信达会员管理系统SQL语句相关

    捷信达会员管理系统SQL语句相关 USE gshis GO SET ANSI_NULLS, QUOTED_IDENTIFIER ON GO /***************************** ...

  2. asp.net core WebApi 快速入门

    参考:https://docs.microsoft.com/zh-cn/aspnet/core/tutorials/first-web-api?view=aspnetcore-2.1 官网的例子 直接 ...

  3. vi/vim键盘对应图

    来源:http://www.runoob.com/linux/linux-vim.html

  4. github协作开发遇到的问题

    1.十一来了,帝都不好买票,30号就调休一天回去了,项目还没搞完,紧张的不行,就自己和同事搞了一个github协作开发,由于是功能和公司项目不是很沾边,但是是自己的主要工作,就和同事协调了一下,搭建了 ...

  5. java.net.ConnectException: Call From slaver1/192.168.19.128 to slaver1:8020 failed on connection exception: java.net.ConnectException: Connection refused; For more details see: http://wiki.apache.org

    1:练习spark的时候,操作大概如我读取hdfs上面的文件,然后spark懒加载以后,我读取详细信息出现如下所示的错误,错误虽然不大,我感觉有必要记录一下,因为错误的起因是对命令的不熟悉造成的,错误 ...

  6. Python执行ImportError:No module named MySQLdb异常

  7. Zabbix监控Low level discovery实时监控网站URL状态

    今天我们来聊一聊Low level discovery这个功能,我们为什么要用到loe level discovery这个功能呢? 很多时候,在使用zabbix监控一些东西,需要对类似于Itens进行 ...

  8. MyEclipse 安装插件 Github安装/使用 教程

    2016年02月18日 09:45:23 阅读数:4531 本文的目的是 1.在 Myeclipse10.7中 集成Github并使用. 选择的安装方式是:MyEclipse 中设置下文中1后,下载g ...

  9. HTML中鼠标滚轮事件onmousewheel

    IE/Opera属于同一类型,使用attachEvent即可添加滚轮事件. /*IE注册事件*/ if(document.attachEvent){ document.attachEvent('onm ...

  10. YII框架增删改查常用语句

    //实例化db $db = new \yii\db\Query(); //插入 $db->createCommand()->insert('user', [ 'name' => 't ...