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)的更多相关文章

  1. PTA甲级1094 The Largest Generation (25分)

    PTA甲级1094 The Largest Generation (25分) A family hierarchy is usually presented by a pedigree tree wh ...

  2. 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 ...

  3. 1094. The Largest Generation (25)

    A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level bel ...

  4. PAT (Advanced Level) 1094. The Largest Generation (25)

    简单DFS. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> ...

  5. PAT (Advanced Level) Practise 1004 解题报告

    GitHub markdownPDF 问题描述 解题思路 代码 提交记录 问题描述 Counting Leaves (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 1600 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. [转]Xcode概览:调试应用程序

    原文网址: blog.csdn.net/fhbystudy/article/details/12856261 本文由CocoaChina翻译组成员Creolophus(github主页)翻译自苹果官方 ...

  2. boost asio one client one thread

    总结了一个简单的boost asio的tcp服务器端与客户端通信流程.模型是一个client对应一个线程.先做一个记录,后续再对此进行优化. 环境:VS2017  + Boost 1.67 serve ...

  3. 黑马SSM项目练习中的Oracle操作

  4. MD5-UTF8-大写加密

    private string GetMD5Hash(string str) { string md5Str = ""; byte[] buffer = Encoding.UTF8. ...

  5. jQuery树形控件zTree使用

    http://www.cnblogs.com/AutumnRhyme/p/5915769.html .................................................. ...

  6. postgresql实现插入数据返回当前的主键ID

    <insert id="addUser" parameterType="com.liupan.user"> <selectKey keyPro ...

  7. linux读取yaml文件的某个属性值

    trigger=$(cat test.yaml | grep "trigger" | awk '{print $2}') 该条命令的意思是:读取test.yaml文件中的trigg ...

  8. D. Time to Raid Cowavans 分块暴力,感觉关键在dp

    http://codeforces.com/contest/103/problem/D 对于b大于 sqrt(n)的,暴力处理的话,那么算出每个的复杂度是sqrt(n),就是把n分成了sqrt(n)段 ...

  9. Hive 基本语法操练(二):视图和索引操作

    1. 视图操作 ------- 1) 创建一个测试表. ``` hive> create table test(id int,name string); OK Time taken: 0.385 ...

  10. windows 安装python

    前言: Windows 中直接使用Python真的是心累 安装vs 2017(最好是最新版的, 因为python依赖于一些vs提供的包) 下载最新的python的安装程序 安装完毕之后, 不像Linu ...