题目链接: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 1004 Counting Leaves

    题目与翻译 1004 Counting Leaves 数树叶 (30分) A family hierarchy is usually presented by a pedigree tree. You ...

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

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

随机推荐

  1. Docker configure http proxy

    from: http://stackoverflow.com/questions/23111631/cannot-download-docker-images-behind-a-proxy That' ...

  2. ASP.NET Web API与Rest web api

    ASP.NET Web API is a framework that makes it easy to build HTTP services that reach a broad range of ...

  3. MyBatis <if>标签的一些问题

    1.常见错误: There is no getter for property named 'parentId' in 'class java.lang.Long'(或者String) org.myb ...

  4. Appium+Robotframework实现Android应用的自动化测试-6:一个简单的例子

    万事具备,只欠编码! 下面看一个简单的示例,这个示例验证Android手机自带的通讯录的添加联系人的操作是否成功.这个例子是Appium官网自带的示例,有兴趣的同学也可以自己下载来研究和学习,下载地址 ...

  5. Html5 postMessage

    解释: 跨文档消息传输Cross Document Messaging. 编写代码前注意判断浏览器是否支持Html5 实例: b页面向a页面发送消息. <!DOCTYPE> <htm ...

  6. 压测 apache ab 初探

    2015年10月30日 14:58:34 ab是apache自带的压测命令, 在其bin目录下边, 不仅可以压测Apache, 也可以测nginx或其他服务器 可以模拟上传post值 (-p, 与下边 ...

  7. SAP ALV显示并打印(非OO方式)

    *&---------------------------------------------------------------------* *& Report  Z_SD_CPF ...

  8. Mysql 基础3

    1. 逗号是个好东西2.对于多条件查询 和范围查询 的灵活运用(and 和or 的灵活运用)in 用的时候注意 补充select * from car where name like '%奥迪%' a ...

  9. Android图片加载库:最全面的Picasso讲解

    前言 上文已经对当今 Android主流的图片加载库 进行了全面介绍 & 对比 如果你还没阅读,我建议你先移步这里阅读 今天我们来学习其中一个Android主流的图片加载库的使用 - Pica ...

  10. HDU 5996 dingyeye loves stone ---BestCoder Round #90

    题目链接 设根节点的深度为0,将所有深度为奇数的节点的石子数目xor起来,则先手必胜当且仅当这个xor和不为0. 证明同阶梯博弈.对于偶深度的点上的石子,若对手移动它们,则可模仿操作:对于奇深度上的石 ...