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 人数最多的那一代,即节点最多的那一层,层序遍历即可

总结:本题目的考点就是简单的层序遍历

 

 1 #include<cstdio> 
 2  
 3 int main()
 4 {
 5     int family[100][100]={0}; //从1开始  0: 存放孩子数    For the sake of simplicity, let us fix the root ID to be 01
 6     int generation[150]={0}; // 当前处理的代数的人员队列   从1开始  0:此代几个人
 7     
 8     int  members=0,havechildren=0,father=0,numchild=0,child=0;
 9     scanf("%d%d",&members,&havechildren);// the total number of family members in the tree ----------------the number of family members who have children
10     for(int i=0;i<havechildren;i++) // ID K ID[1] ID[2] ... ID[K]
11     {
12             scanf("%d %d",&father,&numchild);
13             family[father][0]=numchild;
14             for(int j=1;j<=numchild;j++) //K (>0) is the number of his/her children
15             {
16                     scanf("%d",&child);  // ID's of his/her children
17                     family[father][j]=child;
18             }
19     }
20     
21     generation[1]=1;//the root level is defined to be 1
22     generation[0]=1;
23     int igenerat=1,largest=1,largestigenerat=1,start=2,len=1;
24     while(generation[0])//find the generation with the largest population 
25     {
26             len=generation[0];
27             start=len+1;      
28             generation[0]=0;
29             for(int i=1;i<=len;i++)
30             {
31                     father=generation[i]; //当前处理的father 
32                     numchild=family[father][0];
33                     for(int j=1;j<=numchild;j++)
34                     {
35                             generation[start]=family[father][j];
36                             generation[0]++;
37                             start++;
38                     }  
39             }//zhengli shuzu
40             
41             for(int i=1;i<=generation[0];i++)  generation[i]=generation[i+len];
42             igenerat++;
43             if(generation[0]>largest) largest=generation[0],largestigenerat=igenerat; 
44     }
45     
46     printf("%d %d",largest,largestigenerat); //the largest population number and the level of the corresponding generation
47     return 0;
48     
49 }

 

posted on 2015-04-30 10:04  Asin_LZM  阅读(283)  评论(0编辑  收藏  举报