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 ...
随机推荐
- 洛谷P2516 [HAOI2010]最长公共子序列
题目描述 字符序列的子序列是指从给定字符序列中随意地(不一定连续)去掉若干个字符(可能一个也不去掉)后所形成的字符序列.令给定的字符序列X="x0,x1,-,xm-1",序列Y=& ...
- JSP-Runoob:JSP 生命周期
ylbtech-JSP-Runoob:JSP 生命周期 1.返回顶部 1. JSP 生命周期 理解JSP底层功能的关键就是去理解它们所遵守的生命周期. JSP生命周期就是从创建到销毁的整个过程,类似于 ...
- MVVMLight消息通知实现机制详解(一)
最近对委托.事件的订阅使用的太多,订阅与被订阅之间的绑定约束非常...麻烦,所以翻了下MVVMLight源码找出这段可以拿出来用的部分,详情见下: 一.开发中遇到的问题: 场景1:ClassA中存在事 ...
- 绝对牛x的代码注释
备注:文中字符均可以直接复制直接用! 再补上一个好玩的网站 Ascii World:(链接:http://www.asciiworld.com/). 网站上的图形很多,感兴趣的可以复制链接到浏览器上打 ...
- 【NOIP练习赛】学习
[NOIP练习赛]T3.学习 Description 巨弱小 D 准备学习,有 n 份学习资料给他看,每份学习资料的 内容可以用一个正整数 ai 表示.小 D 如果在一天内学习了多份资料, 他只能记住 ...
- 题解报告:hihoCoder #1174:拓扑排序·一
题目链接:https://hihocoder.com/problemset/problem/1174 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 由于今天上课的老师讲 ...
- 每天学点Linux命令之 vi 命令
来学一个vi的命令.要完成的是在一个只读文件中,删掉一行,然后插入两行. 那只读文件你要修改,用sudo vi 总可以了吧.首先 vi命令进入编辑模式. 在非插入模式中: h 光标左移 l 光标右移 ...
- 【Leetcode 220】 Contains Duplicate III
问题描述:判断数组中是否存在<ai aj> abs(ai - aj)<=t && abs(i - j) <=k: 问题分析:需要一个数据结构来维护满足条件k. ...
- SublimeText学习(一)-安装
1.下载安装包:http://www.sublimetext.com/2 2.开始安装,一直下一步 3.开始汉化 汉化包下载:http://files.cnblogs.com/files/2star/ ...
- Algebrizer
Microsoft SQL Server 2012 Internals 把 SQL 语句的处理分为四个阶段,分别是 解析.绑定.优化.执行,如图所示: 解析(Parse)主要是语法分析,比较简 ...