1004. Counting Leaves (30)
1004. Counting Leaves (30)
Input
Each input file contains one test case. Each case starts with a line containing 0 < N < 100, the number of nodes in a tree, and M (< N), the number of non-leaf nodes. Then M lines follow, each in the format:
ID K ID[1] ID[2] ... ID[K]
where ID is a two-digit number representing a given non-leaf node, K is the number of its children, followed by a sequence of two-digit ID's of its children. For the sake of simplicity, let us fix the root ID to be 01.
Output
For each test case, you are supposed to count those family members who have no child for every seniority level starting from the root. The numbers must be printed in a line, separated by a space, and there must be no extra space at the end of each line.
The sample case represents a tree with only 2 nodes, where 01 is the root and 02 is its only child. Hence on the root 01 level, there is 0 leaf node; and on the next level, there is 1 leaf node. Then we should output "0 1" in a line.
Sample Input
2 1
01 1 02
Sample Output
0 1 有关树的遍历的问题,用到了深搜的方法
树的存放很有意思,map<int,vector<int> >; key为某个节点的ID,vector是他的孩子ID数组
#include <iostream>
#include <map>
#include <vector> using namespace std; map<int,vector<int> > adjlist;
int levelleaves[]={}; void dfs(int node,int level){
if(adjlist[node].empty()){
levelleaves[level]++;
return;
}
vector<int>:: iterator itea = adjlist[node].begin();
for(;itea!=adjlist[node].end();itea++){
dfs(*itea,level+);
}
} int main()
{
int n,m;
cin>>n>>m;
int leaves = n-m;
for(int i=;i<m;i++){
int id1,k, id2;
cin>>id1>>k;
for(int j=;j<k;j++){
cin>>id2;
adjlist[id1].push_back(id2);
}
}
dfs(,);
int a=levelleaves[];
cout<<levelleaves[];
for(int i=;a<leaves;i++){
cout<<" "<<levelleaves[i];
a=a+levelleaves[i];
}
return ;
}
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 1004 Counting Leaves (30)(30 分)(dfs或者bfs)
1004 Counting Leaves (30)(30 分) A family hierarchy is usually presented by a pedigree tree. Your job ...
- 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分) DFS
1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- PAT 1004. Counting Leaves (30)
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family membe ...
- PAT A 1004. Counting Leaves (30)【vector+dfs】
题目链接:https://www.patest.cn/contests/pat-a-practise/1004 大意:输出按层次输出每层无孩子结点的个数 思路:vector存储结点,dfs遍历 #in ...
- 【PAT Advanced Level】1004. Counting Leaves (30)
利用广度优先搜索,找出每层的叶子节点的个数. #include <iostream> #include <vector> #include <queue> #inc ...
- PAT (Advanced Level) 1004. Counting Leaves (30)
简单DFS. #include<iostream> #include<cstring> #include<cmath> #include<algorithm& ...
- PAT甲题题解-1004. Counting Leaves (30)-统计每层叶子节点个数+dfs
统计每层的叶子节点个数建树,然后dfs即可 #include <iostream> #include <cstdio> #include <algorithm> # ...
随机推荐
- GPS部标平台的架构设计(三) 基于struts+spring+hibernate+ibatis+quartz+mina框架开发GPS平台
注意,此版本是2014年研发的基于Spring2.5和Struts2的版本,此版本的源码仍然销售,但已不再提供源码升级的服务,因为目前我们开发的主流新版本是2015-2016年近一年推出的基于spri ...
- C正则库做DNS域名验证时的性能对比
C正则库做DNS域名验证时的性能对比 本文对C的正则库regex和pcre在做域名验证的场景下做评测. 验证DNS域名的正则表达式为: "^[0-9a-zA-Z_-]+(\\.[0-9a ...
- Tomcat 常用配置及网站部署
一.同一Tomcat 多个端口部署不同的项目 在tomcat 安装目录下C:/Program Files/apache-tomcat-6.0.29/conf找到server.xml (1 ...
- CSS中隐藏内容的3种方法及属性值
CSS中隐藏内容的3种方法及属性值 (2011-02-11 13:33:59) 在制作网页时,隐藏内容也是一种比较常用的手法,它的作用一般有:隐藏文本/图片.隐藏链接.隐藏超出范围的内容.隐藏弹出 ...
- php读取excel内容
使用php读取到excel文件中的内容 1.下载PHPExcel类 2.代码: header("Content-type:text/html;charset=utf-8");req ...
- openGL漫游功能简单实现
最近弄openGL漫游功能的时候,在网上找了好多源码都没有达到预期效果,然后就自己写了一个算法分享一下. 上下键实现位移,左右键实现转动. 算法思想:由于改变观察点函数原型为: void gluLoo ...
- Jquery插件Ztree使用所遇问题
问题1.$.fn.zTree为空或为Undefined 我在MVC中引用Jquery插件Ztree的JS并不存任何问题,而当我将Ztree的js引入项目中,就出现$.fn.zTree为空或为Undef ...
- python常用小模块使用汇总
在写代码过程中常用到一些好用的小模块,现整理汇总一下: 1.获取当前的文件名和目录名,并添到系统环境变量中. file = os.path.abspath(__file__) ...
- 修改远程桌面连接端口3389,RDP-Tcp的portnumber要用十六进制修改
1. 运行regedit 2. 展开 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server\Wds\rdpwd\Tds ...
- MVC5 + EF6 简单示例
本文所使用的软件及环境: Visual Studio Ultimate 2013 (下载地址:http://www.visualstudio.com/downloads/download-visual ...