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

分析

用邻接表建树,层级遍历更新数据,用last,tail更新层级

#include<iostream>
#include<vector>
#include<queue>
using namespace std;
int main(){
int n,m,father,son,num,last=1,tail,cnt=0,max=-1,level,tag=1;
cin>>n>>m;
vector<int> family[n+1];
for(int i=0;i<m;i++){
cin>>father>>num;
for(int j=0;j<num;j++){
cin>>son;
family[father].push_back(son);
}
}
queue<int> q;
q.push(1);
while(!q.empty()){
int t=q.front();
q.pop();
cnt++;
for(int i=0;i<family[t].size();i++){
q.push(family[t][i]);
tail=family[t][i];
}
if(t==last){
last=tail;
if(cnt>max){
max=cnt;
level=tag;
}
tag++;
cnt=0;
}
}
cout<<max<<" "<<level;
return 0;
}

PAT 1094. The Largest Generation (层级遍历)的更多相关文章

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

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

  2. PAT 1094. The Largest Generation(BFS)

    CODE: #include<cstdio> #include<cstring> #include<queue> using namespace std; bool ...

  3. PAT甲级——1094 The Largest Generation (树的遍历)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/93311728 1094 The Largest Generati ...

  4. 1094 The Largest Generation ——PAT甲级真题

    1094 The Largest Generation A family hierarchy is usually presented by a pedigree tree where all the ...

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

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

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

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

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

  9. PAT 甲级 1094 The Largest Generation

    https://pintia.cn/problem-sets/994805342720868352/problems/994805372601090048 A family hierarchy is ...

随机推荐

  1. string转date

    /*util-->sql*/ java.util.Date utdt; java.sql.Date sqldt =null; SimpleDateFormat simFormat = new S ...

  2. AJAX异步实现

    AJAX 最大的优点是在不重新加载整个页面的情况下,可以与服务器交换数据并更新部分网页内容. <head> <meta charset="utf-8"> & ...

  3. PCB Genesis 无需启动Xmanager图形窗口运行脚本 实现方法

    从事PCB工程行业的都知道,启动Genesis需2个exe程序(Xmanager.exe与get.exe)需一起启动才可以打开我们熟悉的软件, 而Xmanager是图形窗口是给用户UI交互使用的,如果 ...

  4. codevs3304水果姐逛街(线段数)

    3304 水果姐逛水果街Ⅰ  时间限制: 2 s  空间限制: 256000 KB  题目等级 : 钻石 Diamond   题目描述 Description 水果姐今天心情不错,来到了水果街. 水果 ...

  5. [Swift通天遁地]三、手势与图表-(12)创建复合图表:包含线性图表和柱形图表

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  6. python - list 列表推导式

    一.如有两个list,分别为: a = [1,2,3,4,5,6]b = ["a","b","c","d"," ...

  7. SQLYog 快捷键

    SQLYog常用快捷键 Ctrl+M     创建一个新的连接Ctrl+N     使用当前设置新建连接Ctrl+F4    断开当前连接 对象浏览器F5         刷新对象浏览器(默认)Ctr ...

  8. ansible基础知识

    安装ansible epel源 第一步: 下载epel源 wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel- ...

  9. SQL Server语言 函数以及SQL编程

    1.数学函数:操作一个数据,返回一个结果 --去上限: ceiling ☆select ceiling(price) from car --去下限:floor ☆select floor(price) ...

  10. 如何扒取一个网站的HTML和CSS源码

    一个好的前端开发,当看到一个很炫的页面的时候会本着学习的心态,想知道网站的源码.以下内容只是为了大家更好的学习,拒绝抄袭,支持正版. 1 首先我们要有一个chrome浏览器 2 在本地创建相关文件夹 ...