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 ...
随机推荐
- 20165323 学习基础和C语言基础调查
20165323 学习基础和C语言基础调查 一.技能学习心得 1.你有什么技能比大多人更好? 我觉得我羽毛球打的还行,不能说打得比大多数人好,但是对于一些打羽毛球的要领还是掌握的. 2.针对这个技能的 ...
- OpenAuth.Net.landv分支之旅开始制作CRM系统
OpenAuth.Net.landv分支之旅开始制作CRM系统 这个事件的由来是因为没有一个统一的会员卡平台系统,目前需要连接三家酒店会员系统,由于三家酒店使用了三种酒店管理系统,彼此之间的耦合低.三 ...
- SSD垃圾回收
A complete GC typically:includes four steps: selecting some blocks that contain somestale data as vi ...
- CentOS6 安装gnutls
所有用的的包:https://pan.baidu.com/s/1EQYf3gsK_xT6kCAjrVs2aQ wget http://download.savannah.gnu.org/release ...
- docker运行php网站程序
有一个之前的php网站程序需要迁移到K8S,简单调研了下. 基础镜像 官方提供了诸如php:7.1-apache的基础镜像,但是确认必要的扩展,例如gd,当然官方提供了docker-php-ext-i ...
- 【BZOJ】3730: 震波
原题链接 题解 查询距离一个点距离在一定范围内的点,直接点分树,前缀和用树状数组维护 答案是当前重心距离不超过k - (x到重心距离)的点的前缀和,减去在x所在子树中,距离重心不超过k - (x到重心 ...
- shell界面执行mysql命令
mysql -uroot -poRcl_123 -Dsnsdb_test -e "select host from user;"
- Java中用Scanner扫描控制台输入时的一个小问题
package com.hxl; import java.util.Scanner; public class Test { public static void main(String[] args ...
- TCP简介
TCP(Transmission Control Protocol) 传输控制协议,是一种面向连接的.可靠的.基于字节流的传输层通信协议. TCP是一种面向连接(连接导向)的.可靠的基于字节流的传输层 ...
- json 对象和json字符串
转载至 http://www.cnblogs.com/cstao110/p/3762056.html JSON字符串与JSON对象的区别 Q:什么是"JSON字符串",什么是&q ...