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 ...
随机推荐
- 2.3.1 TextView(文本框)详解
http://www.runoob.com/w3cnote/android-tutorial-textview.html 1.基础属性详解: 通过下面这个简单的界面,我们来了解几个最基本的属性: 布局 ...
- linux 的常用命令---------第六阶段
磁盘管理 IDE 硬盘 (了解)硬盘接口 : SATA 硬盘 SCSI 硬盘 SAS 硬盘 分区付的认识:(笔试题) MBR :硬盘主引导记录,共512字节,由三部分组成 主引导程序 :占446个 ...
- Dubbo -- 系统学习 笔记 -- 目录
用户指南 入门 背景 需求 架构 用法 快速启动 服务提供者 服务消费者 依赖 必需依赖 缺省依赖 可选依赖 成熟度 功能成熟度 策略成熟度 配置 Xml配置 属性配置 注解配置 API配置 示例 启 ...
- leetcode650—2 Keys Keyboard
Initially on a notepad only one character 'A' is present. You can perform two operations on this not ...
- day71
上节回顾:(模板层) 1 模板之变量---{{ }} -支持数字,字符串,布尔类型,列表,字典---相当于对它进行了打印 -函数--->相当于加括号运行(不能传参数) -对象---& ...
- 第17章 EXTI—外部中断/事件控制器
第17章 EXTI—外部中断/事件控制器 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.co ...
- CentOS 6.8 虚拟机安装详解
第一步:安装 VMware 官方网站:www.vmware.com 下载百度云链接:http://pan.baidu.com/s/1bphDOWv 密码:0zix VMware 是一个虚拟 PC 的软 ...
- 允许Ubuntu系统下Mysql数据库远程连接
第一步: vim /etc/mysql/my.cnf找到bind-address = 127.0.0.1 注释掉这行,如:#bind-address = 127.0.0.1 或者改为: bind-ad ...
- 20155223 Exp6 信息收集与漏洞扫描
20155223 Exp6 信息收集与漏洞扫描 本次实验以熟悉信息收集手段与漏洞扫描手段为主. 实践步骤 whois域名查找 在虚拟机Kali的终端输入命令:whois baidu.com,查询百度的 ...
- MySQL清理慢查询日志slow_log的方法
一.清除原因 因为之前打开了慢查询,导致此表越来越大达到47G,导致磁盘快被占满,使用xtrabackup进行备份的时候文件也超大. mysql> show variables like 'lo ...