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& ...
随机推荐
- POJ--Lost Cows (线段树)
题目:http://poj.org/problem?id=2182 http://acm.hdu.edu.cn/showproblem.php?pid=2711 题意:有N头牛,编号为1-- ...
- HTML_CSS使用
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- ExecutorService线程池submit的使用
有关线程池ExecutorService,只谈submit的使用 可创建的类型如下: private static ExecutorService pool = Executors.newFixedT ...
- laravel 优化小记
laravel 优化 7 Performance Optimization Tips for the Laravel Developer 运行 php artisan optimize php art ...
- bzoj1568 Blue Mary
题意:P:加入一条一次函数.Q:询问x位置的最大函数值. 标程: #include<bits/stdc++.h> using namespace std; ; int q,x,n; dou ...
- 0918CSP-S模拟测试赛后总结
14名.110分.可以算是几次大落之后的一次小小的崛起?? 然而sdfz的开挂选手AK了啊…… T2重测前rank7我就高兴地像个傻子??也不看看这次T1是个什么题. 实力还是不行.一眼秒掉了简单题, ...
- linux学习(五)-----组管理和权限管理
Linux 组基本介绍 在 linux 中的每个用户必须属于一个组,不能独立于组外.在 linux 中每个文件有所有者.所在组.其它组的概念. 1)所有者 2)所在组 3)其它组 4)改变用户所在的组 ...
- day23_2_logging
#!/usr/bin/env python# -*- coding:utf-8 -*-# ------------------------------------------------------- ...
- iframe 的那些事儿
项目中有不少地方用到iframe,今儿把使用iframe遇到的一些问题一块儿总结一下. 1.javascript监听iframe加载完成事件 iframe加载过程需要一定时间,这个加载过程常常出现白屏 ...
- vue打包后index.html界面报错
vue项目完成后,打包放到服务器上,打开index.html页面时发现一片空白并且报错 很明显是js和css引用不到. 解决办法: 修改vue项目config文件夹下面的index.js,将asset ...