PAT甲级真题及训练集(25)--1094. The Largest Generation (25)
来源:互联网 发布:北京淘宝客服简历模板 编辑:程序博客网 时间:2024/06/05 18:41
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 1321 1 2301 4 03 02 04 0503 3 06 07 0806 2 12 1313 1 2108 2 15 1602 2 09 1011 2 19 2017 1 2205 1 1107 1 1409 1 1710 1 18Sample Output:
9 4
提交代码
/**作者:一叶扁舟时间:16:48 2017/7/9思路:统计树每一层结点的个数,并输出层数结点个数最多的个数和第几层,本题是站在第A1004题基础上修改的*/#include <stdio.h>#include <stdlib.h>#include <algorithm>#include <stack>#include <queue>#include <math.h>#include <string.h>#include <vector>using namespace std;#define SIZE 100001typedef struct TreeNode{int data;vector<int> child;}TreeNode;int level[SIZE] = { 0 };//定义每一层统计树节点数//定义的树,可以用结构体,也可以用直接用vector<int> tree来定义TreeNode treeNode[SIZE];int IsRoot[SIZE] = { 0 };//0默认是根节点,1是子结点//树的最大深度int maxLevel = 0;//得到每一层结点个数void getLevelNodeNum(int rootNum, int depth){//到了叶子节点level[depth]++;//特别要注意这个要放在外面,如果放在if里面了,则是判断叶子结点所在层的结点个数//事实上是错误的,因为统计的是每一层的结点个数,也包括了非叶子结点if (treeNode[rootNum].child.size() == 0){if (depth > maxLevel){maxLevel = depth;}return;}for (unsigned int i = 0; i < treeNode[rootNum].child.size(); i++){//递归访问root的子结点getLevelNodeNum(treeNode[rootNum].child[i], depth + 1);}}int getTreeRoot(int N){for (int i = 1; i <= N; i++){if (IsRoot[i] != 1){return i;}}return 0;}int main(){int N;int m;//有孩子结点的个数scanf("%d %d", &N, &m);for (int i = 0; i < m; i++){int num, nodeNo;//nodeNo父节点,num为父节点nodeNo后面的子结点的个数scanf("%d %d", &nodeNo, &num);for (int j = 0; j < num; j++){int temp;scanf("%d", &temp);IsRoot[temp] = 1;//子结点再这里出现了,说明不可能是根节点//存入对应的孩子结点treeNode[nodeNo].child.push_back(temp);}}//得到根结点int rootNum = getTreeRoot(N);getLevelNodeNum(rootNum, 1);double result = 0;int maxNode = 0;int levelNum = 0;//记录最多结点数的层数//查询出最大结点的层数for (int i = 1; i <= maxLevel; i++){if (level[i] > maxNode){maxNode = level[i];levelNum = i;}}//输出printf("%d %d\n", level[levelNum], levelNum);system("pause");return 0;}
- PAT甲级真题及训练集(25)--1094. The Largest Generation (25)
- 1094. The Largest Generation (25) PAT甲级
- PAT 甲级 1094. The Largest Generation (25)
- 1094. The Largest Generation (25)-PAT甲级真题(bfs,dfs,树的遍历)
- PAT甲级 The Largest Generation (25)
- [转]PAT甲级练习1094. The Largest Generation (25)
- PAT A 1094. The Largest Generation (25)
- PAT 1094. The Largest Generation (25)
- PAT 1094. The Largest Generation (25)
- PAT 1094. The Largest Generation (25)
- 【PAT】1094. The Largest Generation (25)
- PAT 1094. The Largest Generation (25)
- pat-1094. The Largest Generation (25)
- PAT(A) - 1094. The Largest Generation (25)
- PAT 1094. The Largest Generation (25)
- PAT-A-1094. The Largest Generation (25)
- pat-a 1094. The Largest Generation (25)
- PAT A 1094. The Largest Generation (25)
- 自定义View触摸相关工具类全解
- python flask下传数据到js
- 新手入门之VIM 编辑小技巧
- Java学习笔记
- 美团CodeM复赛 02,03
- PAT甲级真题及训练集(25)--1094. The Largest Generation (25)
- Matplotlib基本操作
- linux字符设备
- centos 6.6 LVM分区 增加磁盘空间
- Linux 配置环境变量
- mac 修改文件权限为777
- C 语言返璞归真: 指针篇(3)
- 学习java开源框架前应该知道的
- QT 5.9和VS2015 编译QTFTP