PAT-A | 1094 | The Largest Generation
2015-09-10 11:45
344 查看
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
解题思路:
每个结点都保存它的父结点和它的层数。
1. 处理输入。根据输入的格式,每一行输入都可以知道子节点的父节点的编号,将其记录下来,level初始化为0.
2. 计算层数。从根结点开始,层序遍历,用队列实现。首先计算根结点的子节点的层数,然后一层一层向下推进。
3. 计算结点数。上一步知道了每个结点的层数,对每个结点遍历一次,统计每一层的结点数,保存在数组gen[]中。
4. 输出结果。知道了每一层的结点数,找出结点数最大的那一层,输出该层结点数与该层号即可。
#include <iostream> #include <queue> using namespace std; struct node { int level; int parent; }; typedef node *tree; int main() { int N, M; int pid, K, cid; //parent id, K, child id tree T; int *gen; // nodes of every generation queue<int> Q; int f;//front of queue cin >> N >> M; T = new node[N + 1]; gen = new int[N + 1]; //step 1. process input for (int i = 0; i != M; ++i) { cin >> pid >> K; for (int j = 0; j != K; ++j) { cin >> cid; T[cid].parent = pid; T[cid].level = 0; //initialize } } //step 2. calculate every node's level T[1].level = 1;//root Q.push(1);//root while (!Q.empty()) { f = Q.front(); Q.pop(); for (int i = 1; i != N + 1; ++i) { if (T[i].parent == f) { T[i].level = T[f].level + 1; Q.push(i); } } } //debug //for (int i = 1; i != N + 1; ++i) { // cout << i << " " << T[i].parent << " " << T[i].level << endl; //} //step 3. calculate the number of nodes of each level //initialize gen[] for (int i = 1; i != N + 1; ++i) { gen[i] = 0; } int maxLevel = 1; for (int i = 1; i != N + 1; ++i) { ++gen[T[i].level]; if (T[i].level > maxLevel) { maxLevel = T[i].level; } } //debug //for (int i = 1; i <= maxLevel; ++i) { // cout << gen[i] << " "; //} //step 4. find the largest population number and the level of the corresponding generation int maxNodes = 0; int level = 0; for (int i = 1; i <= maxLevel; ++i) { if (gen[i] > maxNodes) { maxNodes = gen[i]; level = i; } } cout << maxNodes << " " << level << endl; delete[]T; delete[]gen; }
这题与PAT-A 1004 Counting Leaves 类型相同。
相关文章推荐
- 一步一步学EF系列四【升级篇 实体与数据库的映射】
- 无限轮播(功能完善)
- 如何延长Win10通知消息显示时间?延长Win10通知消息显示时间的方法
- html ajax 简单封装
- mysql里筛选生日
- Spring中@Async注解实现方法的异步调用
- 用CGcontext绘制2D单行表格
- 初识XML
- EXT基本方法、属性(mixins、statics、require)
- springMVC中文件的下载和上传
- 无限轮播(循环展示)
- 无限轮播(新闻数据展示)
- 百度地图 获取矩形point
- 【转】TCP三次握手和四次握手协议
- expect笔记
- 关于重写的一个问题
- 3.0.1、关于 Docker
- piwik的一些坑
- centos服务器搭建svn
- CALayer常用属性整理