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. 关于Android模块化我有一些话不知当讲不当讲

    关于Android模块化我有一些话不知当讲不当讲 最近公司一个项目使用了模块化设计,本人参与其中的一个小模块开发,但是整体的设计并不是我架构设计的,开发半年有余,在此记录下来我的想法. 关于Andro ...

  2. 暴风魔镜SDK:MojingSDK For Unity V1.3.5112 (R).zip

    去年买了个暴风魔镜4,如今一直放在家里吃灰,这些天对Unity3D开发VR兴趣正浓,刚好公司项目不忙,花了几天玩玩暴风魔镜SDK,因为网上的资料不算多,暴风提供的文档也不太适合像我这样的Unity小白 ...

  3. Java Script 第二章.

    对象: JavaScript中的所有事物都是对象:字符串,数组,数值,函数..... JavaScript中提供多个内建对象,比如说 String,  Date,  Array等等.对象只是带有属性和 ...

  4. Django基础(2)--模板自定义标签和过滤器,模板继承 (extend),Django的模型层-ORM简介

    没整理完 昨日回顾: 视图函数: request对象 request.path 请求路径 request.GET GET请求数据 QueryDict {} request.POST POST请求数据 ...

  5. java insert mysql 中文乱码

    jdbc:mysql://192.168.1.77:3306/db360?useUnicode=true&characterEncoding=UTF-8 drop database if ex ...

  6. Json 解析Json

    1.把LitJson导入到项目里面; 2.建一个下面的脚本,不挂在游戏对象上; 3.新建下面一个脚本,挂在相机上. using System.Collections; using System.Col ...

  7. Oracle 搭建Node.js开发环境

      先决条件 安装oralce客户端驱动. 安装node.js.   创建项目 安装oracledb模块 $npm install oracledb 如果失败了,你可能要爬墙.   参考package ...

  8. Oracle存储函数jdbc调用

    package com.jckb.procedure; import java.sql.CallableStatement; import java.sql.Connection; import ja ...

  9. 项目上传至Github

    到https://github.com/ 注册用户,然后点 Start a project,创建仓库 记住这个 地址. 再去 https://git-scm.com/downloads 下载git 安 ...

  10. Android Service创建USB HOST通信

    之前做了一个关于Android USB通信的Case,通过Android的USB总线给Zigbee供电,和板载的Zigbee(基于Zigbee的自组网)进行通信.要使用Android的USB Host ...