PAT (Advanced Level) Practice 1094 The Largest Generation (25 分) 凌宸1642

PAT (Advanced Level) Practice 1094 The Largest Generation (25 分) 凌宸1642

题目描述:

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.

译:每个输入文件包含一个测试。每个用例开始为两个正整数,代表在家谱树中的成员结点的总数 N (<100)(因此所有结点按照 01 到 N 进行编号),表示拥有孩子的家族成员的个数 M (<N) 。接下来 M 行有如下的格式,包含一个家庭成员的信息:

ID K ID[1] ID[2] ... ID[K]

ID 是一个2位数,表示成员的编号, K (>0) 表示其孩子的个数 , 接下来是其孩子结点的编号 ID 序列。为了简单起见,我们规定根结点的 ID01 。所有处于同一行的数字之间都用空格隔开。


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.

译:对于每个测试用例,在一行中输出拥有人口最多的一代的人口数和代数。可以保证这样的结果是唯一的,并且根结点的代数为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

The Idea:

首先是建树,然后就是对树进行层序遍历并统计每一层的人数即可。

  • 建树,由于N的范围很小,可以直接使用结构体来表示节点。
  • 找到根节点,本题固定为 01
  • 进行层序遍历,与最基础的层序遍历不同,这里还需要填充每个结点的层数,所以在孩子结点入队之前,可以将其层数值修改为父结点的层数值 + 1。然后再判断每一层的人数时,我采用了一个表示现在位于哪一层的标志nowP ,当nowP 与当前队首元素结点的level值不一样时,说明此时是该层的第一个元素,所以这一层的人数值为 此时队列的大小值 q.size() + 1
  • 得到该层人数之后,与最大人数的那一层进行比较,决定是否更新。

The Codes:

#include<bits/stdc++.h>
using namespace std ;
struct NODE{
	int level ;
	vector<int> child;
}Node[100] ;

int n , m , k , id , t ; /
int maxP = 1 , maxG = 1 ;
void levelOrder(int root){
	queue<int> q ;
	q.push(root) ;
	int nowP = 0 , nowG = 1 ;
	while(!q.empty()){
		int top = q.front() ;
		q.pop() ;
		if(Node[top].level != nowG){
			nowG ++ ; // 进入下一层
			nowP = 1 + q.size() ; // 本层的总人数
			if(nowP > maxP){  // 判断是否需要更新
				maxP = nowP ;
				maxG = nowG ;
			}
		}
		for(int i = 0 ; i < Node[top].child.size() ; i ++){
			int t = Node[top].child[i] ;
			q.push(t) ; // 孩子结点入队
			Node[t].level = Node[top].level + 1 ; // 孩子结点的层值 = 父结点的层值 + 1
		}
	}
} 

int main(){
	cin >> n >> m ;
	for(int i = 0 ; i < m ; i ++){
		cin >> id >> k ;
		for(int j = 0 ; j < k ; j ++){
			cin >> t ;
			Node[id].child.push_back(t) ;
		}
	}
	Node[1].level = 1 ;
	levelOrder(1) ;
	cout << maxP << " " << maxG << endl ;
	return 0 ;
}

posted @ 2021-08-06 19:08  凌宸1642  阅读(36)  评论(0)    收藏  举报