给出一棵树,问每一层各有多少叶子节点

dfs遍历树

#include<bits/stdc++.h>

using namespace std;
vector<int>p[];
int n,m;
int node ,k;
int vis[];
int maxn=-;
void dfs(int node,int step)
{
if(p[node].empty()){
vis[step]++;
maxn=max(step,maxn);
}
else{
for(int i=;i<p[node].size();i++){
dfs(p[node][i],step+);
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin>>n>>m;
for(int i=;i<m;i++){
cin>>node>>k;
int t;
for(int j=;j<k;j++){
cin>>t;
p[node].push_back(t);
}
}
dfs(,);
cout<<vis[];
for(int i=;i<=maxn;i++){
cout<<" "<<vis[i];
}
cout<<endl;
return ;
}

bfs遍历求树

 #include<bits/stdc++.h>

 using namespace std;
struct NODE
{
int data;
int step;
NODE(){}
NODE(int data1,int step1):data(data1),step(step1){}
};
vector<int>p[];
int n,m;
int node ,k;
int vis[];
int maxn=-;
void bfs(int node,int step)
{
queue<NODE>Q;
Q.push(NODE(node,step));
while(!Q.empty()){
NODE u=Q.front();
Q.pop();
if(p[u.data].empty()){
maxn=max(u.step,maxn);
vis[u.step]++;
}
for(int i=;i<p[u.data].size();i++){
Q.push(NODE(p[u.data][i],u.step+));
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin>>n>>m;
for(int i=;i<m;i++){
cin>>node>>k;
int t;
for(int j=;j<k;j++){
cin>>t;
p[node].push_back(t);
}
}
bfs(,);
cout<<vis[];
for(int i=;i<=maxn;i++){
cout<<" "<<vis[i];
}
cout<<endl;
return ;
}

1004 Counting Leaves (30 分)(树的遍历)的更多相关文章

  1. 1004 Counting Leaves (30分) DFS

    1004 Counting Leaves (30分)   A family hierarchy is usually presented by a pedigree tree. Your job is ...

  2. PAT 1004 Counting Leaves (30分)

    1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is t ...

  3. 【PAT甲级】1004 Counting Leaves (30 分)(BFS)

    题意:给出一棵树的点数N,输入M行,每行输入父亲节点An,儿子个数n,和a1,a2,...,an(儿子结点编号),从根节点层级向下依次输出当前层级叶子结点个数,用空格隔开.(0<N<100 ...

  4. 1004 Counting Leaves (30 分)

    A family hierarchy is usually presented by a pedigree tree. Your job is to count those family member ...

  5. 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 ...

  6. 1004. Counting Leaves (30)

    1004. Counting Leaves (30)   A family hierarchy is usually presented by a pedigree tree. Your job is ...

  7. PAT 解题报告 1004. Counting Leaves (30)

    1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is t ...

  8. 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 mem ...

  9. PAT 1004. Counting Leaves (30)

    A family hierarchy is usually presented by a pedigree tree.  Your job is to count those family membe ...

  10. PAT A 1004. Counting Leaves (30)【vector+dfs】

    题目链接:https://www.patest.cn/contests/pat-a-practise/1004 大意:输出按层次输出每层无孩子结点的个数 思路:vector存储结点,dfs遍历 #in ...

随机推荐

  1. MySQL 数据库和一些常用命令的使用

        常用命令总结: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3 ...

  2. caffe resize用interpolation

    opencv的resize默认的是使用双线性插值INTER_LINEAR,也可以是尝试其他的方式进行插值操作 if (param.random_interpolation_method()) { // ...

  3. 卷积神经网络CNN在自然语言处理的应用

    摘要:CNN作为当今绝大多数计算机视觉系统的核心技术,在图像分类领域做出了巨大贡献.本文从计算机视觉的用例开始,介绍CNN及其在自然语言处理中的优势和发挥的作用. 当我们听到卷积神经网络(Convol ...

  4. 统一项目中编码风格(Eclipse Java code format、codetemplate)

    在公司内的日常开发过程中,除了需要遵守统一的编码规范之外,还需要对编写的代码做统一的格式化,Eclipse提供了格式化编码的工具,快捷键是:Ctrl+Shift+F. 为了统一项目组的代码风格,建议使 ...

  5. shell编程中的vim命令说明

    vim命令模式:  1.一般命令模式 2.编辑模式 3.底行命令行命令模式 一般命令模式 直接用字符操作编辑模式 可以写文档(跟txt有点像)底行命令模式 先按'ESC',在按下“:”,之后在输出命令 ...

  6. js延迟执行与循环执行

    延迟一段时间执行特定代码: setTimeout(function () { window.location.href = 'login' },1200); 循环执行: function test() ...

  7. java基础 数组 Set Map 集合综合应用 生成带0的随机字符串 "00000001" 水果商品号问题

    package com.swift.test01; /*有四种水果(苹果,香蕉,西瓜,橘子) 1.给每种水果设定一个商品号,商品号是8个0-9的随机数,商品号码不能重复, 最小值 "0000 ...

  8. EAIntroView–高度可定制的iOS应用欢迎页通用解决方案

    简介 高度可定制的应用欢迎页通用解决方案,可高度定制,不要仅限于现有的demo. 项目主页: EAIntroView 最新示例: 点击下载 入门 安装 安装后,引入” EAIntroView.h”并设 ...

  9. 【模板时间】◆模板·I◆ 倍增计算LCA

    [模板·I]LCA(倍增版) 既然是一篇重点在于介绍.分析一个模板的Blog,作者将主要分析其原理,可能会比较无趣……(提供C++模板) 另外,给reader们介绍另外一篇非常不错的Blog(我就是从 ...

  10. springBoot支持PageHelp插件使用学习笔记

    首先在springboot项目的maven中加入依赖(版本可能需要自己选择合适的) <dependency> <groupId>com.github.pagehelper< ...