根据家谱树从根结点开始输出每一层的叶子结点数量。使用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. 安装淘宝npm(cnpm)

    https://www.cnblogs.com/yominhi/p/7039795.html http://npm.taobao.org/ 初始化一个 mpvue 项目 现代前端开发框架和环境都是需要 ...

  2. .NetCore 下开发独立的(RPL)含有界面的组件包 (二)扩展中间件及服务

    .NetCore 下开发独立的(RPL)含有界面的组件包 (一)准备工作 .NetCore 下开发独立的(RPL)含有界面的组件包 (二)扩展中间件及服 务 .NetCore 下开发独立的(RPL)含 ...

  3. flanneld,flannel和cni逐步深入

    这个问题,现在慢慢搞定. 把这两者的关系搞清楚了. 还有kubeadm join和手工安装Node的故事, 也没那么玄乎~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ htt ...

  4. centos安装pg以及pg配置ssl

    https://blog.csdn.net/iteye_21194/article/details/82645389 https://blog.csdn.net/rudy5348/article/de ...

  5. CAS5.3.X 配置备忘

    ## # 普通MD5用户jdbc验证 ## #配置数据库连接 cas.authn.jdbc.query[0].driverClass=com.mysql.cj.jdbc.Driver cas.auth ...

  6. mysql恢复ibd文件

    1.将原表删除,包括ibd和frm文件 2.重新创建表结构. 3.丢弃表空间 alter table tableName discard tablespace; 4.将要恢复的ibd文件拷贝到数据库目 ...

  7. Evaluation map and reflexive space

    For a normed space \(X\), an isometric isomorphism can be defined from \(X\) to its second dual spac ...

  8. 构建简单的json树形菜单

    json结构: var Menu = [{ tit:"一级菜单", submenu:[{ tit:"二级菜单", url:"", func: ...

  9. IIS:另一个程序正在使用此文件进程无法访问。

    启动网站时,遇到这个错误,一般是端口已经被占用,更换一个空闲端口即可. 通过以下命令可查询 根据最后一列的数字在任务管理器中可查看被哪个程序占用了

  10. Codeforces 666E E - Forensic Examination SA + 莫队 + 线段树

    E - Forensic Examination 我也不知道为什么这个复杂度能过, 而且跑得还挺快, 数据比较水? 在sa上二分出上下界, 然后莫队 + 线段树维护区间众数. #include< ...