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 ...
随机推荐
- [WebShow系列] Web浏览器最大化满屏及比例缩放方法
如果要在大屏上展示,大屏所带电脑的浏览器应该处于满屏,此时就不会显示浏览器软件的边框了.个别浏览器在满屏状态下,某些边栏等还继续保留,此时应设置此浏览器的显示选项方可消除. 如果屏幕中的显示对象过小或 ...
- AddDemo教学演示
- POJ1033 Defragment
题目来源:http://poj.org/problem?id=1033 题目大意: 某操作系统的文件系统中,所有的磁盘空间被分为N个大小相等的cluster,编号1至N.每个文件占用一个或多个clus ...
- CF E .Tree with Small Distances(树上的贪心)
题意: 这是一颗有n-1条边的无向树 , 在树上加最少的边使树的1节点到其他节点的距离最多为 2 : 分析:很容易考虑的贪心的做法,但是该如何的贪心呢 ? 我一开始是打算贪心节点的儿子最多那一个 , ...
- JQuery的extend扩展方法
jQuery.extend 函数使用详解 JQuery的extend扩展方法: Jquery的扩展方法extend是我们在写插件的过程中常用的方法,该方法有一些重载原型,在此,我们一起去 ...
- redmine迁移
(redmine使用的是bitnamiredmine一键安装,环境为apache+mysql+php) 在新环境中一键安装bitnamiredmine,安装完后,执行下面操作 1.备份原环境数据库,恢 ...
- RMAN restore fails with ORA-01180: can not create datafile 1 (文档 ID 1265151.1)
http://blog.itpub.net/26655292/viewspace-2131269/ ########Q&A issue1:ORA-01180: can not create d ...
- web.xml中监听器如何顺序加载
最近用到在Tomcat服务器启动时自动加载数据到缓存,这就需要创建一个自定义的缓存监听器并实现ServletContextListener接口, 并且在此自定义监听器中需要用到Spring的依赖注入功 ...
- Linux中ext2文件系统的结构
1.ext2产生的历史 最早的Linux内核是从MINIX系统过渡发展而来的.Linux最早的文件系统就是MINIX文件系统.MINIX文件系统几乎到处都是bug,采用的是16bit偏移量,最大容量为 ...
- 使用:/usr/bin/phpize 报错
使用:/usr/bin/phpize 出现下面错误提示 Can't find PHP headers in /usr/include/php The php-devel package is requ ...