PAT-A1004. Counting Leaves (30)
根据家谱树从根结点开始输出每一层的叶子结点数量。使用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)的更多相关文章
- PAT 1004 Counting Leaves (30分)
1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is t ...
- 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 ...
- PAT 1004. Counting Leaves (30)
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family membe ...
- 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)
1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- 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 ...
- pat1004. Counting Leaves (30)
1004. Counting Leaves (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A fam ...
- 1004 Counting Leaves (30分) DFS
1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- 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 ...
随机推荐
- mac下安装Brew 警告:Warning: /usr/local/bin is not in your PATH.
终端输入命令 export PATH=/usr/local/bin:$PATH
- 【Leetcode | 5】求和问题
一.1两数之和 二.15三数之和 C++ Soution 1: class Solution { public: vector<vector<int>> threeSum(ve ...
- Win10任务栏通知区域上已卸载程序无效图标选项如何清除?
在Win10系统中,大部分用户都已经知道在“选择在任务栏上显示哪些图标”来让一些软年图标显示,一些隐藏,不过使用Win10系统久了之后发现,在设置通知区域图标中有很多已经卸载程序的无效选项!这让设置时 ...
- bzoj3992
题解: 求模素数 p 原根的方法:对 p-1 进行素因子分解,记pi为p-1的第i个因子,若恒有a^((p-1)/pi) mod p ≠ 1 成立,则 a 就是 p 的原根.(对于合数求原根,只需把 ...
- 在CentOS 7+ 安装Kubernetes入门(单Master)
TL;DR; ***,***,***,重要的事情说三次.如果不会***,这篇文章就没有看下去的意义.作为一个技术人员如果不愿意折腾,很难有所作为.作为一个单纯的技术人员,最好把心思放在技术上,做到真正 ...
- net core体系-web应用程序-1VS2017构建一个简单的web
使用vs2017,添加一个新项目-asp.net core web应用程序. 结构如图, wwwroot放了网站的静态资源如css.js.image文件: appsetting.json是应用程序的配 ...
- Python 实现红绿灯
一.通过Event来实现两个或多个线程间的交互,下面是一个红绿灯的例子,即起动一个线程做交通指挥信号灯,一个线程做车辆,车辆行驶按红灯停,绿灯行的规则. #!/usr/bin/python # -*- ...
- Codeforces 1000G Two-Paths 树形动态规划 LCA
原文链接https://www.cnblogs.com/zhouzhendong/p/9246484.html 题目传送门 - Codeforces 1000G Two-Paths 题意 给定一棵有 ...
- 求小于n且与n互质的数的个数
int eu(int n){ int ans=n; for(int i=2;i*i<=n;i++) { if(n%i==0) { ans=ans/i*(i-1); while(n%i==0)n/ ...
- python面试题之如何用Python输出一个斐波那契数列
so eary! 1 a,b = 0, 1 2 while b<100: 3 print (b), 4 a, b = b, a+b 本文转载自:python黑洞网 原文链接:http://www ...