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 ...
随机推荐
- NO13 Linux的基础优化-关闭SELinux功能-Linux的7种运行级别-防火墙设置-中文显示设置
壹 安装Linux系统后调优及安全设置: 1 关闭SELinux功能: [root@localhost data]# sed 's#SELINUX=enforcing#SELINUX=disable ...
- python -- 相对路径、绝对路径、以及路径的获取
1.定义 绝对路径:就是文件的真正存在的路径,是指从硬盘的根目录(盘符)开始,进行一级级目录指向文件. 相对路径:就是以当前文件为基准进行一级级目录指向被引用的资源文件. ../ 表示当前文件所在 ...
- 【linux】linux系统安全设置
1.下载安装安全软件 2.取消Telnet登录采用SSH方式并更改ssh服务端远程登录的配置 1)Telnet登录协议是明文不加密不安全,所以采用更安全的SSH协议. 2)更改ssh服务端远程登录相关 ...
- 用JS改变embed标签的src属性
思路: A.先隐藏embed标签 B.清除embed元素 C.为embed重新赋值,加入Html页面中 1.html代码 <object id="forfun" classi ...
- hibernate保存失败
报错:org.hibernate.jdbc.BdatchedTooManyRowsAffectedException: Batch update returned unexpected row cou ...
- Django ORM中常用的字段类型以及参数配置
一.数值型 AutoField对应int(11).自增主键,Django Model默认提供,可以被重写. BooleanField对应tinyint(1).布尔类型字段,一般用于记录状态标记. De ...
- 037-PHP如何返回闭包函数实例
<?php /*: 如何返回闭包函数实例*/ # 直接调用将不会输出$txt的内容 function demo() { $txt = '我爱PHP'; $func = function () u ...
- 与Power BI一起使用Cortana
使用此页面测试您的Cortana卡.https://app.powerbi.com/cortana/test 文档: 使用Power BI为Cortana创建自定义答案页https://powerbi ...
- P5091 【模板】欧拉定理(欧拉降幂)
P5091 [模板]欧拉定理 以上3张图是从这篇 博客 里盗的,讲的比较清楚. #include<bits/stdc++.h> using namespace std; typedef l ...
- VS Code使用.vue的v-for方法提示错误的解决办法
1.在使用v-for的时候在后面跟上:key="key" <div v-for="(value, key) in [1,2,3]" :key=" ...