PAT Advanced 1004 Counting Leaves (30) [BFS,DFS,树的层序遍历]
题目
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.
Input
Each input file contains one test case. Each case starts with a line containing 0 < N < 100, the number of nodes in a tree, and M (< N), the number of non-leaf nodes. Then M lines follow, each in the format: ID K ID[1] ID[2] … ID[K] where ID is a two-digit number representing a given non-leaf node, K is the number of its children, followed by a sequence of two-digit ID’s of its children. For the sake of simplicity, let us fix the root ID to be 01.
Output
For each test case, you are supposed to count those family members who have no child for every seniority level starting from the root. The numbers must be printed in a line, separated by a space, and there must be no extra space at the end of each line. The sample case represents a tree with only 2 nodes, where 01 is the root and 02 is its only child. Hence on the root 01 level, there is 0 leaf node; and on the next level, there is 1 leaf node. Then we should output “0 1” in a line.
Sample Input
2 1
01 1 02
Sample Output
0 1
题目分析
已知每个节点的子节点,统计每层叶子节点数
解题思路
思路 01
- dfs深度优先遍历树,h记录当前节点所在层数,max_h记录最大层数,int left[n]记录每层叶子节点数
思路 02
- bfs广度优先遍历树(借助queue),max_h记录最大层数,int left[n]记录每层叶子节点数,int h[n]记录每个节点所在层数(根节点初始化为h[1]=0)
知识点
- 广度优先遍历树,使用int h[n]记录每个节点所在层数
- 深度优先遍历树,使用参数h记录当前节点所在层数
Code
Code 01(dfs)
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> nds[101];
struct node {
int data;
int depth=0;
};
int max_h,leaf[101];
void dfs(int index,int h) {
max_h=max(h,max_h);//记录最大层数
if(nds[index].size()==0) { //叶子节点
leaf[h]++;
return;
}
for(int i=0; i<nds[index].size(); i++) {
dfs(nds[index][i],h+1);
}
}
int main(int argc, char * argv[]) {
int n,m,rid,cid,k;
scanf("%d %d",&n,&m);
for(int i=0; i<m; i++) {
scanf("%d %d",&rid,&k);
for(int j=0; j<k; j++) {
scanf("%d",&cid);
nds[rid].push_back(cid);
}
}
dfs(1,0);
for(int i=0; i<=max_h; i++) {
if(i!=0)printf(" ");
printf("%d",leaf[i]);
}
return 0;
}
Code 02(bfs)
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
vector<int> nds[101];
int leaf[101],h[101],max_h;
void bfs(){
queue<int> q;
q.push(1);
while(!q.empty()){
int now = q.front();
q.pop();
max_h=max(max_h,h[now]);
if(nds[now].size()==0){
leaf[h[now]]++;
} else{
for(int i=0;i<nds[now].size();i++){
h[nds[now][i]]=h[now]+1;
q.push(nds[now][i]);
}
}
}
}
int main(int argc, char * argv[]) {
int n,m,rid,cid,k;
scanf("%d %d",&n,&m);
for(int i=0; i<m; i++) {
scanf("%d %d",&rid,&k);
for(int j=0; j<k; j++) {
scanf("%d",&cid);
nds[rid].push_back(cid);
}
}
h[1]=0;
bfs();
for(int i=0;i<=max_h;i++){
if(i!=0)printf(" ");
printf("%d",leaf[i]);
}
return 0;
}
PAT Advanced 1004 Counting Leaves (30) [BFS,DFS,树的层序遍历]的更多相关文章
- 1004 Counting Leaves (30分) DFS
1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- PAT Advanced 1004 Counting Leaves
题目与翻译 1004 Counting Leaves 数树叶 (30分) A family hierarchy is usually presented by a pedigree tree. You ...
- PAT A 1004. Counting Leaves (30)【vector+dfs】
题目链接:https://www.patest.cn/contests/pat-a-practise/1004 大意:输出按层次输出每层无孩子结点的个数 思路:vector存储结点,dfs遍历 #in ...
- 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 ...
- PAT 解题报告 1004. Counting Leaves (30)
1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is t ...
- 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 ...
- PAT甲1004 Counting Leaves【dfs】
1004 Counting Leaves (30 分) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- 【PAT Advanced Level】1004. Counting Leaves (30)
利用广度优先搜索,找出每层的叶子节点的个数. #include <iostream> #include <vector> #include <queue> #inc ...
随机推荐
- maven详解 之仓库
Maven仓库分类 MAVEN仓库分类 Maven仓库分为:本地仓库+远程仓库两大类 远程仓库又分为:中央仓库+私服+其它公共远程仓库 1,在Maven中,任何一个依赖.插件或者项目构建的输出,都 ...
- DevOps 教程
DevOps是一种研发文化,它促进开发团队和运维团队之间更好地协作,以自动化和可重复的方式,更快地将代码部署到生产环境中.DevOps是development和operations两个单词的组合. D ...
- Linux netfliter 架构
netfliter 简介 netfilter是在Linux 2.4.X内核引入的一个子系统,它提供了一个抽象的.通用框架,这个框架提供了一整套的钩子函数的管理机制.包括钩子函数的原型定义,注册,注销等 ...
- C++ for无限循环~
无限循环 如果条件永远不为假,则循环将变成无限循环.for 循环在传统意义上可用于实现无限循环.由于构成循环的三个表达式中任何一个都不是必需的,您可以将某些条件表达式留空来构成一个无限循环. #inc ...
- 如何有效避免Essay写作抄袭
每到学期末的时候,各种考试,论文以及作业数不胜数,压得留学党们快要喘不过气了.我想比起写论文,同学们更操心的问题应该是:Plagiarism.要知道在国外Plagiarism的这种行为在学术中是零容忍 ...
- 报错盲注之exp注入(double数值类型溢出原理详解)
首先感谢原文博主,在此致敬.本文转自:http://www.cnblogs.com/lcamry/articles/5509124.html Exp()为以 e 为底的对数函数:MySQL版本在 5. ...
- 关于 with 语句
class C(object): def __enter__(self): print('jinru') return self def __exit__(self, exc_type, exc_va ...
- Petr#(字符串哈希)
CF113B Petr# 大概就是字符串匹配加一个字符串哈希判重.懒得打kmp,就用字符串哈希匹配了. 字符串哈希大概就是把字符串转成一个p进制的数,每一段字符串都有一个对应的哈希值.p尽量取质数,这 ...
- redis性能测试方法
redis本身设计为单线程服务器,性能本身并不随着多核而提高,但是会随着cpu本身而改变,AMD的可能只有Intel一半的性能,Intel是最好的选择. 性能会随着连接数的增多而下降,30000大概只 ...
- 在MFC做DLL动态链接库时,使用boost,出现断言错误
建立的MFC DLL工程中有使用boost::thread,就会发生compile正常但是一程式执行或者直接编辑就出現ASSERT错误. 错误位置:dllinit.cpp,Line: 587,ASSE ...