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 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 IDto 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 <stdio.h>
#include <vector>
#include <queue>
using namespace std;
const int maxn=;
vector<int> fa[maxn];
int main(){
int n,m;
scanf("%d %d",&n,&m);
for(int i=;i<=m;i++){
int root,k;
scanf("%d %d",&root,&k);
for(int j=;j<k;j++){
int ch;
scanf("%d",&ch);
fa[root].push_back(ch);
}
}
queue<int> q;
q.push();
int maxm=,lvl=,max_l=;
while(!q.empty()){
queue<int> child;
int num=;
while(!q.empty()){
int now = q.front();
q.pop();
for(int i=;i<fa[now].size();i++){
child.push(fa[now][i]);
num++;
}
}
lvl++;
if(num>maxm){
maxm=num;
max_l=lvl;
}
while(!child.empty()){
q.push(child.front());
child.pop();
}
}
printf("%d %d",maxm,max_l);
}
注意点:统计每层个数,用两个队列实现,同时统计个数和层数,一层全遍历完,再把下一层加入到队列中去
PAT A1094 The Largest Generation (25 分)——树的bfs遍历的更多相关文章
- PTA甲级1094 The Largest Generation (25分)
PTA甲级1094 The Largest Generation (25分) A family hierarchy is usually presented by a pedigree tree wh ...
- 【PAT甲级】1094 The Largest Generation (25 分)(DFS)
题意: 输入两个正整数N和M(N<100,M<N),表示结点数量和有孩子结点的结点数量,输出拥有结点最多的层的结点数量和层号(根节点为01,层数为1,层号向下递增). AAAAAccept ...
- PAT A1130 Infix Expression (25 分)——中序遍历
Given a syntax tree (binary), you are supposed to output the corresponding infix expression, with pa ...
- PAT A1138 Postorder Traversal (25 分)——大树的遍历
Suppose that all the keys in a binary tree are distinct positive integers. Given the preorder and in ...
- pat1094. The Largest Generation (25)
1094. The Largest Generation (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
- 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 (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 ...
- PAT 甲级 1021 Deepest Root (25 分)(bfs求树高,又可能存在part数part>2的情况)
1021 Deepest Root (25 分) A graph which is connected and acyclic can be considered a tree. The heig ...
- PAT 1094 The Largest Generation[bfs][一般]
1094 The Largest Generation(25 分) A family hierarchy is usually presented by a pedigree tree where a ...
随机推荐
- 贝塞尔曲线与CSS3动画、SVG和canvas的应用
简介 贝塞尔曲线是可以做出很多复杂的效果来的,比如弹跳球的复杂动画效果,首先加速下降,停止,然后弹起时逐渐减速的效果. 使用贝塞尔曲线常用的两个网址如下: 缓动函数:http://www.xuanfe ...
- 2018-11-06 Visual Studio Code插件-英汉词典初版发布
VS插件市场地址: 英汉词典 - Visual Studio Marketplace 开源在: program-in-chinese/vscode_english_chinese_dictionary ...
- 关于苹果延迟了App接入HTTPS服务截止日期
可参考 http://www.cocoachina.com/apple/20161223/18431.html https://developer.apple.com/news/?id=1221201 ...
- springboot 常见请求方式
转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/80404645 本文出自[赵彦军的博客] 用户模型类 package com.yib ...
- (网页)bootstrap模态框手动关闭(转)
在菜鸟教程上的bootstrap 上面的模态框上看到的: 下面是一些可与 modal() 一起使用的有用的方法. $('#identifier').modal({ keyboard: false }) ...
- Tmux 日常快捷键 及配置
使用Linux的人不管是开发人员.还是运维人员都不可避免的需要使用到终端模拟器(比如,gnome-terminal)去执行一些命令或者脚本. tmux可以将终端模拟器方便的切分为不同的小窗口如下图如示 ...
- HTTP Authentication
PS:近期看了一本思维导图的书,实践一下.
- Cas 服务器 为Service定义UI
Cas服务器允许用户为每个Service自定义登陆等UI外观,但需要尊着Cas定于的规则: 一.为Service配置theme(主题) 在<Cas 服务器 Service(Cas客户端)注册信息 ...
- scrapy入门:安装scrapy
1.安装Scrapy pip 安装: pip install scrapy 要求pip至少是18.1版本的,10.x版本会提示更新pip 更新pip命令: python -m pip install ...
- 【MM系列】SAP里批量设置采购信息记录删除标记
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP里批量设置采购信息记录删除标记 ...