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 ...
随机推荐
- RDS和ROS使用小结
微软的RDS和linux下的ROS,都已经使用了一段时间,RDS已经很久不更新了,前景必然不如ROS,但无奈用得顺手,还是偶尔怀旧一下. 使用RDS除了内置的文档需要仔细阅读,有些corobot.pr ...
- 《java入门第一季》之面向对象(代码块一网打尽)
上一篇里面对代码块做出介绍,这里给出一个面试题,加深印象. 如有毁三观的地方,请见谅.拒绝黄赌毒 写程序的执行结果. class Student { static { System.out.print ...
- CentOS删除自带的java,安装新java
[root@localhost ~]# java -version java version "1.4.2″ gij (GNU libgcj) version 4.1.2 20071124 ...
- 【网站建设】Linux上安装MySQL - 12条命令搞定MySql
从零开始安装mysql数据库 : 按照该顺序执行 : a. 查看是否安装有mysql:yum list installed mysql*, 如果有先卸载掉, 然后在进行安装; b. 安装mysql客 ...
- LeetCode之“字符串”:最短回文子串
题目链接 题目要求: Given a string S, you are allowed to convert it to a palindrome by adding characters in f ...
- git push 小结
$ git push ssh://git@dev.lemote.com/rt4ls.git master // 把本地仓库提交到远程仓库的master分支中 $ git remote add orig ...
- 恶补web之四:xhtml学习
xhtml是更严格更纯净的html代码,它与html4.01兼容.xhtml是以xml重构额html4.01 xhtml与2000年1月26日成为w3c标准,w3c将xhtml定义为最新的html版本 ...
- 代理网络中安装tomcat的注意事项
搭建J2EE开发环境的时候,tomcat怎么都没办法访问主页面.主要的问题就是Network Error (tcp_error) 百度了半天也没搞明白,最后没办法,打算重装tomcat,便对照完整的安 ...
- java web--DOM
Dom总结dom:文档对象模型的简称.dom的解析:与XML一样遵循同样的规范 将标记型文档解析成一棵DOM树,并将树中的内容都封装成节点对象. 如果html文档过大,同样会造成解析过慢,怎么使用sa ...
- volatile的适用场景
volatile保证线程间的数据是可见的(共享的),但不保证数据同步 volatile相当于synchronized的弱实现,也就是说volatile实现了类似synchronized的语义,却又没有 ...