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 (<) 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 (<) 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 (>) 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 <iostream>
#include <queue>
#include <vector>
using namespace std;
int N, M, maxN = , resL = , root = , level[] = { }, manN[] = { };
vector<int>man[];
void BFS()
{
queue<int>q;
q.push(root);
level[root] = ;
manN[level[root]]++;
while (!q.empty())
{
root = q.front();
q.pop();
int temp = ;
for (auto v : man[root])
{
level[v] = level[root] + ;
manN[level[v]]++;//记录每一层的人数
if (man[v].size() > )
q.push(v);
}
}
} void DFS(int s,int l)
{
manN[l]++;//l层的人数
for (auto v : man[s])
DFS(v, l + );
} int main()
{
cin >> N >> M;
for (int i = ; i < M; ++i)
{
int a, b, k;
cin >> a >> k;
for (int j = ; j < k; ++j)
{
cin >> b;
man[a].push_back(b);
}
}
//BFS();
DFS(, );
for (int i = ; i <= N; ++i)
{
if (maxN < manN[i])
{
maxN = manN[i];
resL = i;
}
}
cout << maxN << " " << resL << endl;
return ;
}

PAT甲级——A1094 The Largest Generation的更多相关文章

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

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

  2. PAT 甲级 1094 The Largest Generation

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

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

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

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

  5. A1094. The Largest Generation

    A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level bel ...

  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练习——1094 The Largest Generation (25 point(s))

    题目如下: #include<iostream> #include<vector> #include<algorithm> using namespace std; ...

  8. PAT_A1094#The Largest Generation

    Source: PAT A1094 The Largest Generation (25 分) Description: A family hierarchy is usually presented ...

  9. PAT甲级题解分类byZlc

    专题一  字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...

随机推荐

  1. HduOJ 2162 - Primes

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2161 题意:判断n是不是素数,输入到0停止.题目规定1 2 都不是素数. 题解:筛素数.老题目.不过这 ...

  2. iOS 工程实现native 跳转指定的Flutter 页面

    概要 在前一篇文章中我们提到,iOS跳转到Flutter工程指定页面时(多个),Flutter只有单例,设置setInitialRouter 无效,如下 let flutterViewControll ...

  3. iOS开发系列-UIImageView的contentMode

    typedef NS_ENUM(NSInteger, UIViewContentMode) { UIViewContentModeScaleToFill, UIViewContentModeScale ...

  4. sql(11) sum

    合计函数 (比如 SUM) 常常需要添加 GROUP BY 语句. GROUP BY 语句GROUP BY 语句用于结合合计函数,根据一个或多个列对结果集进行分组.新建表 StudentSS_id G ...

  5. js实现F5键刷新后菜单保持之前状态以及监听F5页面刷新子iframe 而父页面不刷新

    利用layui实现菜单效果时,刷新页面仍回到首页状态,需要 实现iframe子页面刷新父元素不刷新,下面是代码 //刷新时禁用F5的默认事件 $(document).keydown(function ...

  6. web移动端rem的适配

    ** 需求: 随着移动端设备的变化,内容也跟着变化.**先来说说rem单位,以rem为单位,其大小是根据根元素(html标签)的字体大小来判断的,      如 html的font-size:100p ...

  7. Markdown转义字符表

  8. python处理多线程之间事件通讯方法

    一.什么是事件 每执行一个事情,肯定有该事情的执行后状态,那事件就是该事情发生的信号 在程序中,多线程之间需要通讯,而事件就是方便线程之间的通讯 案例: 1.服务器启动需要5秒 2.客服端启动后去链接 ...

  9. iOS 5 ARC 入门

    这篇文章还可以在这里找到 英语, 波兰语 Learn the ins and outs of ARC in iOS 5! 这是iOS 5 盛宴中的第12篇教程! 这篇教程是我们的新书 iOS 5 By ...

  10. re.groups取出来的空元祖??

    源自学习笔记: day23_1_re_ groups方法取出来的字符是空的元组??为啥? ''' # ------------------------------------------------- ...