PAT 甲级 1094 The Largest Generation
https://pintia.cn/problem-sets/994805342720868352/problems/994805372601090048
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 (<) 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 (<) 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
(>) 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
代码:
#include <bits/stdc++.h>
using namespace std; int N, M;
int vis[110], num[110];
vector<int> v[110];
int depth = -1; void dfs(int root, int step) {
if(v[root].size() == 0) {
depth = max(depth, step);
} vis[root] = 1;
num[step] ++;
for(int i = 0; i < v[root].size(); i ++) {
if(vis[v[root][i]] == 0) {
dfs(v[root][i], step + 1);
}
}
} int main() {
scanf("%d%d", &N, &M);
memset(vis, 0, sizeof(vis));
for(int i = 0; i < M; i ++) {
int id, K, x;
scanf("%d%d", &id, &K);
for(int k = 0; k < K; k ++) {
scanf("%d", &x);
v[id].push_back(x);
}
} dfs(1, 1);
int temp, ans = INT_MIN;
for(int i = 0; i <= depth; i ++) {
if(num[i] > ans) {
ans = num[i];
temp = i;
}
}
/*for(int i = 1; i <= depth; i ++)
printf("%d ", num[i]);*/
printf("%d %d\n", ans, temp);
//printf("%d\n", depth);
return 0;
}
dfs
PAT 甲级 1094 The Largest Generation的更多相关文章
- PAT甲级——1094 The Largest Generation (树的遍历)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/93311728 1094 The Largest Generati ...
- PTA甲级1094 The Largest Generation (25分)
PTA甲级1094 The Largest Generation (25分) A family hierarchy is usually presented by a pedigree tree wh ...
- PAT甲级——A1094 The Largest Generation
A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level bel ...
- PAT Advanced 1094 The Largest Generation (25) [BFS,DFS,树的遍历]
题目 A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level ...
- PAT练习——1094 The Largest Generation (25 point(s))
题目如下: #include<iostream> #include<vector> #include<algorithm> using namespace std; ...
- 1094 The Largest Generation ——PAT甲级真题
1094 The Largest Generation A family hierarchy is usually presented by a pedigree tree where all the ...
- PAT 1094 The Largest Generation[bfs][一般]
1094 The Largest Generation(25 分) A family hierarchy is usually presented by a pedigree tree where a ...
- 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甲级】1094 The Largest Generation (25 分)(DFS)
题意: 输入两个正整数N和M(N<100,M<N),表示结点数量和有孩子结点的结点数量,输出拥有结点最多的层的结点数量和层号(根节点为01,层数为1,层号向下递增). AAAAAccept ...
随机推荐
- leetcode 200. Number of Islands 、694 Number of Distinct Islands 、695. Max Area of Island 、130. Surrounded Regions
两种方式处理已经访问过的节点:一种是用visited存储已经访问过的1:另一种是通过改变原始数值的值,比如将1改成-1,这样小于等于0的都会停止. Number of Islands 用了第一种方式, ...
- [转]opengl入门例题(读取bmp图片,并显示)
#include<gl/glut.h> #define FileName "bliss.bmp" static GLint imagewidth; static GLi ...
- WorldWind源码剖析系列:星球球体的加载与渲染
WorldWind源码剖析系列:星球球体的加载与渲染 WorldWind中主函数Main()的分析 在文件WorldWind.cs中主函数Main()阐明了WorldWind的初始化运行机制(如图1所 ...
- 【转】为什么volatile不能保证原子性而Atomic可以?
直接上好文链接!!! 为什么volatile不能保证原子性而Atomic可以?
- 使用Java线程并发库实现两个线程交替打印的线程题
背景:是这样的今天在地铁上浏览了以下网页,看到网上一朋友问了一个多线程的问题.晚上闲着没事就决定把它实现出来. 题目: 1.开启两个线程,一个线程打印A-Z,两一个线程打印1-52的数据. 2.实现交 ...
- Drupal性能优化:蜜蜂培训性能优化一
大家一直都说Drupal的性能不怎么样,跑起来慢,即使不是在用户量大的时候,最近公司的蜜蜂培训产品在一个客户的使用过程中,由于用户量及数据量的激增,就遇到了比较大的性能问题,这篇文章就记录了整个优化过 ...
- OutputFormat输出过程的学习
花了大约1周的时间,最终把MapReduce的5大阶段的源代码学习结束掉了.收获不少.就算本人对Hadoop学习的一个里程碑式的纪念吧.今天花了一点点的时间,把MapReduce的最后一个阶段.输出O ...
- Ruby知识总结-一般变量+操作符+if+数组和哈希
ruby入门掌握其实很简单,下面对我司主要使用的部分入门做一个简单的归纳总结: 本文的文章结构: 1.变量 2.操作符 3.if~else~end .unless 4.数组(Array) 5.哈希(H ...
- Java基础—线程
推荐阅读:http://www.iteye.com/topic/806990 一.起手式——基本概念 1.什么叫线程 进程:进行中的程序:作为资源分配的单位. 线程:轻量级的进程:程序里的顺序控制流, ...
- STM32通用定时器原理
/************************************************************************************************ 转载 ...