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; ...
随机推荐
- struts2 中 form-action action-form 的传参方式
1. struts2 Action获取表单提交数据 主要有三种方式: 1.1 使用ActionContext类 //获取actionContext对象 ActionContext context = ...
- 鸿蒙内核源码分析(CPU篇) | 整个内核就是一个死循环 | 祝新的一年牛气冲天 ! | v32.02
百篇博客系列篇.本篇为: v32.xx 鸿蒙内核源码分析(CPU篇) | 整个内核就是一个死循环 | 51.c.h .o 任务管理相关篇为: v03.xx 鸿蒙内核源码分析(时钟任务篇) | 触发调度 ...
- [模板]多项式全家桶小记(求逆,开根,ln,exp)
前言 这里的全家桶目前只包括了\(ln,exp,sqrt\).还有一些类似于带余数模,快速幂之类用的比较少的有时间再更,\(NTT\)这种前置知识这里不多说. 还有一些基本的导数和微积分内容要了解,建 ...
- P4240-毒瘤之神的考验【莫比乌斯反演,平衡规划】
正题 题目链接:https://www.luogu.com.cn/problem/P4240 题目大意 \(Q\)组数据给出\(n,m\)求 \[\sum_{i=1}^n\sum_{j=1}^m\va ...
- NOIP 模拟六 考试总结
T1辣鸡 T1就搞得这莫不愉快.. 大致题意是给你几个矩形,矩形覆盖的点都标记上,每个矩形无重复部分,求满足(x,y) (x+1,y+1)都标记过的点对数,范围1e9. 看起来很牛的样子,我确实也被1 ...
- After Effects 图层属性及属性组结构详解
根据结构类型的属性分类 在 After Effects 的脚本开发中,图层的属性可被区分为三种类型:PROPERTY.INDEXED_GROUP 和 NAMED_GROUP .通过使用app.proj ...
- NOIP&CSP 考前 Dev-cpp 的选项问题和考试心态
(进入考场后您将获得一个崭新的 \(Dev-cpp\),没有中文,没有编译选项,没有缺省源:我还将获得一个崭新的脑子,没有心态,没有智商,没有调试能力--) 中文 \[Step1 \] \[Step2 ...
- 用 @Value("${xxxx}")注解从配置文件读取值的用法
1. 用法: 从配置properties文件中读取init.password 的值. @Value("${init.password}") private String init ...
- C 标准库函数手册摘要
<stdlib.h> int abs( int value ); long int labs( long int value ); 返回参数的绝对值 int rand( void ); v ...
- I-Base62
I - Base62 PS:一个任意进制转换的大数问题 传送门:Base62 短除法原理: 20(10进制) => 202(3进制) 20 = (2 * 3 ^ 2 + 0 * 3 ^ 1 + ...