PAT甲级——A1094 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 (<) 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-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
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int N, M, maxN = , resL = , root = , level[] = { }, manN[] = { };
vector<int>man[];
void BFS()
{
queue<int>q;
q.push(root);
level[root] = ;
manN[level[root]]++;
while (!q.empty())
{
root = q.front();
q.pop();
int temp = ;
for (auto v : man[root])
{
level[v] = level[root] + ;
manN[level[v]]++;//记录每一层的人数
if (man[v].size() > )
q.push(v);
}
}
} void DFS(int s,int l)
{
manN[l]++;//l层的人数
for (auto v : man[s])
DFS(v, l + );
} int main()
{
cin >> N >> M;
for (int i = ; i < M; ++i)
{
int a, b, k;
cin >> a >> k;
for (int j = ; j < k; ++j)
{
cin >> b;
man[a].push_back(b);
}
}
//BFS();
DFS(, );
for (int i = ; i <= N; ++i)
{
if (maxN < manN[i])
{
maxN = manN[i];
resL = i;
}
}
cout << maxN << " " << resL << endl;
return ;
}
PAT甲级——A1094 The Largest Generation的更多相关文章
- PAT甲级——1094 The Largest Generation (树的遍历)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/93311728 1094 The Largest Generati ...
- PAT 甲级 1094 The Largest Generation
https://pintia.cn/problem-sets/994805342720868352/problems/994805372601090048 A family hierarchy is ...
- PTA甲级1094 The Largest Generation (25分)
PTA甲级1094 The Largest Generation (25分) A family hierarchy is usually presented by a pedigree tree wh ...
- PAT A1094 The Largest Generation (25 分)——树的bfs遍历
A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level bel ...
- A1094. The Largest Generation
A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level bel ...
- PAT Advanced 1094 The Largest Generation (25) [BFS,DFS,树的遍历]
题目 A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level ...
- PAT练习——1094 The Largest Generation (25 point(s))
题目如下: #include<iostream> #include<vector> #include<algorithm> using namespace std; ...
- PAT_A1094#The Largest Generation
Source: PAT A1094 The Largest Generation (25 分) Description: A family hierarchy is usually presented ...
- PAT甲级题解分类byZlc
专题一 字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...
随机推荐
- ES6 学习 -- let const
看过很多大佬的ES6笔记,总结一下学习后的收获,给自己当作一个笔记用用: ES3.ES5定义变量有两种方法:var 和 function ES6定义变量有var.function.let.const等 ...
- gcc 4步编译过程
一. gcc编译过程 1. 预处理: 主要进行宏替换以及头文件的展开 gcc -E *.c -o *.i 2. 编译::编译生成汇编文件,会检查语法错误 gcc -S *.i ...
- vim 命令行模式 操作指令
复制n行: nyy 粘贴:p 剪切(删除)n行: ndd 剪切 ( 删除 ) n个字符:nx 移动光标到第一行 : gg 移动光标到最后一行 : G 设置格式 :gg=G 返回上一次操作前(撤销 ...
- Win32SDK应用程序
转自:https://blog.csdn.net/jxf_ioriyagami/article/details/1486626 1 说在前面 由于VC6及MFC的特点,我们许多人从标准C++学习 ...
- matlab 求已知概率密度函数的随机数生成
N=10000; %需要随机数的个数 a=zeros(N,1); %存放随机数的数列 n=0; f1=@(t) 1./(1.2*pi*(1+5*(t-7.3).^2)); f2=@(t) 1./(1. ...
- 新知道一个 端对端加密 Signal protocol
看 socketio Sponsors 列表中的小蓝鸟,发现网站中有使用 x-jquery-tmpl [翻译]WhatsApp 加密概述(技术白皮书) 知道一个叫 Signal 协议 的端对端加密 端 ...
- yii2.0 数据库查询操作
User::find()->all(); 此方法返回所有数据: User::findOne($id); 此方法返回 主键 id=1 的一条数据(举个例子): User:: ...
- day19 装饰器
Python之路,Day7 = Python基础7 randomwrapper 包装材料:包装纸:书皮global a 全局的(也就是,函数最外面的那个)nonlocal a 局部的,上层的函数的变量 ...
- vue cli2.x配置多环境打包
一.安装 npm install --save-dev cross-env 二.配置步骤 1.修改config下的文件 //test.env.js 'use strict' module.export ...
- selenium python bindings 写测试用例
这章总结selenium在UI测试方面的用法 import unittest from selenium import webdriver from selenium.webdriver.common ...