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 ...
随机推荐
- Cowrie蜜罐部署教程【转载】
0.蜜罐分类: 低交互:模拟服务和漏洞以便收集信息和恶意软件,但是攻击者无法和该系统进行交互: 中等交互:在一个特有的控制环境中模拟一个生产服务,允许攻击者的部分交互: 高交互:攻击者可以几乎自由的访 ...
- js实现td排序及分组分类
如题 <!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.o ...
- js css优化-- 合并和压缩
在项目框架中,首先要引用很多css和js文件,80%的用户响应时间都是浪费在前端.而这些时间主要又是因为下载图片.样式表.JavaScript脚本.flash等文件造成的.减少这些资源文件的Reque ...
- MyBatis-执行插入语句的时候返回主键ID到传入的参数对象中
<!-- 保存项目信息 --> <insert id="saveItem" parameterType="pd" useGeneratedKe ...
- php 四种基础算法 ---- 选择排序法
2. 选择排序法: 选择排序法思路: 每次选择一个相应的元素,然后将其放到指定的位置 代码: function select_sort($arr) {//实现思路 双重循环完成,外层控制轮数,当前的最 ...
- jQuery Mobile 学习
jQuery Mobile 学习系列 http://blog.csdn.net/bao990423420/article/details/13995021
- Android内核驱动程序的编写和编译过程
注意:涉及的代码为android内核代码而不是android源码. 在智能手机时代,每个品牌的手机都有自己的个性特点.正是依靠这种与众不同的个性来吸引用户,营造品牌凝聚力和用户忠城度,典型的代表非ip ...
- Python实现删除目录下相同文件
让我们来分析一下这个问题:首先,文件个数非常多,手工查找是不现实的,再说,单凭我们肉眼,在几千张图片或文件里面找到完全相同的难度也是很大的.所以要用程序实现.那么用程序怎么实现呢?根据什么判断两个文件 ...
- div里面的margin-top失效
div标签中的元素margin-top失效的解决方法 元素上级标签是div,已经设置了width和height等的属性,可是,在对元素使用margin进行调整的时候,无法生效,下面有个不错的解决方法, ...
- html 7.29
4.请判断以下说法是否正确:HTML 会被 XHTML 取代. 您的回答:错误 正确答案:正确 9.请判断以下说法是否正确:DOCTYPE 没有关闭标签. 您的回答:错误 正确答案:正确 13.下列哪 ...