1094 The Largest Generation ——PAT甲级真题
1094 The Largest Generation
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
题目大意:让你统计一颗树每层节点的最大数目,以及相应层次。
大致思路:用BFS统计层序遍历每一层节点,同时定义一个数组level用来记录每一层结点的高度,其中level(孩子节点) = level(父节点) + 1。定义一个数组cnt用来统计每一层结点的个数。
代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
vector<int> root[N];
int n, m;
int ans1, ans2;
int level[N], cnt[N]; //统计每一层树的高度和每一层的节点数
void BFS(int x) {
queue<int> q;
q.push(x);
level[1] = 1;
ans1 = 1, ans2 = 1;
while (!q.empty()) {
int t = q.front();
q.pop();
cnt[level[t]]++;
// cout << cnt[level[t]] << endl;
for (int i = 0; i < root[t].size(); i++) {
level[root[t][i]] =
level[t] + 1; //孩子结点的层数等于父结点层数 + 1
q.push(root[t][i]);
}
}
for (int i = 1; i <= n; i++) {
// cout << cnt[level[i]] << endl;
if (ans1 < cnt[level[i]]) {
ans1 = cnt[level[i]];
ans2 = level[i];
}
}
cout << ans1 << " " << ans2 << endl;
}
int main() {
scanf("%d %d", &n, &m);
memset(level, 0, sizeof(level));
memset(cnt, 0, sizeof(cnt));
for (int i = 0; i < m; i++) {
int id, k;
scanf("%d %d", &id, &k);
for (int j = 0; j < k; j++) {
int x;
scanf("%d", &x);
root[id].push_back(x);
}
}
BFS(1);
return 0;
}
1094 The Largest Generation ——PAT甲级真题的更多相关文章
- PAT 甲级真题题解(63-120)
2019/4/3 1063 Set Similarity n个序列分别先放进集合里去重.在询问的时候,遍历A集合中每个数,判断下该数在B集合中是否存在,统计存在个数(分子),分母就是两个集合大小减去分 ...
- PAT 甲级真题题解(1-62)
准备每天刷两题PAT真题.(一句话题解) 1001 A+B Format 模拟输出,注意格式 #include <cstdio> #include <cstring> #in ...
- 1080 Graduate Admission——PAT甲级真题
1080 Graduate Admission--PAT甲级练习题 It is said that in 2013, there were about 100 graduate schools rea ...
- PAT甲级真题及训练集
正好这个"水水"的C4来了 先把甲级刷完吧.(开玩笑-2017.3.26) 这是一套"伪题解". wacao 刚才登出账号测试一下代码链接,原来是看不到..有空 ...
- PAT 甲级真题
1019. General Palindromic Number 题意:求数N在b进制下其序列是否为回文串,并输出其在b进制下的表示. 思路:模拟N在2进制下的表示求法,“除b倒取余”,之后判断是否回 ...
- PAT甲级真题 A1025 PAT Ranking
题目概述:Programming Ability Test (PAT) is organized by the College of Computer Science and Technology o ...
- Count PAT's (25) PAT甲级真题
题目分析: 由于本题字符串长度有10^5所以直接暴力是不可取的,猜测最后的算法应该是先预处理一下再走一层循环就能得到答案,所以本题的关键就在于这个预处理的过程,由于本题字符串匹配的内容的固定的PAT, ...
- 1018 Public Bike Management (30分) PAT甲级真题 dijkstra + dfs
前言: 本题是我在浏览了柳神的代码后,记下的一次半转载式笔记,不经感叹柳神的强大orz,这里给出柳神的题解地址:https://blog.csdn.net/liuchuo/article/detail ...
- 1022 Digital Library——PAT甲级真题
1022 Digital Library A Digital Library contains millions of books, stored according to their titles, ...
随机推荐
- H3C交换机堆叠
(1) 配置Device A# 将用作IRF物理端口的Ten-GigabitEthernet1/0/1-Ten-GigabitEthernet1/0/4的手工关闭.使用端口批量配置功能可以更 ...
- f5添加多个vlan的方法
1.方法一 方法二: F5不更改配置,核心添加路由 ip route 10.160.101.0 255.255.255.0 10.160.100.10
- 手把手教你在容器服务 TKE 中使用动态准入控制器
在 TKE 中使用动态准入控制器 原理概述 动态准入控制器 Webhook 在访问鉴权过程中可以更改请求对象或完全拒绝该请求,其调用 Webhook 服务的方式使其独立于集群组件,具有非常大的灵活性, ...
- shell脚本的使用该熟练起来了,你说呢?(篇四)
继续前一篇的文章: shell脚本的使用该熟练起来了,你说呢?(篇一) shell脚本的使用该熟练起来了,你说呢?(篇二) shell脚本的使用该熟练起来了,你说呢?(篇三) 文章里面测试的命令脚本文 ...
- unix环境高级编程第三章笔记
文件描述符 1.文件描述符的概念 对于内核而言,所有打开的文件都会用一个文件描述符来引用,打开或和创建一个新文件的时候,内核会给进程返回一个文件描述符,而当使用read write时,可以使用这个文件 ...
- Codeforces Global Round 11 C. The Hard Work of Paparazzi(dp/最长上升子序列)
题目链接:https://codeforces.com/contest/1427/problem/C 题意 \(r\) 行与 \(r\) 列相交形成了 \(r \times r\) 个点,初始时刻记者 ...
- 【noi 2.6_9265】取数游戏(DP)
题意:从自然数1到N中不取相邻2数地取走任意个数,问方案数. 解法:f[i][1]表示在前i个数中选了第i个的方案数,f[i][0]表示没有选第i个.f[i][1]=f[i-1][0]; f[i][ ...
- Docker配置文件deamon.json详解
vim /etc/docker/daemon.json { "authorization-plugins": [], "data-root": "&q ...
- Cobbler自定义安装系统和私有源
1.自定义安装系统(根据mac地址) --name=定义名称 --mac=客户端的mac地址 --ip-address=需求的ip --subnet=掩码 --gateway=网关 --interfa ...
- K8S(06)web管理方式-dashboard
K8S的web管理方式-dashboard 目录 K8S的web管理方式-dashboard 1 部署dashboard 1.1 获取dashboard镜像 1.1.1 获取1.8.3版本的dsash ...