题目链接: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. jQuery清除文本框,内容并设置不可用

    JQuery清除文本框,内容并设置不可用  如果是设置只读,则将disabled换成readonly function CleanText(textid) { $("#"+text ...

  2. Android自定义图形shape

    在Android开发过程中,经常需要改变控件的默认样式, 那么通常会使用多个图片来解决.不过这种方式可能需要多个图片,比如一个按钮,需要点击时的式样图片,默认的式样图片. 这样就容易使apk变大.另一 ...

  3. js注册读秒进度条

    转载自://http://blog.csdn.net/wugouzi/article/details/12621385 <head> <meta http-equiv="C ...

  4. ajax传值方式为数组

    js: function responseJson1(){    var array=[1001,1002];    var str="";        //获取table对象  ...

  5. UVA 156 Ananagrams ---map

    题目链接 题意:输入一些单词,找出所有满足如下条件的单词:该单词不能通过字母重排,得到输入文本中的另外一个单词.在判断是否满足条件时,字母不分大小写,但在输出时应保留输入中的大小写,按字典序进行排列( ...

  6. JS 保留两位小数问题收集

    1.使用四舍五入的方法,保留小数点后的两位小数: toFixed里面的参数表示保留的小数的位数,范围是0-20,超过20位就会报错了 <script> var num=22.127456; ...

  7. MongoDB配置文件YAML-based选项全解

    配置文件部分 MongoDB引入一个YAML-based格式的配置文件.2.4版本以前的仍然兼容. 我的mongodb配置文件: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...

  8. Warp divergence

    Threads are executed in warps of 32, with all threads in the warp executing the same instruction at ...

  9. 对于大一学习计算机的新手(c/c++ )提出一些学习经验

    对于刚刚上大一的新手,且是那种十分有上进的学生,在学习计算机的过程中必然会有一大堆的困惑,比如: 1 .如何学好编程(这与以往的应试教育完全不同,按照以往的那种学习方式,看书刷题不过是成为一个考试学霸 ...

  10. Scrapy爬取美女图片 (原创)

    有半个月没有更新了,最近确实有点忙.先是华为的比赛,接着实验室又有项目,然后又学习了一些新的知识,所以没有更新文章.为了表达我的歉意,我给大家来一波福利... 今天咱们说的是爬虫框架.之前我使用pyt ...