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

✍个人博客:https://blog.csdn.net/Newin2020?spm=1011.2415.3001.5343
📚专栏地址:PAT题解集合
📝原题地址:题目详情 - 1094 The Largest Generation (pintia.cn)
🔑中文翻译:最大的一代
📣专栏定位:为想考甲级PAT的小伙伴整理常考算法题解,祝大家都能取得满分!
❤️如果有收获的话,欢迎点赞👍收藏📁,您的支持就是我创作的最大动力💪

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 N N ,以及有孩子的结点数 M M M

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

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

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

思路
  1. 因为数据范围不是很大,所以可以用一个邻接矩阵将所欲结点之间的关系存起来。
  2. 可以利用类似层次遍历的方式去计算每一层结点的数量,每遍历一层的结点时只用将该层结点的所有孩子加到下一层数组中即可,通过下一层是否还存在结点来判断是否跳出循环。
  3. 比较每一层结点数量,找到结点数量最多的那一层,并输出最终结果。
代码
#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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值