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 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
这道题是2015考研机试前的那个PAT的D题 http://www.patest.cn/contests/pat-a-101-125-1-2015-03-14
那次PAT据说比机试简单。。。于是很多高分大神申请了免机试。。。性价比超高。。。。
我表示很忧伤。。。虽然我对自己机试成绩还算满意,但是还是有点感慨。。。如果参加这次PAT 说不定分数更好。。。嗯,做梦的感觉好好好哦。。。
分析:这道题需要根据指定序号成员的孩子信息(节点和节点的孩子信息)构造宗谱(树),然后找到人数(节点)最多的那一代(层)。
1. N (<100) which is the total number of family members in the tree 所以最简单暴力的矩阵表示妥妥的时空都够用。
2. find the generation with the largest population 人数最多的那一代,即节点最多的那一层,层序遍历即可
总结:本题目的考点就是简单的层序遍历
#include<cstdio> int main()
{
int family[][]={}; //从1开始 0: 存放孩子数 For the sake of simplicity, let us fix the root ID to be 01
int generation[]={}; // 当前处理的代数的人员队列 从1开始 0:此代几个人 int members=,havechildren=,father=,numchild=,child=;
scanf("%d%d",&members,&havechildren);// the total number of family members in the tree ----------------the number of family members who have children
for(int i=;i<havechildren;i++) // ID K ID[1] ID[2] ... ID[K]
{
scanf("%d %d",&father,&numchild);
family[father][]=numchild;
for(int j=;j<=numchild;j++) //K (>0) is the number of his/her children
{
scanf("%d",&child); // ID's of his/her children
family[father][j]=child;
}
} generation[]=;//the root level is defined to be 1
generation[]=;
int igenerat=,largest=,largestigenerat=,start=,len=;
while(generation[])//find the generation with the largest population
{
len=generation[];
start=len+;
generation[]=;
for(int i=;i<=len;i++)
{
father=generation[i]; //当前处理的father
numchild=family[father][];
for(int j=;j<=numchild;j++)
{
generation[start]=family[father][j];
generation[]++;
start++;
}
}//zhengli shuzu for(int i=;i<=generation[];i++) generation[i]=generation[i+len];
igenerat++;
if(generation[]>largest) largest=generation[],largestigenerat=igenerat;
} printf("%d %d",largest,largestigenerat); //the largest population number and the level of the corresponding generation
return ; }
PAT (Advanced Level) Practise - 1094. The Largest Generation (25)的更多相关文章
- PTA甲级1094 The Largest Generation (25分)
PTA甲级1094 The Largest Generation (25分) A family hierarchy is usually presented by a pedigree tree wh ...
- 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 ...
- 1094. The Largest Generation (25)
A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level bel ...
- PAT (Advanced Level) 1094. The Largest Generation (25)
简单DFS. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> ...
- PAT (Advanced Level) Practise 1004 解题报告
GitHub markdownPDF 问题描述 解题思路 代码 提交记录 问题描述 Counting Leaves (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 1600 ...
- PAT (Advanced Level) Practise - 1092. To Buy or Not to Buy (20)
http://www.patest.cn/contests/pat-a-practise/1092 Eva would like to make a string of beads with her ...
- PAT (Advanced Level) Practise - 1093. Count PAT's (25)
http://www.patest.cn/contests/pat-a-practise/1093 The string APPAPT contains two PAT's as substrings ...
- PAT (Advanced Level) Practise - 1095. Cars on Campus (30)
http://www.patest.cn/contests/pat-a-practise/1095 Zhejiang University has 6 campuses and a lot of ga ...
- 1079. Total Sales of Supply Chain (25)【树+搜索】——PAT (Advanced Level) Practise
题目信息 1079. Total Sales of Supply Chain (25) 时间限制250 ms 内存限制65536 kB 代码长度限制16000 B A supply chain is ...
随机推荐
- H5页面开发的touchmove事件
在做一屏滚动的H5页面的时候,必须移除touchmove事件,如果不移除,在安卓机上会触发微信原生的向下滚动拉出刷新.在IOS上出现上下都可以继续滑动,所以需要移除document的touchmove ...
- 在SpringBoot中使用Docker(利用dockerfile-maven-plugin插件)
周末在家做了一个实验: 将Docker通过插件的方式集成到SpringBoot中 然后通过Maven命令根据项目中的Dockerfile自动生成Docker镜像,同时将镜像推送到远程Linux服务器( ...
- HBase 相关API操练(三):MapReduce操作HBase
MapReduce 操作 HBase 在 HBase 系统上运行批处理运算,最方便和实用的模型依然是 MapReduce,如下图所示. HBase Table 和 Region 的关系类似 HDFS ...
- 解决Hadoop无法加载本地库的问题: Unable to load native-hadoop library for your platform
今天跑Hadoop程序时一直提示我无法加载本地库,然后就直接退出运行了,如下图所示. 原因是由于Apache提供的Hadoop本地库是32位的,而在64位的服务器上就会有问题,因此需要自己编译64位的 ...
- strstr strcpy 函数的实现
一. strcpy 代码实现 #include <iostream> #include <assert.h> #include <iostream> //#incl ...
- SpringBoot | 第十一章:Redis的集成和简单使用
前言 上几节讲了利用Mybatis-Plus这个第三方的ORM框架进行数据库访问,在实际工作中,在存储一些非结构化或者缓存一些临时数据及热点数据时,一般上都会用上mongodb和redis进行这方面的 ...
- mysql 链接时报错:1251-Client does not support authentication protocol requested by server
一 原因是mysql服务器要求的认证插件版本与客户端不一致造成的. 二 由于我是最新的mysql和破解版的navicat,那么就是mysql太高级了. 解决方法有两个,我毫不犹豫的选择mysql降级. ...
- 自定义控件使用GDI+绘制旋转Label文字
http://www.cnblogs.com/CUIT-DX037/ 1.添加用户控件: 2.添加代码: public partial class UcLabel : UserControl { pu ...
- Swift网络库Alamofire的导入
一.手动导入 1, 官网下载 Alamofire 2, 解压下载的文件 放入工程的顶层目录下 3, 打开工程 Add Files 4, 选中项目 TARGETS > General > E ...
- Android Service创建USB HOST通信
之前做了一个关于Android USB通信的Case,通过Android的USB总线给Zigbee供电,和板载的Zigbee(基于Zigbee的自组网)进行通信.要使用Android的USB Host ...