PTA甲级1094 The Largest Generation (25分)

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

【程序思路】

定义一个结构体,保存每一行输入的数据。结构体中的height记录该节点所在的高度。
然后利用队列层序遍历树,定义数组c,索引为高度,值为该高度的节点个数。最后遍历数组c找到最大值的下标,再输出。

【程序实现】

#include<bits/stdc++.h>
using namespace std;
struct tree{
int *child = NULL;
int height, k;
}Tree[105] , t;
int main(){
int n, m, x, k, c[105] = {0 , 1}, index = 0;
queue<struct tree> q;
cin>>n>>m;
for(int i = 0; i < m; i++) {
cin>>x>>k;
Tree[x].child = new int[k];
Tree[x].k = k;
for(int j = 0; j < k; j++)
cin>>Tree[x].child[j];
}
Tree[1].height = 1;
q.push(Tree[1]);
while(!q.empty()) {
t = q.front();
q.pop();
if (t.child != NULL) {
for (int i = 0; i < t.k; i++) {
Tree[t.child[i]].height = t.height + 1;
c[t.height + 1] ++;
q.push(Tree[t.child[i]]);
}
}
}
for (int i = 1; i < 105; i++)
if (c[i] > c[index])
index = i;
cout<<c[index]<<' '<<index;
return 0;
}

PTA甲级1094 The Largest Generation (25分)的更多相关文章

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

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

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

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

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

  4. 1094. The Largest Generation (25)

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

  5. PAT 甲级 1094 The Largest Generation

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

  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) 1094. The Largest Generation (25)

    简单DFS. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> ...

  8. 1094. The Largest Generation (25)-(dfs,树的遍历,统计每层的节点数)

    题目很简单,就是统计一下每层的节点数,输出节点数最多的个数和对应的层数即可. #include <iostream> #include <cstdio> #include &l ...

  9. PAT练习——1094 The Largest Generation (25 point(s))

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

随机推荐

  1. centos7.5 SVN 搭建

    第一步:通过yum命令安装svnserve,命令如下: >yum -y install subversion 此命令会全自动安装svn服务器相关服务和依赖,安装完成会自动停止命令运行 若需查看s ...

  2. python序列的修改、散列和切片

    新Vector类 接原vector类定义的新Vector类,原向量类是二维,现定义多维向量类: from array import array import reprlib import math c ...

  3. lua常用一些功能

    lua在openresty中获取请求的请求头 local head_table = ngx.req.get_headers() 由于table类型,无法直接使用ngx.say()显示,需要转换为cjs ...

  4. selenium--常用的获取元素属性

    当我们要设计功能测试用例时,一般会有预期结果,有些预期结果测试人员无法通过肉眼进行判断的.因为自动化测试运行过程是无人值守,一般情况下,脚本运行成功,没有异样信息就标识用户执行成功.那怎么才能知道我打 ...

  5. Spring,IOC源码分析

    有错勿喷 1.首先是Spring,IOC的基本概念 IOC是一个容器 容器启动的时候创建所有单实例对象 我们可以直接从容器中获取到这个对象 2.调试流程 ioc容器的启动过程?启动期间都做了什么(什么 ...

  6. php flush() 页面缓冲及时输出 每隔一秒输出页面输出

    <?php //方案一 ob_end_clean(); echo str_pad('', 1024); // 设置足够大,大过php.ini的output_buffering设置值 for ($ ...

  7. MyBatis的缓存玩法

    重要概念 SqlSession:代表和数据库的一次会话,提供了操作数据库的方法. MappedStatement:代表要发往数据执行的命令,可以理解为SQL的抽象表示. Executor:和数据库交互 ...

  8. CSS3思维导图

  9. caffe.cpp解析

    来自链接:http://blog.csdn.net/u014114990/article/details/47747025 主要讲解:GetBrewFunction()函数定义如下,其返回BrewFu ...

  10. md5验证文件上传,确保信息传输完整一致

    注:因为是公司项目,仅记录方法和思路以及可公开的代码. 最近在公司的项目中,需要实现一个上传升级包到服务器的功能: 在往服务器发送文件的时候,需要确保 文件从开始发送,到存入服务器磁盘的整个传输的过程 ...