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 (<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
#include<iostream>
#include<cstdio>
#include<vector>
using namespace std;
typedef struct{
int data;
vector<int>child;
}node;
node tree[];
int N, M, width[] = {}, maxDepth = -;
void DFS(int s, int dp){
if(s > N)
return;
width[dp]++;
if(dp > maxDepth){
maxDepth = dp;
}
for(int i = ; i < tree[s].child.size(); i++){
DFS(tree[s].child[i], dp + );
}
}
int main(){
scanf("%d%d", &N, &M);
for(int i = ; i < M; i++){
int id, K, temp;
scanf("%d%d", &id, &K);
tree[id].data = id;
for(int j = ; j < K; j++){
scanf("%d", &temp);
tree[id].child.push_back(temp);
}
}
DFS(,);
int index = -, max = -;
for(int i = ; i <= maxDepth; i++){
if(width[i] > max){
max = width[i];
index = i;
}
}
printf("%d %d", max, index);
cin >> N;
return ;
}
A1094. The Largest Generation的更多相关文章
- 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 ...
- PAT甲级——A1094 The Largest Generation
A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level bel ...
- PAT_A1094#The Largest Generation
Source: PAT A1094 The Largest Generation (25 分) Description: A family hierarchy is usually presented ...
- PAT1094:The Largest Generation
1094. The Largest Generation (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
- PAT 1094 The Largest Generation[bfs][一般]
1094 The Largest Generation(25 分) A family hierarchy is usually presented by a pedigree tree where a ...
- pat1094. The Largest Generation (25)
1094. The Largest Generation (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
- PAT甲级——1094 The Largest Generation (树的遍历)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/93311728 1094 The Largest Generati ...
- PAT (Advanced Level) Practise - 1094. The Largest Generation (25)
http://www.patest.cn/contests/pat-a-practise/1094 A family hierarchy is usually presented by a pedig ...
- 1094 The Largest Generation ——PAT甲级真题
1094 The Largest Generation A family hierarchy is usually presented by a pedigree tree where all the ...
随机推荐
- npm install、npm install --save、npm install --save --dev、npm install -S、npm install -D的区别
npm install X: 会把X包安装到node_modules目录中 不会修改package.json 之后运行npm install命令时,不会自动安装X npm install X –sav ...
- 前端开发之css
<!--页面中的组成部分通常随便打开一个网页,有文字,图片,视频,表格,音频,表单(注册信息) css 属性/尺寸/边框/背景 1.css的尺寸属性,就是大小width max-width mi ...
- Java中 VO、 PO、DO、DTO、 BO、 QO、DAO、POJO的概念(转)
PO(persistant object) 持久对象 在 o/r 映射的时候出现的概念,如果没有 o/r 映射,没有这个概念存在了.通常对应数据模型 ( 数据库 ), 本身还有部分业务逻辑的处理.可以 ...
- Gatsby & React & NPX & NVM
Gatsby & React Gatsby is a blazing fast modern site generator for React. https://www.gatsbyjs.or ...
- JQ获取URL中是否含有某个字符的话,对页面进行某种操作
一.//JQ获取URL中是否含有某个字符的话,对页面进行某种操作 例:如果URL中含有xia的字符,就在页面引入一个cssvar str=window.location.href; //获取地址栏UR ...
- Python实现快速排序--数据结构
快速排序(Quick Sort) 快速排序是由东尼·霍尔所发展的一种排序算法.在平均状况下,排序n个元素要O(nlogn)次比较.在最坏状况下则需要O(n^2)次比较,但这种状况并不常见.事实上,快速 ...
- jQuery代码优化的9种方法
前面的话 本文将详细介绍jQuery代码优化的9种方法 用对选择器 在jQuery中,可以用多种选择器,选择同一个网页元素.每种选择器的性能是不一样的,应该了解它们的性能差异 1.最快的选择器:id选 ...
- Xamarin + MvvmCross 安装 Part 1
前言 最近,由于工作需要,公司准备开发移动端APP.临近年底,公司不准备大面招人,由于公司一直基于.net平台进行开发,本人自告奋勇,准备先用xamarin做下移动开发.开始了在网上不停的google ...
- Nginx 如何限制响应速度
在 location 里设置 location { set $limit_rate 1k; 表示每秒只响应1k的速度 }
- ES系列十五、ES常用Java Client API
一.简介 1.先看ES的架构图 二.ES支持的客户端连接方式 1.REST API http请求,例如,浏览器请求get方法:利用Postman等工具发起REST请求:java 发起httpClien ...