PAT A1094 The Largest Generation (25 分)——树的bfs遍历
A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level belong to the same generation. Your task is to find the generation with the largest population.
Input Specification:
Each input file contains one test case. Each case starts with two positive integers N (<100) which is the total number of family members in the tree (and hence assume that all the members are numbered from 01 to N), and M (<N) which is the number of family members who have children. Then M lines follow, each contains the information of a family member in the following format:
ID K ID[1] ID[2] ... ID[K]
where ID
is a two-digit number representing a family member, K
(>0) is the number of his/her children, followed by a sequence of two-digit ID
's of his/her children. For the sake of simplicity, let us fix the root ID
to be 01
. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print in one line the largest population number and the level of the corresponding generation. It is assumed that such a generation is unique, and the root level is defined to be 1.
Sample Input:
23 13
21 1 23
01 4 03 02 04 05
03 3 06 07 08
06 2 12 13
13 1 21
08 2 15 16
02 2 09 10
11 2 19 20
17 1 22
05 1 11
07 1 14
09 1 17
10 1 18
Sample Output:
9 4
#include <stdio.h>
#include <vector>
#include <queue>
using namespace std;
const int maxn=;
vector<int> fa[maxn];
int main(){
int n,m;
scanf("%d %d",&n,&m);
for(int i=;i<=m;i++){
int root,k;
scanf("%d %d",&root,&k);
for(int j=;j<k;j++){
int ch;
scanf("%d",&ch);
fa[root].push_back(ch);
}
}
queue<int> q;
q.push();
int maxm=,lvl=,max_l=;
while(!q.empty()){
queue<int> child;
int num=;
while(!q.empty()){
int now = q.front();
q.pop();
for(int i=;i<fa[now].size();i++){
child.push(fa[now][i]);
num++;
}
}
lvl++;
if(num>maxm){
maxm=num;
max_l=lvl;
}
while(!child.empty()){
q.push(child.front());
child.pop();
}
}
printf("%d %d",maxm,max_l);
}
注意点:统计每层个数,用两个队列实现,同时统计个数和层数,一层全遍历完,再把下一层加入到队列中去
PAT A1094 The Largest Generation (25 分)——树的bfs遍历的更多相关文章
- PTA甲级1094 The Largest Generation (25分)
PTA甲级1094 The Largest Generation (25分) A family hierarchy is usually presented by a pedigree tree wh ...
- 【PAT甲级】1094 The Largest Generation (25 分)(DFS)
题意: 输入两个正整数N和M(N<100,M<N),表示结点数量和有孩子结点的结点数量,输出拥有结点最多的层的结点数量和层号(根节点为01,层数为1,层号向下递增). AAAAAccept ...
- PAT A1130 Infix Expression (25 分)——中序遍历
Given a syntax tree (binary), you are supposed to output the corresponding infix expression, with pa ...
- PAT A1138 Postorder Traversal (25 分)——大树的遍历
Suppose that all the keys in a binary tree are distinct positive integers. Given the preorder and in ...
- pat1094. The Largest Generation (25)
1094. The Largest Generation (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
- PAT Advanced 1094 The Largest Generation (25) [BFS,DFS,树的遍历]
题目 A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level ...
- PAT (Advanced Level) Practise - 1094. The Largest Generation (25)
http://www.patest.cn/contests/pat-a-practise/1094 A family hierarchy is usually presented by a pedig ...
- PAT 甲级 1021 Deepest Root (25 分)(bfs求树高,又可能存在part数part>2的情况)
1021 Deepest Root (25 分) A graph which is connected and acyclic can be considered a tree. The heig ...
- PAT 1094 The Largest Generation[bfs][一般]
1094 The Largest Generation(25 分) A family hierarchy is usually presented by a pedigree tree where a ...
随机推荐
- DotNetBar的窗口样式丢失
DotNetBar的窗口样式丢失 C# 调用DotNetBar很方便,将DevComponents.DotNetBar2.dll和DevComponents.DotNetBar.Design.dll放 ...
- vue-cil和webpack中本地静态图片的路径问题解决方案
1 本地图片动态绑定img的src属性 一般我们在html中或者vue组件文件中引用图片是这样,这是不需要做特别处理的 我们将图片放入assets中或者重新建立个文件夹img什么的都可以,随意- 但是 ...
- Java面试题总结(不定期更新)
1.HashMap和Hashtable的区别? HashMap:key.value都可以为空,线程不安全.初始容量16,扩容方式每次为2倍 Hashtable:不支持null key 和null va ...
- Android内存优化(五) Lint代码扫描工具
1.使用 工具栏 -> Analyze -> Inspect Code… 点击 Inspect Code 后会弹出检查范围的对话框: 默认是检查整个项目,我们可以点击 Custom sc ...
- JHipster开发环境安装
本文演示如何在CentOS7上安装Jhipster以及其依赖组件. 这里采用官方推荐的Yarn安装方法,操作系统版本为CentOS 7.4. 1 安装JDK 推荐版本:OpenJDK 1.8.0-64 ...
- SQL Server 事务隔离级别
一.事务隔离级别控制着事务的如下表现: 读取数据时是否占用锁以及所请求的锁类型. 占用读取锁的时间. 引用其他事务修改的行的读操作是否: 在该行上的排他锁被释放之前阻塞其他事务. 检索在启动语句或事务 ...
- 用好lua+unity,让性能飞起来——关于《Unity项目常见Lua解决方案性能比较》的一些补充
<Unity项目常见Lua解决方案性能比较>,这篇文章对比了现在主流几个lua+unity的方案 http://blog.uwa4d.com/archives/lua_perf.html ...
- Yii2.0手册地址
官网打不开,可以看这里 http://yii2.techbrood.com/ ;跟官网里面文档一样.ps:今天真郁闷,官网都打不开
- 基于centOS7:新手篇→tomcat的部署方式
一.自动部署 将项目直接拷贝到webapps目录下,通过项目名直接访问 二.在server.xml中指定项目 打开Tomcat/conf/server.xml文件,在host标签中加入以下参数并重启T ...
- IDEA用maven创建springMVC项目和配置(XML配置和Java配置)
1.DEA创建项目 新建一个maven project,并且选择webapp原型. 然后点击next 这里的GroupId和ArtifactID随意填写,但是ArtifactID最好和你的项目一名一样 ...