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 IDto 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遍历的更多相关文章

  1. PTA甲级1094 The Largest Generation (25分)

    PTA甲级1094 The Largest Generation (25分) A family hierarchy is usually presented by a pedigree tree wh ...

  2. 【PAT甲级】1094 The Largest Generation (25 分)(DFS)

    题意: 输入两个正整数N和M(N<100,M<N),表示结点数量和有孩子结点的结点数量,输出拥有结点最多的层的结点数量和层号(根节点为01,层数为1,层号向下递增). AAAAAccept ...

  3. PAT A1130 Infix Expression (25 分)——中序遍历

    Given a syntax tree (binary), you are supposed to output the corresponding infix expression, with pa ...

  4. PAT A1138 Postorder Traversal (25 分)——大树的遍历

    Suppose that all the keys in a binary tree are distinct positive integers. Given the preorder and in ...

  5. pat1094. The Largest Generation (25)

    1094. The Largest Generation (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. PAT 1094 The Largest Generation[bfs][一般]

    1094 The Largest Generation(25 分) A family hierarchy is usually presented by a pedigree tree where a ...

随机推荐

  1. HTML5中的input type为file控件限制上传文件类型及扩展

    简单介绍 input file控件限制上传文件类型如下:1.文件类型中间用,分开:2.html和htm这样的要写成两个: 3实例: <input type="file" na ...

  2. docker swarm 集群进入某节点容器失败的原因及解决方法

    今日在自己的docker swarm 测试环境中,想进入某个节点的容器去查看下,结果进入容器失败,并且报了如下错误信息: [root@worker1 ~]# docker exec -it 9a6f6 ...

  3. Openlayer3中应用的技术

    ol3-ext有很多很丰富的效果,可以不用重复造轮子,ol3-ext示例大全:http://viglino.github.io/ol3-ext/ 在本次项目中使用到了ol3-ext的两个功能:图层管理 ...

  4. ThreadPoolExecutor 线程池的源码解析

    1.背景介绍 上一篇从整体上介绍了Executor接口,从上一篇我们知道了Executor框架的最顶层实现是ThreadPoolExecutor类,Executors工厂类中提供的newSchedul ...

  5. 浅谈Kotlin(三):类

    浅谈Kotlin(一):简介及Android Studio中配置 浅谈Kotlin(二):基本类型.基本语法.代码风格 浅谈Kotlin(三):类 浅谈Kotlin(四):控制流 前言: 已经学习了前 ...

  6. Android string资源 包含 数学符号等特殊字符 及 参数占位符

    定义:<?xml version="1.0" encoding="utf-8"?><resources>    <string n ...

  7. 2059-authentication plugin 'caching_sha2_password"cnnot bt loaded :mysql8.0数据库链接不上:

    问题:最近数据库出了问题,就重新安装了数据库8.0,8.0建立数据库时出现问题,错误提示: 2059-authentication plugin 'caching_sha2_password" ...

  8. VMware安装Centos7后有线线缆被拔出

    背景:在win10 系统中的虚机软件VMware Workstation中安装CentOS7桌面版,安装过程中没有设置网络 1.确认你win10系统打开了这两个服务:VMware DHCP Servi ...

  9. HTMLTestRunner修改成Python3版本

    修改前:HTMLTestRunner下载地址:http://tungwaiyip.info/software/HTMLTestRunner.html BSTestRunner     下载地址:htt ...

  10. 一个解决过程:Servlet [某路径xxx] in web application [/项目xxx] threw load() exception和java.lang.ClassNotFoundException XXX

    Servlet [某路径xxx] in web application [/项目xxx] threw load() exception和java.lang.ClassNotFoundException ...