1094. The Largest Generation (25)

时间限制
200 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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的更多相关文章

  1. pat1094. The Largest Generation (25)

    1094. The Largest Generation (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...

  2. PAT 1094 The Largest Generation[bfs][一般]

    1094 The Largest Generation(25 分) A family hierarchy is usually presented by a pedigree tree where a ...

  3. PAT甲级——1094 The Largest Generation (树的遍历)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/93311728 1094 The Largest Generati ...

  4. 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 ...

  5. PAT_A1094#The Largest Generation

    Source: PAT A1094 The Largest Generation (25 分) Description: A family hierarchy is usually presented ...

  6. 1094 The Largest Generation ——PAT甲级真题

    1094 The Largest Generation A family hierarchy is usually presented by a pedigree tree where all the ...

  7. PTA甲级1094 The Largest Generation (25分)

    PTA甲级1094 The Largest Generation (25分) A family hierarchy is usually presented by a pedigree tree wh ...

  8. 1094. The Largest Generation (25)

    A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level bel ...

  9. 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 ...

随机推荐

  1. 数据结构是哈希表(hashTable)

    哈希表也称为散列表,是根据关键字值(key value)而直接进行访问的数据结构.也就是说,它通过把关键字值映射到一个位置来访问记录,以加快查找的速度.这个映射函数称为哈希函数(也称为散列函数),映射 ...

  2. ping通windows下虚拟机上的linux系统

    直接ping  linux的ip 直接就失败了. 现在我的windows有两个虚拟网卡 接下来让linux使用VMnet8网卡 修改我的linux系统下的lo网卡的ip地址为VMnet8的ip地址 现 ...

  3. 加载SpriteBuilder中的scene为何不能带后缀

    我们在Xcode中切换SpriteBuilder中的scene时,一般使用的是如下代码: -(void)exitButtonPressed:(CCControl*)sender{ CCLOG(@&qu ...

  4. C语言获取系统时间的函数

    在标准C编程中,我们如何写程序来获取当前系统的时间呢? 获取系统时间的函数 #include <time.h> time_t  time(time_t  *t) 参数:如果参数不为空,那么 ...

  5. VS2005工程的Device右边内容为空问题

    VS2005工程的Device右边内容为空问题 可能是刚刚在删除C盘一些文件或是这些文件因为某些原因丢失了,在打开WINCE6.0系统工程的时候,发现无法编译,才注意到VS2005工程的Device右 ...

  6. how tomcat works 总结

    希望各位网友在看完<<how tomcat works>>一书或者鄙人的tomcat专栏文章后再看这篇博客 这里主要是梳理各个章节的核心概念 第一章 一个简单的Web服务器 第 ...

  7. redis持久化AOF与RDB配置

    AOF保存的数据方案时最完整的,如果同时开启了rdb和aof下,会采用aof方式. (1)设置数据保存到数据文件中的save规则 save 900 1     #900秒时间,至少有一条数据更新,则保 ...

  8. The 15th tip of DB Query Analyzer

    The 15th tip of DB Query Analyzer      ---- SQL Execute Schedule function is realized in 6.01 Ma Gen ...

  9. 面试之路(6)-BAT面试之操作系统内存详解

    本文主要参考两篇博客,读后整理出来,以供大家阅读,链接如下: http://blog.jobbole.com/95499/?hmsr=toutiao.io&utm_medium=toutiao ...

  10. G1 GC技术解析

    介绍 G1 GC,全称Garbage-First Garbage Collector,通过-XX:+UseG1GC参数来启用.G1收集器是工作在堆内不同分区上的收集器,分区既可以是年轻代也可以是老年代 ...