1094. The largest Generation (25) time limit MS Memory limit 65536 KB code length limit 16000 B procedure StandardAuthor Chen, Yue
A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level belong to the same genera tion. Your task is to find the generation with the largest population.
Input Specification:
Each input file contains the one test case. Each case starts with a positive integers N (<100) which is the total number of family members in the tree (and hence Assume that all the members were numbered from () to N), and M (<n) which was the number of family members who had chil dren. 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) are the number of his/her children, followed by a SE Quence 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 is separated by a space.
Output Specification:
For each test case, print on one line the largest population number and the level of the corresponding generation. It is assumed that such a generation are unique, and the root level was defined to be 1.
Sample Input:
23 1321 1 2301 4 03 02 04 0503 3 06 07 0806 2 12 1313 1 2108 2 15 1602 2 09 1011 2 19 2017 1 2205 1 1107 1 1409 1 1710 1 1 8
Sample Output:
9 4
Submit Code
Grandma teaches the way, by the way.
Last: Before the update, the final element of this layer
Tail: Next layer Current last element
Floor: points to the current number of layers
1#include <cstdio>2#include <algorithm>3#include <iostream>4#include <cstring>5#include <queue>6#include <vector>7#include <cmath>8 using namespacestd;9vector<int> v[ -];Ten intMain () { One intn,m; Ascanf"%d%d",&n,&m); - intI,num,count,inch; - for(i=0; i<m;i++){ thescanf"%d%d",&num,&count); - while(count--){ -scanf"%d",&inch); -V[num].push_back (inch); + } - } +queue<int>Q; AQ.push (1); at intlast=1, tail,floor=1, cur,maxcount=1, maxfloor=1, curcount=0; - while(!Q.empty ()) { -Cur=Q.front (); - Q.pop (); - for(i=0; I<v[cur].size (); i++){ - Q.push (V[cur][i]); inTail=V[cur][i]; -curcount++; to } + if(last==cur) { -last=tail; thefloor++;//Point-to- next floor * if(curcount>Maxcount) { $Maxcount=Curcount;Panax NotoginsengMaxfloor=Floor ; - } theCurcount=0; + } A } theprintf"%d%d\n", Maxcount,maxfloor); + return 0; -}
pat1094. The largest Generation (25)