题目

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

  1. dfs深度优先遍历树,h记录当前节点所在层数,max_h记录最大层数,int left[n]记录每层叶子节点数

思路 02

  1. bfs广度优先遍历树(借助queue),max_h记录最大层数,int left[n]记录每层叶子节点数,int h[n]记录每个节点所在层数(根节点初始化为h[1]=0)

知识点

  1. 广度优先遍历树,使用int h[n]记录每个节点所在层数
  2. 深度优先遍历树,使用参数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;
}

最新文章

  1. ZooKeeper 笔记(4) 实战应用之【消除单点故障】
  2. left join 条件区别
  3. DevPress GridControl添加按钮列
  4. github 使用体会
  5. jpa 支持(sql)JDBC标准语句
  6. cocos中BatchNode精灵集合的使用
  7. 算法之旅,直奔&lt;algorithm&gt;之十三 fill
  8. Apache MINA 框架之Handler介绍
  9. HDU_2553——n皇后问题,作弊
  10. 记录一下自己用到的python logging
  11. Windows,查看进程的连接的IP地址,批量模式,最后做成Excel
  12. J-Robot,能走、能跳舞的机器人
  13. docker容器的安装与使用
  14. ubuntu16.04 backup and restore
  15. Web 应用程序项目 Himall.Web 已配置为使用 IIS。 无法访问 IIS 元数据库
  16. python中RabbitMQ的使用(远程过程调用RPC)
  17. 【GitLab】gitlab上配置webhook后,点击测试报错:Requests to the local network are not allowed
  18. Qt学习之路(28): 坐标变换
  19. 【textarea】在JSP上添加textarea-文本域 调试使用
  20. nagios-4.0.8 安装部署

热门文章

  1. postman 请求get post方法的 区别
  2. poj 1318 Word Amalgamation
  3. 一道算法题加深我对C++中map函数的理解
  4. 每天一点点之vue框架学习 - uni-app 修改上一页参数
  5. linux下安装redis,按照redis官网安装不成功需要提前安装c++环境(安装成功并可以测试)
  6. 75.Python中ORM聚合函数详解:Sum
  7. Azure Container Registry-基于开源 Docker Registry 的专用 Docker 注册表服务
  8. 二十五、JavaScript之查找字符串中的字符串indexOf和lastIndexOf的用法
  9. 九十九、SAP中ALV事件之十二,给ALV的标题栏添加图片
  10. idea新建maven web项目