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 ...
随机推荐
- 哈佛大学构建动态网站--第七讲ajax
Ajax ajax举例: DOM的结构 通过js来修改html页面. Ajax的含义: return false的用途 跨浏览器的ajax 为什么不直接从yahoo获得数据呢? XMLHttpRequ ...
- Entity Framework 学习高级篇1—改善EF代码的方法(上)
本节,我们将介绍一些改善EF代码的相关方法,如NoTracking,GetObjectByKey, Include等. l MergeOption.NoTracking 当我们只需要读 ...
- list组件
<?xml version="1.0"?> <!-- Simple example to demonstrate the Spark List component ...
- Oracle 游标及存储过程实例
/*********实例一*********/ create or replace procedure users_procedure is cursor users_cursor is select ...
- mongodb 慢SQL查询
在 MySQL中,慢查询日志是经常作为我们优化数据库的依据,那在MongoDB中是否有类似的功能呢?答案是肯定的,那就是Mongo Database Profiler.不仅有,而且还有一些比MySQL ...
- iwlist等工具的移植
http://blog.csdn.net/jk110333/article/details/8658054 参考了这边文章 -------------------------------------- ...
- 1.4 测试各阶段(单元、集成、系统 、Alpha、Beta、验收)
单元测试:单元测试是对软件基本组成单元(软件设计的最小单位)进行正确性检验的测试工作,如函数.过程(function,procedure)或一个类的方法(method).单元测试是一个方法层面上的测试 ...
- OpenGL-----深度测试,剪裁测试、Alpha测试和模板测试
片断测试其实就是测试每一个像素,只有通过测试的像素才会被绘制,没有通过测试的像素则不进行绘制.OpenGL提供了多种测试操作,利用这些操作可以实现一些特殊的效果.我们在前面的课程中,曾经提到了“深度测 ...
- KVO 进阶
Key-value coding (KVC) 和 key-value observing (KVO) 是两种能让我们驾驭 Objective-C 动态特性并简化代码的机制.在这篇文章里,我们将接触一些 ...
- HDU1879--继续畅通工程(最小生成树)
Problem Description 省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可).现得到城镇道路统计 ...