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.
Each input file contains one test case. Each case starts with two positive integers N (<) 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 (<) 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
(>) is the number of his/her children, followed by a sequence of two-digitID
‘s of his/her children. For the sake of simplicity, let us fix the rootID
to be01
. All the numbers in a line are separated by a space.
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.
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
9 4
1 /* 2 time: 2019-06-28 14:44:36 3 problem: PAT_A1094#The Largest Generation 4 AC: 18:52 5 6 题目大意: 7 求树中结点个数最多的层次及其结点个数 8 9 基本思路: 10 层次遍历,记录结点层次并统计各层次人数即可 11 */ 12 #include<cstdio> 13 #include<vector> 14 #include<queue> 15 using namespace std; 16 const int M=110; 17 vector<int> tree[M]; 18 int hight[M]={0},num[M]={0},maxP=0,maxL; 19 20 void Travel(int root) 21 { 22 hight[root]=1; 23 queue<int> q; 24 q.push(root); 25 while(!q.empty()) 26 { 27 root = q.front(); 28 num[hight[root]]++; 29 if(num[hight[root]] > maxP) 30 { 31 maxP = num[hight[root]]; 32 maxL = hight[root]; 33 } 34 q.pop(); 35 for(int i=0; i<tree[root].size(); i++) 36 { 37 int kid = tree[root][i]; 38 hight[kid] = hight[root]+1; 39 q.push(kid); 40 } 41 } 42 43 } 44 45 int main() 46 { 47 #ifdef ONLINE_JUDGE 48 #else 49 freopen("Test.txt", "r", stdin); 50 #endif // ONLINE_JUDGE 51 52 int n,m,id,k,kid; 53 scanf("%d%d", &n,&m); 54 for(int i=0; i<m; i++) 55 { 56 scanf("%d%d", &id,&k); 57 for(int j=0; j<k; j++) 58 { 59 scanf("%d", &kid); 60 tree[id].push_back(kid); 61 } 62 } 63 Travel(1); 64 printf("%d %d", maxP,maxL); 65 66 return 0; 67 }
PAT_A1094#The Largest Generation
原文:https://www.cnblogs.com/blue-lin/p/11102780.html