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

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. P1151 子数整数

    题目描述 对于一个五位数a_1a_2a_3a_4a_5a1​a2​a3​a4​a5​,可将其拆分为三个子数: sub_1=a_1a_2a_3sub1​=a1​a2​a3​ sub_2=a_2a_3a_ ...

  2. 2018.8.6 学习 log4j.properties 配置文件

    配置文件的话第一步当然是解决乱码问题 Eclipse中properties文件中文乱码解决方式 打开eclipse的properties文件时你会发现,其中部分中文注释乱码了,下面将写出如何设置pro ...

  3. 下载Xcode历史版本方法

    1.打开链接:https://developer.apple.com/download/more 进入页面 2.在搜索框输入Xcode,回车搜索.如图,找到各种版本Xcode 搜索Xcode 3.双击 ...

  4. Spring初始化Bean或销毁Bean前执行操作的方式

    如果想在Spring初始化后,或者销毁前做某些操作,常用的设定方式有三种: 第一种:通过 在xml中定义init-method 和 destory-method方法 推荐使用,缺陷是只能在XML中使用 ...

  5. [转]C++ explicit的作用

    explicit作用: 在C++中,explicit关键字用来修饰类的构造函数,被修饰的构造函数的类,不能发生相应的隐式类型转换,只能以显示的方式进行类型转换. explicit使用注意事项: * e ...

  6. 第34-2题:LeetCode113. Path Sum II

    题目 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例: 给定如下二叉树,以及目标和 sum = 22, 5 / \ ...

  7. JS - 属性描述符各配置的默认值的注意事项

    通过字面量或者obj.x = 1;创建的属性 与 通过Object.defineProperty创建的属性,他们的属性描述符的默认值是不同的,前者都为true,后者都为false.

  8. tcp文件下载客户端+服务端

    客户端: import socket if __name__ == '__main__': # 创建tcp客户端socket tcp_client_socket = socket.socket(soc ...

  9. IBM Rational Software Architect V9.0安装图解

    IBM Rational Software Architect(RSA) -- IBM软件开发平台的一部分 – 是IBM在2003年二月并购Rational以来,首次发布的Rational产品.改进过 ...

  10. python__基础 : 类的继承,调用父类的属性和方法

    1.继承,调用父类属性方法 在python里面,继承一个类只需要这样写: class Animal: def heshui(self): print('动物正在喝水') class Cat(Anima ...