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 ...
随机推荐
- printf的封装与实现
1 UART通信协议 1.1 UART通信的物理连接 图1 UART的物理连接 1.2 逻辑电平 用电平表示逻辑1和逻辑0,逻辑1和逻辑0用来组织计算机层面的数据. 1.3 电平标准 根据通讯使用的电 ...
- P1037 在霍格沃茨找零钱
转跳点:
- POJ 3393:Lucky and Good Months by Gregorian Calendar 年+星期 模拟
Lucky and Good Months by Gregorian Calendar Time Limit: 1000MS Memory Limit: 65536K Total Submissi ...
- oracle11g导出dmp文件时不能导出空表,导致缺表
一.执行 select 'analyze table '||table_name||' compute statistics;' from user_tables; 将该查询语句查询到的结果粘贴到sq ...
- java 实体 set数据 报空指针异常
今天在做一个调用阿里云AXB隐私保护,需要调用通话记录的消费队列,然后set到实体中,然后插入到数据库,但是set的这一步报错 以为工具拿不到值,然后打印发现是有值的, 然后再看一下实例的类型是没错的 ...
- Adobe Photoshop CC2014 for MAC 详细破解步骤
1,安装Adobe Photoshop CC2014 for MAC,可以断网安装,如果不断网的话,需要申请一个Adobe ID,是免费申请. 2,下载破解工具,https://sdifen.ctfi ...
- 屏幕适配 - JS - 网站布局元素
网页可见区域宽:document.body.clientWidth; 网页可见区域高:document.body.clientHeight; 网页可见区域高:document.body.offsetW ...
- IBGP(内部BGP)的对等体组(命令解析)
IBGP(内部BGP)对等体组配置解析: ①:创建对等体组. ②:定义对等体组策略,指定邻居路由器及所在的AS. ③:定义,更新源. ④:(若边界)定义自己下一跳. ⑤:加入对等体组. IBGP(内部 ...
- c++ STD Gems07
reverse.rotate.permutation #include <iostream> #include <vector> #include <string> ...
- Short Essay你真的会写了吗?
提到short essay(可能其他essay也一样),很多同学都很头疼.“没有思路?不知从何下笔?没有亮点?”等等,这些都是同学们的致命伤,因此,short essay就成为了广大留学生的“送命题” ...