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 ...
随机推荐
- php 去除数组中指定的值
方法1: //去除值为"Cat"的元素 $a=array("a"=>"Dog","b"=>"Cat ...
- linux 的常用命令---------第七阶段
LVM 逻辑卷管理器 -----其作用为 :在线扩容 卷组 vG (也叫LVM卷组) ------------------→ 在此卷组vG上建立 : 逻辑卷组 LV ( ...
- java.lang.NoSuchMethodError: No static method getFont
最近在Android Studio升级3.0后,在AlertDialog弹窗时报出了如下问题: java.lang.NoSuchMethodError: No static method getFon ...
- Fiddler抓包调试前端脚本代码
0.写在前面的话 之前看了阮一峰老师关于互联网协议入门的博客,受益匪浅,接着再去体会了下HTTP协议,就想着看实际网络访问中的那些HTTP请求头和响应是什么样的.Chrome的调试工具的Network ...
- ASP.NET Core MVC 模型绑定 (转载)
ASP.NET Core MVC的Model Binding会将HTTP Request数据,以映射的方式对应到参数中.基本上跟ASP.NET MVC差不多,但能Binding的来源更多了一些.本篇将 ...
- 编写Linux脚本
下面是重新启动Linux下某进程的shell脚本.以tomcat进程为例: #!/bin/sh pid=`ps -ef|grep tomcat|grep -v grep|awk '{print $2} ...
- Regionserver启动后又关闭
今天启动hbase shell,输入hbase命令时报错: ERROR [regionserver/regionserver1/172.18.0.61:16020] reggionserver.HRe ...
- 番外篇:因为一个固态导致的——系统重装与JAVA软件环境下载安装配置
第一步:拆机改装 最近想换个固态硬盘提提速度,就买了个,然后拧下后盖螺丝,将键盘盖拿下,中间有两个根线连着把扣打开就可以了,将新的固态硬盘装到原本的机械硬盘的地方,又买了个光驱托盘改装位将光驱位装上了 ...
- WPF应用
代码 private void button1_Click(object sender, RoutedEventArgs e) { calculate sa = new calculate(int.P ...
- 汇编 fsub ,fmul,fdiv,fild,CVTTPS2PI 指令
知识点: 浮点指令 fsub 一.浮点指令fsub 格式 fsub memvar // st0=st0-memvar 知识点: 浮点指令 fmul 一.浮点指令fmul 格式 fmul mem ...