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; ...
随机推荐
- python继承细节
不要子类化内置类型 内置类型(由C语言编写)不会调用用户定义的类覆盖的特殊方法. 例如,子类化dict作为测验: class DoppeDict(dict): def __setitem__(self ...
- three.js 模型拖动之DragControls控制器
需求: 拖动场景内的模型 方案: 增加控制器DragControls 1.引入控制器 import { OrbitControls } from "three/examples/jsm/co ...
- jmeter设置为中文
我的jmeter安装路径是在D:\Jmeter\apache-jmeter-5.1.1\bin. 设置中文有2种方法: 1.第一种方法:点击jmeter.bat进入jmeter界面,点击[option ...
- Python turtle.right与turtle.setheading的区别
一.概念 turtle.right与turtle.left用法一致,我们以turtle.right为例进行讲述. turtle.right(angle)向右旋转angle角度. turtle.seth ...
- YbtOJ#893-带权的图【高斯消元,结论】
正题 题目链接:https://www.ybtoj.com.cn/problem/893 题目大意 给出一张\(n\)个点\(m\)条边的无向联通图,每条边正反向各有\(A,B,C\)三种边权. 保证 ...
- 数据库MHA故障分析
一.故障分析 1.MHA故障以后是否正常:不正常 2.如果master恢复了?MHA还能自动恢复吗?:不能 3.主从恢复删除此文件 rm saved_master_binlog_from_192 ...
- SphereEx 登陆 ApacheCon Asia|依托 ShardingSphere 可插拔架构体系打造数据应用完整生态
2021 年 8 月 8 日,ApacheCon 首次亚洲大会于线上正式闭幕.作为久负盛名的开源盛宴,本届 ApacheCon Asia 受到了海内外众多开源领域人士的关注. 作为 Apache 软件 ...
- python os.walk处理树状目录结构的文件
在项目工作中,时常需要用到处理文件的方法,尤其是在windows环境下的树状目录结构 os.walk恰好能完美的处理这种树状目录结构文件,能高效地帮助我们得到我们需要处理的文件 目录结构: Deskt ...
- 4.14——208. 实现 Trie (前缀树)
前缀树(字典树)是经典的数据结构,以下图所示: 本来处理每个节点的子节点集合需要用到set,但是因为输入规定了只有26个小写字母,可以直接用一个[26]的数组来存储. 关于ASCII代码: Java ...
- [Beta]the Agiles Scrum Meeting 9
会议时间:2020.5.24 21:00 1.每个人的工作 今天已完成的工作 成员 已完成的工作 issue yjy 撰写技术博客 tq 实现评测机获取评测状态功能 评测部分增加更多评测指标 wjx ...