博客
关于我
【笨方法学PAT】1094 The Largest Generation (25 分)
阅读量:123 次
发布时间:2019-02-26

本文共 2280 字,大约阅读时间需要 7 分钟。

一、题目

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 1321 1 2301 4 03 02 04 0503 3 06 07 0806 2 12 1313 1 2108 2 15 1602 2 09 1011 2 19 2017 1 2205 1 1107 1 1409 1 1710 1 18

Sample Output:

9 4

二、题目大意

输入树的结点个数N,结点编号为1~N,非叶子结点个数M,然后输出M个非叶子结点格子的孩子结点的编号,求结点个数最多的一层,根结点的层号为1,输出该层的结点个数以及层号。

三、考点

树、DFS

四、注意

1、使用struct node{}建树;

2、使用vector数组,在dfs的过程中维护每个depth的节点个数;

3、遍历vector找到最大的层。

五、代码

#include
#include
using namespace std;struct node { vector
v;};vector
vec;vector
vec_dep;void dfs(int root, int depth) { vec_dep[depth]++; //leaf if (vec[root].v.size() == 0) { return; } //next for (int i = 0; i < vec[root].v.size(); ++i) { dfs(vec[root].v[i], depth + 1); } return;}int main() { //read int n, m; cin >> n >> m; vec.resize(n + 1); vec_dep.resize(n + 1, 0); //read and build tree while (m--) { int id, k; cin >> id >> k; vec[id].v.resize(k); for (int i = 0; i < k; ++i) { cin >> vec[id].v[i]; } } //dfs dfs(1, 1); //max int max_pop = 0, max_gen = 1; for (int i = 1; i <= n; ++i) { if (vec_dep[i] > max_pop) { max_pop = vec_dep[i]; max_gen = i; } } //output cout << max_pop << " " << max_gen; system("pause"); return 0;}

 

转载地址:http://ptaf.baihongyu.com/

你可能感兴趣的文章
Mysql学习总结(52)——最全面的MySQL 索引详解
查看>>
Mysql学习总结(53)——使用MySql开发的Java开发者规范
查看>>
Mysql学习总结(54)——MySQL 集群常用的几种高可用架构方案
查看>>
Mysql学习总结(55)——MySQL 语句大全再温习
查看>>
Mysql学习总结(56)——MySQL用户管理和权限设置
查看>>
Mysql学习总结(57)——MySQL查询当天、本周、本月、上周、本周、上月、距离当前现在6个月数据
查看>>
Mysql学习总结(58)——深入理解Mysql的四种隔离级别
查看>>
Mysql学习总结(59)——数据库分库分表策略总结
查看>>
Mysql学习总结(5)——MySql常用函数大全讲解
查看>>
Mysql学习总结(60)——并发量大、数据量大的互联网业务数据库设计规范总结
查看>>
Mysql学习总结(61)——MySQL优化之DBA级优化整理汇总
查看>>
Mysql学习总结(62)——MySQL连接com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link问题
查看>>
Mysql学习总结(63)——Mysql数据库架构方案选择与分析
查看>>
Mysql学习总结(64)——Mysql配置文件my.cnf各项参数解读
查看>>
Mysql学习总结(65)——项目实战中常用SQL实践总结
查看>>
Mysql学习总结(66)——设置MYSQL数据库编码为UTF-8
查看>>
Mysql学习总结(67)——MYSQL慢查询日志
查看>>
Mysql学习总结(68)——MYSQL统计每天、每周、每月、每年数据 SQL 总结
查看>>
Mysql学习总结(69)——Mysql EXPLAIN 命令使用总结
查看>>
Mysql学习总结(6)——MySql之ALTER命令用法详细解读
查看>>