【PAT甲级 - C++题解】1094 The Largest Generation

简介: 【PAT甲级 - C++题解】1094 The Largest Generation

1094 The Largest Generation


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

题意


这道题意思是相当于给定一棵树,让我们在这棵树中找到结点数最多的那一层,然后输出那一层的结点数已经是第几层。


输入的第一行是总结点数 N NN ,以及有孩子的结点数 M MM 。


接下来 M MM 行的输入格式为:

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


其中 ID 表示结点编号,K 表示该结点有多少个孩子,后面再跟 K 个孩子的编号。

思路


因为数据范围不是很大,所以可以用一个邻接矩阵将所欲结点之间的关系存起来。

可以利用类似层次遍历的方式去计算每一层结点的数量,每遍历一层的结点时只用将该层结点的所有孩子加到下一层数组中即可,通过下一层是否还存在结点来判断是否跳出循环。

比较每一层结点数量,找到结点数量最多的那一层,并输出最终结果。


代码

#include<bits/stdc++.h>
using namespace std;
const int N = 110;
bool g[N][N];
vector<int> level[N];
int n, m;
int main()
{
    //输入结点信息
    cin >> n >> m;
    for (int i = 0; i < m; i++)
    {
        int id, k;
        cin >> id >> k;
        while (k--)
        {
            int son;
            cin >> son;
            g[id][son] = true;
        }
    }
    level[1].push_back(1);  //默认根结点为1
    int l = 1;
    while (level[l].size())  //类似于层次遍历
    {
        for (auto& node : level[l])   //遍历当层的所有结点
            for (int i = 1; i <= n; i++)   //将当前结点的所有孩子推入下一层
                if (g[node][i])  level[l + 1].push_back(i);
        l++;    //继续遍历下一层
    }
    //找到结点数最多的那一层
    int k = 1;
    for (int i = 1; i < l; i++)
        if (level[i].size() > level[k].size())
            k = i;
    cout << level[k].size() << " " << k << endl;
    return 0;
}


目录
相关文章
|
C++
【PAT甲级 - C++题解】1040 Longest Symmetric String
【PAT甲级 - C++题解】1040 Longest Symmetric String
66 0
|
算法 C++
【PAT甲级 - C++题解】1044 Shopping in Mars
【PAT甲级 - C++题解】1044 Shopping in Mars
82 0
|
C++
【PAT甲级 - C++题解】1117 Eddington Number
【PAT甲级 - C++题解】1117 Eddington Number
76 0
|
存储 C++ 容器
【PAT甲级 - C++题解】1057 Stack
【PAT甲级 - C++题解】1057 Stack
76 0
|
存储 C++
【PAT甲级 - C++题解】1055 The World‘s Richest
【PAT甲级 - C++题解】1055 The World‘s Richest
78 0
|
C++
【PAT甲级 - C++题解】1051 Pop Sequence
【PAT甲级 - C++题解】1051 Pop Sequence
79 0
|
人工智能 BI C++
【PAT甲级 - C++题解】1148 Werewolf - Simple Version
【PAT甲级 - C++题解】1148 Werewolf - Simple Version
134 0
|
存储 定位技术 C++
【PAT甲级 - C++题解】1091 Acute Stroke
【PAT甲级 - C++题解】1091 Acute Stroke
58 0
|
存储 人工智能 C++
【PAT甲级 - C++题解】1085 Perfect Sequence
【PAT甲级 - C++题解】1085 Perfect Sequence
71 0
|
C++
【PAT甲级 - C++题解】1046 Shortest Distance
【PAT甲级 - C++题解】1046 Shortest Distance
65 0