PAT (Advanced Level) 1004. Counting Leaves (30)
简单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)的更多相关文章
- PAT 解题报告 1004. Counting Leaves (30)
1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is t ...
- PTA (Advanced Level) 1004 Counting Leaves
Counting Leaves A family hierarchy is usually presented by a pedigree tree. Your job is to count tho ...
- PAT (Advanced Level) 1049. Counting Ones (30)
数位DP.dp[i][j]表示i位,最高位为j的情况下总共有多少1. #include<iostream> #include<cstring> #include<cmat ...
- 【PAT甲级】1004 Counting Leaves (30 分)(BFS)
题意:给出一棵树的点数N,输入M行,每行输入父亲节点An,儿子个数n,和a1,a2,...,an(儿子结点编号),从根节点层级向下依次输出当前层级叶子结点个数,用空格隔开.(0<N<100 ...
- PAT 1004 Counting Leaves (30分)
1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is t ...
- 1004. Counting Leaves (30)
1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- 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 ...
- 1004 Counting Leaves (30分) DFS
1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- 【PAT Advanced Level】1004. Counting Leaves (30)
利用广度优先搜索,找出每层的叶子节点的个数. #include <iostream> #include <vector> #include <queue> #inc ...
随机推荐
- SUPERVISOR进程管理器配置指南
SUPERVISOR进程管理器配置指南1. supervisor简介1.1. 官网http://supervisord.org/ 1.2. 介绍Supervisor是一个进程控制系统. 它是一个C/S ...
- VMware 全虚拟打开
1.修改.vmx 文件,最后一行加入: hypervisor.cpuid.v0 = “FALSE" mce.enable = “TRUE" vhv.enable = “TRUE&q ...
- 二分三角形的时候尤其需要注意!!! HDU 5115 二分+模拟
题目大意:http://blog.csdn.net/snowy_smile/article/details/49535301 思路:分类讨论,分别在[1,2].(2,3).[3,4).[4,1]相遇, ...
- error: invalid 'asm': invalid operand for code 'w'
google 出结果 http://stackoverflow.com/questions/15623609/including-curl-into-the-android-aosp ........ ...
- linux 查看磁盘、文件夹、文件大小(df du)
du 查看文件夹大小 1.查看当前文件夹中所有文件夹及其子文件夹的大小,注意是文件夹大小,不是文件 # du -h -rw-r--r-- 1 root root 82785865 6月 9 15:53 ...
- c++模板入门
最近使用了c++模板,觉得非常强大,只是写起来需要掌握一点技巧.大部分模板都是直接把定义写在.h头文件,并且有些人还说这样做的原因是模板不支持分编译,可是以前的编译器对模板的支持不够好吧,但是现在完全 ...
- php 生成二维码 代码示例
logo 是正方形 或者 圆形的 居多 <?php include ('phpqrcode.php'); $value = 'http://www.codesc.net ...
- db2数据导出导入
C:\Users\yexuxia>set db2instance=TCASHMAN C:\Users\yexuxia>db2(c) Copyright IBM Corporation 19 ...
- HDU-1301 Jungle Roads(最小生成树[Prim])
Jungle Roads Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total ...
- HDU - 5234 Happy birthday
Problem Description Today is Gorwin’s birthday. So her mother want to realize her a wish. Gorwin say ...