题目链接:https://www.patest.cn/contests/pat-a-practise/1004

大意:输出按层次输出每层无孩子结点的个数

思路:vector存储结点,dfs遍历

 #include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn=1e2+;
int n,m,k,x,f[maxn],deep,y;
vector<int> t[maxn];
void init()
{
cin>>n>>m;
while(m--)
{
cin>>x>>k;
while(k--)
{
cin>>y;
t[x].push_back(y);
}
}
}
void dfs(int x,int dep)
{
deep=max(dep,deep);
if(t[x].size()==)f[dep]++;
for(int i=;i<t[x].size();i++)
dfs(t[x][i],dep+);
} int main()
{
init();
dfs(,);
for(int i=;i<=deep;i++)
printf("%s%d",i?" ":"",f[i]);
printf("\n");
return ;
}

PAT A 1004. Counting Leaves (30)【vector+dfs】的更多相关文章

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

  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)

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

  4. 1004. Counting Leaves (30)

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

  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分) DFS

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

  7. PAT甲1004 Counting Leaves【dfs】

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

  8. 【PAT Advanced Level】1004. Counting Leaves (30)

    利用广度优先搜索,找出每层的叶子节点的个数. #include <iostream> #include <vector> #include <queue> #inc ...

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

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

随机推荐

  1. spring.net (3)依赖注入基础

    属性的注入: 在上篇例子中已经出现并解释过: <object id="dog" type="SpringDemo.Dog,SpringDemo" sing ...

  2. C语言,输入一个正整数,按由大到小的顺序输出它的所有质数的因子(如180=5*3*3*2*2)

    #include <iostream> using namespace std; int main() { long num; while(cin >> num){ ){ co ...

  3. NPOI--操作Excel之利器(一)

    最近在做一个产品配置的项目,类似于京东上的自主装机,也就是根据自己的需要配置一套完整的产品,只不过我们做的是一个网络产品的配置,如路由器,交换机等网络设备.配置完成后会将配置的信息导出到Excel中, ...

  4. C#实现微信公众号群发消息(突破破解一天只能发一次的限制)

    总体思路:1.首先必须要在微信公众平台上申请一个公众号. 2.然后进行模拟登陆.(由于我对http传输原理和编程不是特别懂,在模拟登陆的地方,不是特别清楚,希望有大神指教) 3.模拟登陆后会获得一个t ...

  5. Linux系统下给非root用户添加sudo权限

    Linux系统下给非root用户添加sudo权限 有时,在linux系统中非root用户运行sudo命令,会提示类似信息:  xxx is not in the sudoers file. This ...

  6. angular : ngModel 内部流程

    angular 1.5 beta link NgModelController provides API for the ngModel directive. The controller conta ...

  7. 听翁恺老师mooc笔记(2)-第一个程序--&amp;运算符

    使用devC++写hello world 第一步:文件-新建-源代码.然后输入"输出hello world"程序: 注意:写程序时关闭中文输入法,否则语句输入的分号可能会被识别为错 ...

  8. [Swift]LeetCode995. K 连续位的最小翻转次数 | Minimum Number of K Consecutive Bit Flips

    In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray o ...

  9. Java 调用翻译软件实现英文文档翻译

    前言: 因最近要进行OCP的考试准备.看着大堆英文文档确实有些疼痛.又因文档内容有点大,又需要逐一去翻译 又很费时费力.于是 百度了一番,找到一些 可以使用Java来调用百度翻译软件的API( 注:( ...

  10. iOS自动布局框架-Masonry详解

    首先,在正式使用Masonry之前,我们先来看看在xib中我们是如何使用AutoLayout     从图中我们可以看出,只要设置相应得局限,控制好父视图与子视图之间的关系就应该很ok的拖出你需要的需 ...