简单DFS。

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<vector>
using namespace std; const int maxn=+;
vector<int>g[maxn];
int n,m;
int ans[maxn];
int root;
int Deep; void dfs(int x,int deep)
{
Deep=max(Deep,deep);
if(g[x].size()==)
{
ans[deep]++;
return ;
}
for(int i=;i<g[x].size();i++)
dfs(g[x][i],deep+);
} int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) g[i].clear();
for(int i=;i<=m;i++)
{
int id; scanf("%d",&id);
int k; scanf("%d",&k);
while(k--)
{
int id2; scanf("%d",&id2);
g[id].push_back(id2);
}
}
memset(ans,,sizeof ans);
root=; Deep=;
dfs(root,);
for(int i=;i<=Deep;i++)
{
printf("%d",ans[i]);
if(i<Deep) printf(" ");
else printf("\n");
}
return ;
}

PAT (Advanced Level) 1004. Counting Leaves (30)的更多相关文章

  1. PAT 解题报告 1004. Counting Leaves (30)

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

  2. PTA (Advanced Level) 1004 Counting Leaves

    Counting Leaves A family hierarchy is usually presented by a pedigree tree. Your job is to count tho ...

  3. PAT (Advanced Level) 1049. Counting Ones (30)

    数位DP.dp[i][j]表示i位,最高位为j的情况下总共有多少1. #include<iostream> #include<cstring> #include<cmat ...

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

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

  5. PAT 1004 Counting Leaves (30分)

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

  6. 1004. Counting Leaves (30)

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

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

  8. 1004 Counting Leaves (30分) DFS

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

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

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

随机推荐

  1. Tomcat 7优化

    1.在bin/catalina.bat文件中加入下面参数,对JVM进行优化,至于这一大驼参数的作用及说明,大家到网上找找,应该有很多的,如:http://www.mzone.cc/article/32 ...

  2. docker 基础命令二

    开启/停止/重启 查看当前正在运行容器docker ps 查看包括已经停止的所有容器docker ps -a 显示最新启动的一个容器docker ps -l 新建一个容器运行docker run 启动 ...

  3. eclipse如何快速抽取样式(style)或者include

    在视图模式上选中要抽取的模块,然后点击右键就可以抽取了

  4. 给EditText设置边框

    布局文件中加入background属性: <EditText android:layout_width="200dp" android:layout_height=" ...

  5. minicom 不能输入

    minicom -s  进入设置界面 在 Serial port setup 中设置 硬件控制流为NO F - Hardware Flow Control : No 就OK了

  6. js的各种错误类型

    1.SyntaxError(语法错误) 解析代码时发生的语法错误 eg:var 1a; Uncaught SyntaxError: Unexpected number 2.ReferenceError ...

  7. Too Much Money

    Too Much Money time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  8. WPF Template模版之DataTemplate与ControlTemplate【一】

    WPF Template模版之DataTemplate与ControlTemplate[一] 标签: Wpf模版 2015-04-19 11:52 510人阅读 评论(0) 收藏 举报  分类: -- ...

  9. Android开机动画启动流程

    android开机动画启动流程   从android的Surface Flinger服务启动分析知道,开机动画是在SurfaceFlinger实例通过调用startBootAnim()启动的. 下面我 ...

  10. HDU<1372>/bfs

    题目连接 简单bfs搜索 #include <set> #include <map> #include <cmath> #include <queue> ...