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. ios14--购物车优化2

    // // ViewController.m // 03-综合练习 // // Created by xiaomage on 15/12/28. // Copyright © 2015年 小码哥. A ...

  2. Protected vs protected internal (Again) in c#

    http://stackoverflow.com/questions/22940317/protected-vs-protected-internal-again-in-c-sharp protect ...

  3. YTU 2797: 复仇者联盟之关灯

    2797: 复仇者联盟之关灯 时间限制: 1 Sec  内存限制: 128 MB 提交: 563  解决: 160 题目描述 输入n(1~500)盏灯并编号,输入1~9(包含1和9)的数字m,灭掉编号 ...

  4. Codeforces--618A--Slime CombiningCrawling(数学)

     Slime CombiningCrawling in process... Crawling failed Time Limit:2000MS     Memory Limit:262144KB ...

  5. 必会!Linux文件的管理

    1.1 创建一个目录 /data [root@liuhao ~]# mkdir /data 1.2 查看目录是否创建成功 <可以找到data即为创建成功> [root@liuhao ~]# ...

  6. Java使用Player播放mp3

    大家平时闲了都会听听歌,散散心,于是很多人就问,在Java里边如何播放歌曲呢,唉,别说,在Java里边还真能歌曲,下面我为大家揭晓. 我们都知道Java里边做什么都需要对应的jar包,首先贴上mave ...

  7. JavaSE综合项目演练

    光阴似箭日月如梭,大家学习已经有了一段时间了,转眼间,从刚开始如何配置JDK已经到了现在快学完网络编程了.学了这么多,眼看就要进入下一个阶段了,数据库编程了,那么在进入下个阶段前,我们来完成一个综合性 ...

  8. BZOJ 4828 DP+BFS

    被一道简单BFS坑了这么长时间我也是hhh了 //By SiriusRen #include <bits/stdc++.h> using namespace std; ,,):d(D),x ...

  9. 【BZOJ3960】DZY Loves Math V(数论)

    题目: BZOJ3560 分析: orz跳瓜. 欧拉函数的公式: \[\phi(n)=n(\prod \frac{p_i-1}{p_i})\] 其中 \(p_i\) 取遍 \(n\) 的所有质因子. ...

  10. day03_12/13/2016_bean属性的设置之setter方法注入