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 ...
随机推荐
- jquery——制作置顶菜单
置顶菜单: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- Raymond Mill In Lisp
Raymond Mill is suitable for producing minerals powder, which is widely used in the metallurgy, buil ...
- java里如何实现循环打印出字符或字符数组里的内容
不多说,直接上干货! java里如何实现循环打印出字符里的内容 没写,暂时不会 java里如何实现循环打印出字符数组里的内容 public class test { public static voi ...
- JavaScript 系列--JavaScript一些奇淫技巧的实现方法(一)简短的sleep函数,获取时间戳
一.前言 有些东西很好用,但是你未必知道:有些东西你可能用过,但是你未必知道原理.实现一个目的有多种途径,俗话说,条条大路通罗马.发散一下大家的思维以及拓展一下知识面. 二.实现一个简短的sleep函 ...
- Python3.5 调用Ansible 执行命令
ansible.py #!/usr/bin/env python3 # -*- coding: utf-8 -*- import os import tempfile from collections ...
- ZK数据同步
整个集群完成Learner选举后,Learner向Leader发起注册,然后进入数据同步环节,即Leader将learner上缺失的事务请求同步给Learner 1). 获取Learner状态 根据L ...
- (三)Redis两种持久化方案
Redis的持久化策略:2种 RDB方式的持久化是通过快照(snapshotting)完成的,当符合一定条件时Redis会自动将内存中的数据进行快照并持久化到硬盘.RDB是Redis默认采用的持久化方 ...
- IE浏览器与非IE浏览器JS日期兼容性问题处理
执行语句 console.log(new Date("2017-07-04 18:40").getTime()); 在IE浏览器中打印出:NAN 在非IE浏览器中打印出:14991 ...
- POI 博客总结.....
主键类: HSSFRow row1= sheet.createRow(1); row.createCell(0).setCellValue("学生编号"); row.createC ...
- qrcode.js的识别解析二维码图片和生成二维码图片
qrcode只通过前端就能生成二维码和解析二维码图片, 首先要引入文件qrcode.js,下载地址为:http://static.runoob.com/download/qrcodejs-04f46c ...