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& ...
随机推荐
- 针对Java集合类的小总结
Java集合类包位于java.util下,有很多常用的数据结构:数组.链表.队列.栈.哈希表等等.了解不同的集合类的特性在开发过程中是比较重要的,感谢@兰亭风雨的专栏分析,这里我也根据自己的理解做轻度 ...
- 多个for循环使用
for循环 例子 语法 vue.js的for循环 <div id="myfor"><li v-for="student in studentList&q ...
- amaze UI(mark)
为移动而生 Amaze UI 以移动优先(Mobile first)为理念,从小屏逐步扩展到大屏,最终实现所有屏幕适配,适应移动互联潮流. 组件丰富,模块化 Amaze UI 含近 20 个 CSS ...
- Vultr IP被墙该怎么办
我们创建好Vultr服务器时候,首先需要检测IP地址是否可用,很多IP在国内被墙,导致使用Xshell连接不上,虽然IP能够ping通,但是SSH依然连接不上.那Vultr IP被墙该怎么办呢? 方法 ...
- SQLSTATE[HY000]: General error: 1366 Incorrect string value
在Laravel项目的 storages/logs/Laravel.log看到的错误信息片段: SQLSTATE[HY000]: General error: 1366 Incorrect strin ...
- canvas基础用法
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- openwrt_ipsec_racoon.init 分析
racoon.init 脚本分析,基于openwrt 官方的脚本分析 #!/bin/sh /etc/rc.common # 包含了文件, 这个会继续分析 # # Copyright (C) Vital ...
- 调整VMware中虚拟机中Linux系统屏幕分辨率的问题
即:解决显示底部菜单栏以及安装软件中没有下一步的按钮用Tab键的问题 右击打开终端,输入以下命令: Xrandr Xrandr -s 1280x720 (自己选择一个分辨率即可) 原文:https: ...
- layui点击按钮自动刷新页面问题
问题 <button class="layui-btn layui-btn-primary" onclick="findData()">查询< ...
- jquery学习笔记(三):事件和应用
内容来自[汇智网]jquery学习课程 3.1 页面加载事件 在jQuery中页面加载事件是ready().ready()事件类似于就JavaScript中的onLoad()事件,但前者只要页面的DO ...