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 ...
随机推荐
- jPaginate应用
分页结合bingojs需要注意两点 1.标签要放在bg-render外面 2.ajax请求参数包含一页显示多少条数据的字段,跟分页插件无关. 调用jPaginate插件的方法很简单: $('#page ...
- SQL SERVER与C#的数据类型对应表
序号 类别 SQLServer C Sharp 备注 1 整数 bit Boolean True转换为1False转换为0 2 tinyint Byte C Sharp 数据类型都位于System命名 ...
- PHP问答题大全
答案在题目后面,文字与背景同色,连续单机三次鼠标一行出答案哦: 1.PHP有几种原始数据类型,分别是什么?答:八种,分别是:int,float,string,bool,array,object,res ...
- 学习最短路建图 HUD 5521
http://acm.hdu.edu.cn/showproblem.php?pid=5521 题目大意:有n个点,m个集合,每个集合里面的点都两两可达且每条边权值都是val,有两个人A, B,A在po ...
- nginx 中文文件名显示问题
VPS论坛里已经说过设置方法,不过貌似很多人还是会遇到中文乱码的问题,Apache可以使用mod_encoding支持中文目录和文件,LNMP下Nginx其实不需要安装额外的组件即可支持中文文件名或中 ...
- Android编译环境(1) - 编译Native C的模块
Android编译环境本身比较复杂,且不像普通的编译环境:只有顶层目录下才有Makefile文件,而其他的每个component都使用统一标准的Android.mk. Android.mk文件本身是比 ...
- 在WIN7/8下把XP装入VHD (上)
系统平台:win8.1 操作目的:工作中需要使用一个只能在winxp下运行的软件,但我平时都用win8.1,也不想弄个麻烦的双系统.在无忧论坛研究了两天后找到个比较好的办法,在VHD里装个window ...
- PHP AJAX技术
AJAX 是一种在无需重新加载整个网页的情况下,能够更新部分网页的技术. AJAX 通过在后台与服务器进行少量数据交换,使网页实现异步更新.这意味着可以在不重载整个页面的情况下,对网页的某些部分进行更 ...
- ubuntu apache2 流量限制模块
mod-bw is an Apache 2 module provided to solve the problem of limiting users’ and virtual hosts’ ban ...
- android:contentDescription的作用是什么
在写Android的XML布局文件时,在ImageView或ImageButton中经常会碰到一个提示: Missing contentDescription attribute on image. ...