PAT1094:The Largest Generation
1094. The Largest Generation (25)
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 思路
图的广搜(BFS),或者可以看成树的层次遍历。仔细看 输入数据 会发现结构很像图的邻接表,所以用vector<vector<int>>模拟一个图的邻接表。所以:
1.根据数据构造图graph。
2.用队列bfs,记录下孩子最多的一层和最多孩子数。(层次的区分可以依靠插入一个label到队列区分,每当一个label出队时,表示这一层遍历完,并且这一层所有节点的孩子都刚好加入队列等待遍历,所以label出队的同时再插入一个label可以将孩子所在的层级与孩子的孩子所在的层级区分开来)
3.输出记录即可。
代码
#include<iostream>
#include<vector>
#include<queue> //Need to be optimized
using namespace std;
int maxchild = ,level = ; void bfs(int root,const vector<vector<int>>& g)
{
int curlevel = ;
maxchild = ;
int countchild = ;
int label = -;
queue<int> q;
q.push(root);
q.push(label);
while(!q.empty())
{
int f = q.front();
q.pop();
if( f == label)
{
++curlevel;
if(maxchild < countchild)
{
level = curlevel;
maxchild = countchild;
}
countchild = ;
if(q.empty()) //检查下label是不是最后一层的label
break;
q.push(label);
}
else
{
countchild++;
for(int i = ;i < g[f].size();i++)
{
q.push(g[f][i]);
}
}
}
} int main()
{
int N,M;
while(cin >> N >> M)
{
//create graph
vector<vector<int>> graph(N + );
for(int i = ; i <=M;i++)
{
int node,childcount;
cin >> node >> childcount;
vector<int> childs(childcount);
for(int j = ;j < childcount;j++)
cin >> childs[j];
graph[node] = childs;
} bfs(,graph);
cout << maxchild << " " << level;
}
}
PAT1094:The Largest Generation的更多相关文章
- pat1094. The Largest Generation (25)
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 ...
- 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 ...
- PAT_A1094#The Largest Generation
Source: PAT A1094 The Largest Generation (25 分) Description: A family hierarchy is usually presented ...
- 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 ...
- 1094. The Largest Generation (25)
A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level bel ...
- 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 ...
随机推荐
- Linux 用户打开进程数的调整
Linux 用户打开进程数的调整 参考文章: 关于RHEL6中ulimit的nproc限制(http://www.cnblogs.com/kumulinux/archive/2012/12/16/28 ...
- 《java入门第一季》之面向对象(如何使用帮助文档)
1:打开帮助文档 2:点击显示,找到索引,看到输入框 3:知道你要找谁?以Scanner举例 4:在输入框里面输入Scanner,然后回车 5:看包 java.lang包下的类不需要导入包,其他的全部 ...
- SpriteBuilder中音频波长超过Timeline结尾的情况
见如下图: 注意最后一个音频波长延续到Timeline结尾之后.表明这个音频文件播放长度超过Timeline(动画)播放的长度.这是否成为一个问题要视情况而定.而在这里无所谓. 如果节点所拥有的Tim ...
- XWork容器的存储结构
我们可以看到,在Container的默认实现,ContainerImpl中有两个实例变量.factoris和factoryNamesByType. 对象制造工厂 class ContainerImpl ...
- 深度剖析linux内核万能--双向链表,Hash链表模版
我们都知道,链表是数据结构中用得最广泛的一种数据结构,对于数据结构,有顺序存储,数组就是一种.有链式存储,链表算一种.当然还有索引式的,散列式的,各种风格的说法,叫法层出不穷,但是万变不离其中,只要知 ...
- getJSONObject与optJSONObject的区别,结合源码分析
*json解析常见问题: getJSONObject与optJSONObject的区别,下面结合源码和案例来分析当我们使用这两周方法来解析数据时,哪种比较好. 源码分析: //使用getJSONObj ...
- linux命令大全(自己慢慢看)
http://blog.zol.com.cn/874/article_873769.html rm -rf mydir /* 删除mydir目录 */ cd mydir /* 进入mydir目录 */ ...
- LeetCode之“动态规划”:Maximal Square && Largest Rectangle in Histogram && Maximal Rectangle
1. Maximal Square 题目链接 题目要求: Given a 2D binary matrix filled with 0's and 1's, find the largest squa ...
- Gradle 1.12用户指南翻译——第三十四章. JaCoCo 插件
本文由CSDN博客万一博主翻译,其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Githu ...
- linux上部署rmi+memcache服务
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/50020437 最近在学习linux上搭建Rmi+Memca ...