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甲级真题的更多相关文章

  1. PAT 甲级真题题解(63-120)

    2019/4/3 1063 Set Similarity n个序列分别先放进集合里去重.在询问的时候,遍历A集合中每个数,判断下该数在B集合中是否存在,统计存在个数(分子),分母就是两个集合大小减去分 ...

  2. PAT 甲级真题题解(1-62)

    准备每天刷两题PAT真题.(一句话题解) 1001 A+B Format  模拟输出,注意格式 #include <cstdio> #include <cstring> #in ...

  3. 1080 Graduate Admission——PAT甲级真题

    1080 Graduate Admission--PAT甲级练习题 It is said that in 2013, there were about 100 graduate schools rea ...

  4. PAT甲级真题及训练集

    正好这个"水水"的C4来了 先把甲级刷完吧.(开玩笑-2017.3.26) 这是一套"伪题解". wacao 刚才登出账号测试一下代码链接,原来是看不到..有空 ...

  5. PAT 甲级真题

    1019. General Palindromic Number 题意:求数N在b进制下其序列是否为回文串,并输出其在b进制下的表示. 思路:模拟N在2进制下的表示求法,“除b倒取余”,之后判断是否回 ...

  6. PAT甲级真题 A1025 PAT Ranking

    题目概述:Programming Ability Test (PAT) is organized by the College of Computer Science and Technology o ...

  7. Count PAT's (25) PAT甲级真题

    题目分析: 由于本题字符串长度有10^5所以直接暴力是不可取的,猜测最后的算法应该是先预处理一下再走一层循环就能得到答案,所以本题的关键就在于这个预处理的过程,由于本题字符串匹配的内容的固定的PAT, ...

  8. 1018 Public Bike Management (30分) PAT甲级真题 dijkstra + dfs

    前言: 本题是我在浏览了柳神的代码后,记下的一次半转载式笔记,不经感叹柳神的强大orz,这里给出柳神的题解地址:https://blog.csdn.net/liuchuo/article/detail ...

  9. 1022 Digital Library——PAT甲级真题

    1022 Digital Library A Digital Library contains millions of books, stored according to their titles, ...

随机推荐

  1. Django(视图)

    一个视图函数,简称视图,是一个简单的Python 函数,它接受Web请求并且返回Web响应.响应可以是一张网页的HTML内容,一个重定向,一个404错误,一个XML文档,或者一张图片. . . 是任何 ...

  2. PHP-文件、目录相关操作

    PHP-文件.目录相关操作 一  目录操作(Directory 函数允许获得关于目录及其内容的信息) 相关函数: 函数 描述 chdir() 改变当前的目录. chroot() 改变根目录. clos ...

  3. 设计模式(九)——装饰者模式(io源码分析)

    1 星巴克咖啡订单项目(咖啡馆): 1) 咖啡种类/单品咖啡:Espresso(意大利浓咖啡).ShortBlack.LongBlack(美式咖啡).Decaf(无因咖啡) 2) 调料:Milk.So ...

  4. 掌握数位dp

    最近遇到了数位dp题目,于是就屁颠屁颠的跑过来学习数位dp了~ "在信息学竞赛中,有这样一类问题:求给定区间中,满足给定条件的某个D 进制数或此类数的数量.所求的限定条件往往与数位有关,例如 ...

  5. Codeforces Round #626 Div2 D,E

    比赛链接: Codeforces Round #626 (Div. 2, based on Moscow Open Olympiad in Informatics) D.Present 题意: 给定大 ...

  6. Codeforces Global Round 7 A. Bad Ugly Numbers(数学)

    题意: 给你一个 n,输出一个 n 位不含 0 且不被任一位整除的正数. 思路: 构造 233 或 899. #include <bits/stdc++.h> using namespac ...

  7. zjnu1707 TOPOVI (map+模拟)

    Description Mirko is a huge fan of chess and programming, but typical chess soon became boring for h ...

  8. Codeforces Round #669 (Div. 2) A. Ahahahahahahahaha (构造)

    题意:有一个长度为偶数只含\(0\)和\(1\)的序列,你可以移除最多\(\frac{n}{2}\)个位置的元素,使得操作后奇数位置的元素和等于偶数位置的元素和,求新序列. 题解:统计\(0\)和\( ...

  9. docker的FAQ

    1.Docker能在非Linux平台(Windows+MacOS)上运行吗? 答:可以 2 .如何将一台宿主机的docker环境迁移到另外一台宿主机? 答:停止Docker服务,将整个docker存储 ...

  10. 深入 Python 解释器源码,我终于搞明白了字符串驻留的原理!

    英文:https://arpitbhayani.me/blogs/string-interning 作者:arpit 译者:豌豆花下猫("Python猫"公众号作者) 声明:本翻译 ...