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

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. ZIGBEE report机制分析

    ZIGBEE提供了report机制(现在只学习了send, receive还没学习) 主要目的是实现attribute属性的report功能,即提供了一种服务端和客户端数据同步的机制 以EMBER的H ...

  2. 字符串替换For linux C

    1.临时空间给了个1024,不需要可减少长度. 2.结果只用用strcpy了,没校验. bool Replace(char *str,const char *src, const char *des) ...

  3. input属性总结

    <input type="text" readonly="readonly" /> 这个是不能输入的 readonly="readonly ...

  4. IOS中input与fixed同时存在的情况会出现bug

    两种解决方案,一种是将内容区域放在中间部分,只是中间部分在滚动(还是固定在底部):另一种是判断当是ios时,将其转换为absolute定位.(跟随着页面的滚动而滚动);; 当使用input时,fixe ...

  5. Huffman Tree -- Huffman编码

    #include <stdlib.h> #include <stdio.h> #include <string.h> typedef struct HuffmanT ...

  6. HyperLedger Fabric 1.4 区块链技术形成(1.2)

    在比特币诞生之时,没有区块链技术概念,当人们看到比特币在无中心干预的前提下,还能安全.可靠的运行,比特币网络打开了人们的想象空间:技术专家们开始研究比特币的底层技术,并抽象提取出来,形成区块链技术,或 ...

  7. [Codeforces958C2]Encryption (medium)(区间DP)

    Description 题目链接 Solution 显然的区间DP,正常想法f[i][j]表示前i个数分成j块,每次在i前找一个k使得balala,然而常规打法会超时 我们发现,对于i前面的所有点,他 ...

  8. C++ 二叉搜索树

    二叉搜索树利用其特有的二叉树性质,使其搜索更方便 源代码: struct node { int val; node *left, *right; }; //the function of insert ...

  9. python-6面向对象编程

    1-类和实例 class Student(object): def __init__(self, name, score):# _init__方法的第一个参数永远是self,表示创建的实例本身 sel ...

  10. Echarts 解决饼图文字过长重叠的问题

    之前在网上查找了很多关于解决饼图文字描述过长导致重叠的问题,找了很多一直没有一个合适的解决方案,最后自己只能花时间研究echarts文档,功夫不负有心人,终于解决了文字重叠展示不全等问题. 废话不多说 ...