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, ...
随机推荐
- java--Aop--记录日志
package com.pt.modules.log; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; imp ...
- 12.su 命令与sudo 服务
1.su 命令:解决切换用户身份的需求,使得当前用户在不退出登录的情况下,顺畅地切换到其他用户. 比如从root 管理员切换至普通用户: [root@Centos test]# id uid=0(r ...
- VXLAN理论解析
转自:https://www.jianshu.com/p/cccfb481d548 产生背景:云计算成为企业IT建设新形态 云计算,凭借其在系统利用率高.人力/管理成本低.灵活性.可扩展性强等方面表现 ...
- UI自动化实战进阶PO设计模式
前言 经过前面的实战我们已经编写了几个测试用例,下面我们要用PO设计模式来调整我们的代码,让页面元素和测试业务进行分离,这样看起来直观而且后期的维护也方便. python有一个第三方的PO设计的库,既 ...
- AtCoder Beginner Contest 177
比赛链接:https://atcoder.jp/contests/abc177/tasks A - Don't be late #include <bits/stdc++.h> using ...
- P2805 [NOI2009]植物大战僵尸 (拓扑排序 + 最小割)
题意:N*M的矩阵 每个点上都有一颗植物 僵尸只能从每一行的最右边向左进攻 每个植物有攻击范围 可以保护在攻击范围内的植物 同时每一颗植物也保护他左边的植物 摧毁每个植物能获得价值 如果这个植物被保护 ...
- Codeforces Global Round 7 C. Permutation Partitions(组合数学)
题意: 给你 n 长全排列的一种情况,将其分为 k 份,取每份中的最大值相加,输出和的最大值和有多少种分法等于最大值. 思路: 取前 k 大值,储存下标,每两个 k 大值间有 vi+1 - vi 种分 ...
- 【bzoj 1190】梦幻岛宝珠(DP)
这题是在01背包问题的基础上,扩充了重量,需要用时间换空间. 思路: 1.仔细看题,注意到重量wi为a*2^b(a<=10,b<=30),很容易想到要按 b 分开做背包的DP.接下来的重点 ...
- 放苹果 POJ - 1664 递推
把M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法?(用K表示)5,1,1和1,5,1 是同一种分法. Input 第一行是测试数据的数目t(0 <= t < ...
- Codeforces Round #570 (Div. 3) B. Equalize Prices、C. Computer Game、D. Candy Box (easy version)、E. Subsequences (easy version)
B题题意: 给你n个物品的价格,你需要找出来一个值b,使得每一个物品与这个b的差值的绝对值小于k.找到最大的b输出,如果找不到,那就输出-1 题解: 很简单嘛,找到上下限直接二分.下限就是所有物品中最 ...