PAT 甲级 1094 The Largest Generation
https://pintia.cn/problem-sets/994805342720868352/problems/994805372601090048
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 (<) 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 (<) 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 (>) 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
代码:
#include <bits/stdc++.h>
using namespace std; int N, M;
int vis[110], num[110];
vector<int> v[110];
int depth = -1; void dfs(int root, int step) {
if(v[root].size() == 0) {
depth = max(depth, step);
} vis[root] = 1;
num[step] ++;
for(int i = 0; i < v[root].size(); i ++) {
if(vis[v[root][i]] == 0) {
dfs(v[root][i], step + 1);
}
}
} int main() {
scanf("%d%d", &N, &M);
memset(vis, 0, sizeof(vis));
for(int i = 0; i < M; i ++) {
int id, K, x;
scanf("%d%d", &id, &K);
for(int k = 0; k < K; k ++) {
scanf("%d", &x);
v[id].push_back(x);
}
} dfs(1, 1);
int temp, ans = INT_MIN;
for(int i = 0; i <= depth; i ++) {
if(num[i] > ans) {
ans = num[i];
temp = i;
}
}
/*for(int i = 1; i <= depth; i ++)
printf("%d ", num[i]);*/
printf("%d %d\n", ans, temp);
//printf("%d\n", depth);
return 0;
}
dfs
PAT 甲级 1094 The Largest Generation的更多相关文章
- PAT甲级——1094 The Largest Generation (树的遍历)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/93311728 1094 The Largest Generati ...
- PTA甲级1094 The Largest Generation (25分)
PTA甲级1094 The Largest Generation (25分) A family hierarchy is usually presented by a pedigree tree wh ...
- PAT甲级——A1094 The Largest Generation
A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level bel ...
- 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练习——1094 The Largest Generation (25 point(s))
题目如下: #include<iostream> #include<vector> #include<algorithm> using namespace std; ...
- 1094 The Largest Generation ——PAT甲级真题
1094 The Largest Generation A family hierarchy is usually presented by a pedigree tree where all the ...
- PAT 1094 The Largest Generation[bfs][一般]
1094 The Largest Generation(25 分) A family hierarchy is usually presented by a pedigree tree where a ...
- 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 ...
- 【PAT甲级】1094 The Largest Generation (25 分)(DFS)
题意: 输入两个正整数N和M(N<100,M<N),表示结点数量和有孩子结点的结点数量,输出拥有结点最多的层的结点数量和层号(根节点为01,层数为1,层号向下递增). AAAAAccept ...
随机推荐
- nginx反向代理和tomcat集群(适用于ubutnu16.04及其centos7)
下面示例,本人亲测有效 为什么要反向代理和集群? 因为并发问题,很多请求如果全部分发给一个tomcat,一个tomcat优化最好的话,据说可达到800负载,但是面对成千上万的请求,单单一个tomcat ...
- 广州商学院Python正方教务系统爬虫(获取个人信息成绩课表修改密码)
使用python的requests库简单爬取,使用xpath解析内容 可以获取个人信息.个人照片.成绩单和课表 github地址:https://github.com/PythonerKK/GZCC- ...
- Python2.7-string模块
string模块 1.模块内定义的常数 字母(ascii_letters.letters),数字(digits.hexdigits.octdigits),空格(whitespace),大小写(asci ...
- Modelsim SE 和 Quartus II 编译器(综合器)的区别
当对目标模块进行RTL描述后,习惯先会用Modelsim做一下功能仿真.当我们写好Tensbench文件,直接在Modelsim SE中对源文件(design和Testbench)进行编译时,如果源文 ...
- 2017-2018-2 20155230《网络对抗技术》实验9:Web安全基础
实践过程记录 下载wegot并配置好java环境后 输入java -jar webgoat-container-7.0-SNAPSHOT-war-exec.jar 在浏览器输入localhost:80 ...
- Unused Method(不再使用的方法)——Dead Code(死亡代码)
系列文章目录: 使用Fortify进行代码静态分析(系列文章) Unused Method(不再使用的方法) 示例: private bool checkLevel(strin ...
- Aspose.Cells.dll的用法
public void OutExcel() { #region WorkbookDesigner designer = new WorkbookDesigner(); Worksheet sheet ...
- 矩阵乘法&&矩阵快速幂&&最基本的矩阵模型——斐波那契数列
矩阵,一个神奇又令人崩溃的东西,常常用来优化序列递推 在百度百科中,矩阵的定义: 在数学中,矩阵(Matrix)是一个按照长方阵列排列的复数或实数集合 ,最早来自于方程组的系数及常数所构成的方阵.这一 ...
- CF 961E Tufurama
JYZdalao上课讲了这道题,觉得很好可做 其实也是一道理解了就水爆了的题目 把题意抽象化,可以发现题目求的满足 i<j a[i]>=j a[j]>=i 的i,j对数.由于i,j顺 ...
- linux 定时器原理
内核定时器: unsigned long timeout = jiffies + (x * HZ); while(1) { // Check the condition. ...