PTA甲级1094 The Largest Generation (25分)
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分)的更多相关文章
- 【PAT甲级】1094 The Largest Generation (25 分)(DFS)
题意: 输入两个正整数N和M(N<100,M<N),表示结点数量和有孩子结点的结点数量,输出拥有结点最多的层的结点数量和层号(根节点为01,层数为1,层号向下递增). AAAAAccept ...
- PAT甲级——1094 The Largest Generation (树的遍历)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/93311728 1094 The Largest Generati ...
- 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 ...
- 1094. The Largest Generation (25)
A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level bel ...
- PAT 甲级 1094 The Largest Generation
https://pintia.cn/problem-sets/994805342720868352/problems/994805372601090048 A family hierarchy is ...
- 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 ...
- PAT (Advanced Level) 1094. The Largest Generation (25)
简单DFS. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> ...
- 1094. The Largest Generation (25)-(dfs,树的遍历,统计每层的节点数)
题目很简单,就是统计一下每层的节点数,输出节点数最多的个数和对应的层数即可. #include <iostream> #include <cstdio> #include &l ...
- PAT练习——1094 The Largest Generation (25 point(s))
题目如下: #include<iostream> #include<vector> #include<algorithm> using namespace std; ...
随机推荐
- 『GoLang』错误处理
Go 没有像 Java 和 .NET 那样的 try/catch 异常机制:不能执行抛异常操作.但是有一套 defer-panic-and-recover 机制. Go 的设计者觉得 try/catc ...
- Redis-Cluster分片扩容
redis分片分片场景在业务量相对较小的时候,可以将所有数据都存到一台机器上,只使用redis单机模式,不存在分片问题.如果业务的数据量超过一台物理机器的内存大小时,则会面对扩展问题,需要多台机器去存 ...
- 鸿蒙内核源码分析(忍者ninja篇) | 都忍者了能不快吗 | 百篇博客分析OpenHarmony源码 | v61.02
百篇博客系列篇.本篇为: v61.xx 鸿蒙内核源码分析(忍者ninja篇) | 都忍者了能不快吗 | 51.c.h.o 编译构建相关篇为: v50.xx 鸿蒙内核源码分析(编译环境篇) | 编译鸿蒙 ...
- ThreadLocal底层
1. 首先我们来看一下他的使用 public class ThreadLocalTest { public static void main(String[] args) { MyThread thr ...
- CentOS 7.9+19c单实例静默安装
一.环境准备 二.解压文件 三.文件配置 四.安装 五.相关调整 六.打补丁 一.环境准备0.依赖包安装 rpm -q --qf '%{NAME}-%{VERSION}-%{RELEASE} (%{A ...
- Serverless 在大规模数据处理的实践
作者 | 西流 阿里云技术专家 前言 当您第一次接触 Serverless 的时候,有一个不那么明显的新使用方式:与传统的基于服务器的方法相比,Serverless 服务平台可以使您的应用快速水平扩展 ...
- Parcel Fabric Tools(宗地结构工具)
宗地结构工具 1.图层和表视图 # Process: 创建宗地结构图层 arcpy.MakeParcelFabricLayer_fabric("", 输出图层) # Process ...
- 洛谷3703 SDOI2017树点涂色(LCT+线段树+dfs序)
又一道好题啊qwqqqq 一开始看这个题,还以为是一个树剖的什么毒瘤题目 (不过的确貌似可以用树剖啊) qwq这真是一道\(LCT\)维护颜色的好题 首先,我们来一个一个操作的考虑. 对于操作\(1\ ...
- 定制input元素
定制input元素 input元素可以用来生成一个供用户输入数据的简单文本框.其缺点在于用户在其中输入什么值都可以.有时这还不错,但是有时设计者可能希望让用户输入特定类型的数据.在后一种情况下,可以对 ...
- Endian
Endian 寻址 多字节对象被存储为连续的字节序列,对象的地址为所使用字节中最小的地址. 例如,假设一个类型为 int 的变量 a 的地址为 0x100,也就是说,地址表达式 &a 的值为 ...