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

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

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

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

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

  3. PAT甲级——A1094 The Largest Generation

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

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

  5. PAT练习——1094 The Largest Generation (25 point(s))

    题目如下: #include<iostream> #include<vector> #include<algorithm> using namespace std; ...

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

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

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

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

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

  9. 【PAT甲级】1094 The Largest Generation (25 分)(DFS)

    题意: 输入两个正整数N和M(N<100,M<N),表示结点数量和有孩子结点的结点数量,输出拥有结点最多的层的结点数量和层号(根节点为01,层数为1,层号向下递增). AAAAAccept ...

随机推荐

  1. treap入门

    这几天刚学了treap,听起来还行,就是调题调到恶心了…… 就以这道题作为板子吧(”你本来也就做了一道题!”) https://www.luogu.org/problemnew/show/P3369 ...

  2. Spring boot注解使用

    1:@SpringBootApplication 注解 a:scanBasePackages 与scanBasePackageClasses配置Spring启动时扫描的包路径或者扫描的字节码文件 b: ...

  3. linux服务器关闭ipv6 方法

    第一个文件: /etc/sysconfig/network 第二个文件:如无此文件,vim添加 /etc/modprobe.d/disable_ipv6.conf

  4. torchvision

    torchvision是一个包,它服务于pytorch深度学习框架,用来生成图片,视频数据集,模型类和预训练的模型torchvision由以下四个部分组成:1. torchvision.dataset ...

  5. kettle学习笔记(七)——kettle流程步骤与应用步骤

    一.概述 流程主要用来控制数据流程与数据流向 应用则是提供一些工具类 二.流程步骤 1.ETL元数据注入 类似Java中的反射,在设计时不知道文件名.文件位置等,在真正执行时才知道具体的一些配置等信息 ...

  6. Eclipse添加Junit测试

    项目上右键,点击build path->add libraaies->选择Junit 附上惨不忍睹的图(eclipse里展开菜单项时老截屏截不好,不知各位有没有好点的解决方案) 2017. ...

  7. 20155308『网络对抗技术』Exp5 MSF基础应用

    20155308『网络对抗技术』Exp5 MSF基础应用 一.原理与实践说明 实践内容 本实践目标是掌握metasploit的基本应用方式,重点常用的三种攻击方式的思路.具体需要完成: 一个主动攻击实 ...

  8. linux下使用软连接之案例二

           在笔者的上一篇文章介绍过怎么通过linux的软连接在不修改上传代码的情况下,将上传到项目路径下的图片改为上传到项目外面,防止重新部署后图片被删除了.同时还可以直接通过类似访问静态资源的方 ...

  9. [c#][福利]BTTool种子文件修改工具

    前言 不知道各位看官是否有过类似的经历.好不容易找到一个电影的种子文件,想用百度云的离线下载功能去下载文件,却被百度云无情提示“离线文件因含有违规内容被系统屏蔽无法下载”!假设有这么一个场景,比如最近 ...

  10. python 带参数的多重继承

    1. 不带参数的多重继承 # 作者:hhh5460 # 时间:2017.07.18 class A(object): def show_x(self): print('A') class B(obje ...