PAT 1094. 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
分析
用邻接表建树,层级遍历更新数据,用last,tail更新层级
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
int main(){
int n,m,father,son,num,last=1,tail,cnt=0,max=-1,level,tag=1;
cin>>n>>m;
vector<int> family[n+1];
for(int i=0;i<m;i++){
cin>>father>>num;
for(int j=0;j<num;j++){
cin>>son;
family[father].push_back(son);
}
}
queue<int> q;
q.push(1);
while(!q.empty()){
int t=q.front();
q.pop();
cnt++;
for(int i=0;i<family[t].size();i++){
q.push(family[t][i]);
tail=family[t][i];
}
if(t==last){
last=tail;
if(cnt>max){
max=cnt;
level=tag;
}
tag++;
cnt=0;
}
}
cout<<max<<" "<<level;
return 0;
}
PAT 1094. The Largest Generation (层级遍历)的更多相关文章
- PAT 1094 The Largest Generation[bfs][一般]
1094 The Largest Generation(25 分) A family hierarchy is usually presented by a pedigree tree where a ...
- PAT 1094. The Largest Generation(BFS)
CODE: #include<cstdio> #include<cstring> #include<queue> using namespace std; bool ...
- PAT甲级——1094 The Largest Generation (树的遍历)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/93311728 1094 The Largest Generati ...
- 1094 The Largest Generation ——PAT甲级真题
1094 The Largest Generation A family hierarchy is usually presented by a pedigree tree where all the ...
- PTA甲级1094 The Largest Generation (25分)
PTA甲级1094 The Largest Generation (25分) A family hierarchy is usually presented by a pedigree tree wh ...
- 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 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 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 甲级 1094 The Largest Generation
https://pintia.cn/problem-sets/994805342720868352/problems/994805372601090048 A family hierarchy is ...
随机推荐
- Linux如何使用cURL分割下载大文件
Linux如何使用cURL分割下载大文件 - 51CTO.COM http://os.51cto.com/art/201508/489368.htm
- Cocos2d-x可以实现的效果总结
Cocos2d-x可以实现的效果总结 动作(Actions) move移动:moveto/moveby 从一个位置移动到另外一个位置 从一个位置移动多少数量级 rotate旋转:rotateto/r ...
- SoapUI项目书写自我规范
-->Assertions 判断某个节点存在(_input_name, _button_name) -->Resource 文件夹命名 值去掉红色部分信息 https://mercury- ...
- Android之仿今日头条顶部导航栏效果
随着时间的推移现在的软件要求显示的内容越来越多,所以要在小的屏幕上能够更好的显示更多的内容,首先我们会想到底部菜单栏,但是有时候像今日头条新闻客户端要显示的内容太多,而且又想在主界面全部显示出来,所以 ...
- JSP-Runoob:JSP 表达式语言
ylbtech-JSP-Runoob:JSP 表达式语言 1.返回顶部 1. JSP 表达式语言 JSP表达式语言(EL)使得访问存储在JavaBean中的数据变得非常简单.JSP EL既可以用来创建 ...
- fprintf与stderr、stdout的使用
#include <stdio.h> void main() { fprintf(stderr,"soyo8888!"); fprintf(stdout,"s ...
- windows 7系统下安装SQL Server 2005图文教程
由于工作需要,今天要在电脑上安装SQL Server 2005.以往的项目都是使用Oracle,MS的数据库还真的没怎么用过,安装Oracle已经轻车熟路,但装SQL Server好像还有点小麻烦,所 ...
- yum 安装redis
1.yum install redis 2.yum install php-redis 3service redis start chkconfig redis on
- memcache缓存系统
一.缓存系统 静态web页面: 1.在静态Web程序中,客户端使用Web浏览器(IE.FireFox等)经过网络(Network)连接到服务器上,使用HTTP协议发起一个请求(Request),告诉服 ...
- synchronized关键字详解(一)
synchronized官方定义: 同步方法支持一种简单的策略防止线程干扰和内存一致性错误,如果一个对象对多个线程可见,则对该对象变量的所有读取或写入都是通过同步方法完成的(这一个synchroniz ...