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. jQuery效果之雪花飘落

    实现思路 1.在一定的频率下在页面中生成一定数目的雪花从上往下飘落: 2.在指定的时间内飘落后移除页面: 3.可设置雪花的大小,在一定范围内随机雪花大小: 4.什么时间后清除生成雪花,停止函数. js ...

  2. ssh多台主机实现互相认证

    一.主机情况 如下图所示,集群一共11台机器.编辑每台主机的hosts文件,添加如下内容,方便统一管理. 10.202.62.60 hadoop60 10.202.62.61 hadoop61 10. ...

  3. Java根据年龄段获取对应年份起始时间戳和最终时间戳、根据生日时间戳获取月份(与数据库的时间戳处理成的月份拼接成SQL条件)

    1.传入年龄段,两个值,一个最小值,一个最大值,然后获取该年龄段的两个时间戳: (1)处理时间方法: /** * 根据年龄获取时间戳(开始年龄key取0,返回一年最后一秒时间戳,时间戳大:反之结束年龄 ...

  4. Testlink1.9.17使用方法(第七章 测试用例集管理)

    第七章 测试用例集管理 QQ交流群:585499566 测试用例准备好以后,可以对测试用例集进行相关的操作. 一. 添加测试用例到测试计划中 在主页的“当前测试计划”下拉列表里-->选择一个测试 ...

  5. java基础(五) String性质深入解析

    引言   本文将讲解String的几个性质. 一.String的不可变性   对于初学者来说,很容易误认为String对象是可以改变的,特别是+链接时,对象似乎真的改变了.然而,String对象一经创 ...

  6. Django Admin后台管理用户密码修改

    方法一 在Terminal中执行:python manage.py changepassword your_name(其中“your_name”为你要修改密码的用户名),根据提示内容修改即可. 方法二 ...

  7. MySQL 博客文章目录(2017-02-18更新)

    1MySQL安装配置 Linux MySQL源码安装缺少ncurses-devel包 Linux平台卸载MySQL总结 Linux 卸载mysql-libs包出现错误 CentOS 7 安装MySQL ...

  8. c# 建立到数据源的连接 以及获取项目配置文件的属性

    两种连接数据库的写法: <connectionStrings> <add name="HRModelsContainer" connectionString=&q ...

  9. Vue2 学习笔记4

    文中例子代码请参考github 父组件向子组件传值 组件实例定义方式,注意:一定要使用props属性来定义父组件传递过来的数据 <script> // 创建 Vue 实例,得到 ViewM ...

  10. C# DBHelper类 参考

    using System;using System.Collections.Generic;using System.Text;using System.Configuration;using Sys ...