L2-026 小字辈 - java

2023-10-27 18:28
文章标签 java l2 026 小字辈

本文主要是介绍L2-026 小字辈 - java,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

L2-026 小字辈


时间限制
400 ms
内存限制
64 MB


题目描述:

本题给定一个庞大家族的家谱,要请你给出最小一辈的名单。

输入格式:
输入在第一行给出家族人口总数 N(不超过 100 000 的正整数) —— 简单起见,我们把家族成员从 1 到 N 编号。随后第二行给出 N 个编号,其中第 i 个编号对应第 i 位成员的父/母。家谱中辈分最高的老祖宗对应的父/母编号为 -1。一行中的数字间以空格分隔。

输出格式:
首先输出最小的辈分(老祖宗的辈分为 1,以下逐级递增)。然后在第二行按递增顺序输出辈分最小的成员的编号。编号间以一个空格分隔,行首尾不得有多余空格。

输入样例:
9
2 6 5 5 -1 5 6 4 7
输出样例:
4
1 9


给定n个数,第i个编号代表第i个人的父母
让你求出最小的辈分以及最小辈分的成员编号


emmmmmmm

1、dfs搜索
先找出根节点,然后依据根节点往下建树
统计并更新最下面的叶子节点

2、并查集
可以利用并查集去实现,在找爹爹的同时把辈分加起来


1、dfs搜索

java 过不了,欸

import java.io.*;
import java.math.*;
import java.util.*;public class Main
{static int N = (int) 1e5;static ArrayList<Integer> map[] = new ArrayList[N + 10];static HashSet<Integer> set = new HashSet<Integer>();static int maxDeep = 0;//	node节点, deep辈分static void dfs(int node, int deep){
//		同辈份if (deep == maxDeep) set.add(node);
//		找到再小的辈分else if (deep > maxDeep){
//			更新最小的辈分maxDeep = deep;set.clear();set.add(node);}//		遍历该辈分下的孩子for (int i = 0; i < map[node].size(); i++)dfs(map[node].get(i), deep + 1);}public static void main(String[] args){int n = sc.nextInt();for (int i = 1; i <= n; i++)map[i] = new ArrayList<Integer>();int root = -1;for (int i = 1; i <= n; i++){int x = sc.nextInt();
//			找到老祖宗if (x == -1){root = i;continue;}map[x].add(i);}dfs(root, 1);out.println(maxDeep);int count = 0;for (int i : set){if (count++ != 0)out.print(" ");out.print(i);}out.flush();out.close();}static Scanner sc = new Scanner(System.in);static PrintWriter out = new PrintWriter(System.out);
}

c++

#include <bits/stdc++.h>using namespace std;const int N = 1e5;
vector<int> mp[N + 10];
set<int> s;
int maxDeep = 1;void dfs(int node, int deep)
{if(deep == maxDeep)s.insert(node);else if(deep > maxDeep){s.clear();s.insert(node);maxDeep = deep;}for(int i = 0; i < mp[node].size(); i ++)dfs(mp[node][i], deep + 1);
}int main()
{int n; scanf("%d", &n);int root = -1;for(int i = 1; i <= n; i ++){int x; scanf("%d", &x);if(x == -1){root = i;continue;}mp[x].push_back(i);}dfs(root, 1);printf("%d\n", maxDeep);for(auto it = s.begin(); it != s.end(); it ++){if(it != s.begin())printf(" ");printf("%d", *it);}return 0;
}

2、 并查集

java 还不是不知道为啥错了两个点

import java.io.*;
import java.math.*;
import java.util.*;public class Main
{static int N = (int) 1e5;static int shu[] = new int[N + 10];static int deep[] = new int[N + 10];static int find(int x){
//		祖宗节点if (shu[x] == -1)deep[x] = 1;//		当前的辈分需要更新if (deep[x] == 0)deep[x] = find(shu[x]) + 1;return deep[x];}public static void main(String[] args){int n = sc.nextInt();for (int i = 1; i <= n; i++)shu[i] = sc.nextInt();int max = 1;for (int i = 1; i <= n; i++)max = Math.max(max, find(i));out.println(max);int cnt = 0;for (int i = 1; i <= n; i++){if (max == deep[i]){if (cnt++ != 0)out.print(" ");out.print(i);}}out.flush();out.close();}static Scanner sc = new Scanner(System.in);static PrintWriter out = new PrintWriter(System.out);
}

c++

#include <bits/stdc++.h>using namespace std;const int N = 1e5;
int shu[N + 10];
int deep[N + 10];int find(int x)
{if(shu[x] == -1)deep[x] = 1;if(deep[x] == 0)deep[x] = find(shu[x]) + 1;return deep[x];
}int main()
{int n; scanf("%d", &n);for(int i = 1; i <= n; i ++)scanf("%d", &shu[i]);int mx = 1;for(int i = 1; i <= n; i ++) mx = max(mx, find(i));printf("%d\n", mx);int cnt = 0;for(int i = 1; i <= n; i ++){if(deep[i] == mx){if(cnt ++ != 0)printf(" ");printf("%d", i);}}return 0;
}

ArrayList
ArrayList

dfs
dfs

并查集
并查集


如果有说错的 或者 不懂的 尽管提 嘻嘻

一起进步!!!


闪现

这篇关于L2-026 小字辈 - java的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/287528

相关文章

Spring Integration Redis 使用示例详解

《SpringIntegrationRedis使用示例详解》本文给大家介绍SpringIntegrationRedis的配置与使用,涵盖依赖添加、Redis连接设置、分布式锁实现、消息通道配置及... 目录一、依赖配置1.1 Maven 依赖1.2 Gradle 依赖二、Redis 连接配置2.1 配置 R

Spring Security重写AuthenticationManager实现账号密码登录或者手机号码登录

《SpringSecurity重写AuthenticationManager实现账号密码登录或者手机号码登录》本文主要介绍了SpringSecurity重写AuthenticationManage... 目录一、创建自定义认证提供者CustomAuthenticationProvider二、创建认证业务Us

Java Stream流以及常用方法操作实例

《JavaStream流以及常用方法操作实例》Stream是对Java中集合的一种增强方式,使用它可以将集合的处理过程变得更加简洁、高效和易读,:本文主要介绍JavaStream流以及常用方法... 目录一、Stream流是什么?二、stream的操作2.1、stream流创建2.2、stream的使用2.

Java对接MQTT协议的完整实现示例代码

《Java对接MQTT协议的完整实现示例代码》MQTT是一个基于客户端-服务器的消息发布/订阅传输协议,MQTT协议是轻量、简单、开放和易于实现的,这些特点使它适用范围非常广泛,:本文主要介绍Ja... 目录前言前置依赖1. MQTT配置类代码解析1.1 MQTT客户端工厂1.2 MQTT消息订阅适配器1.

Spring Boot项目如何使用外部application.yml配置文件启动JAR包

《SpringBoot项目如何使用外部application.yml配置文件启动JAR包》文章介绍了SpringBoot项目通过指定外部application.yml配置文件启动JAR包的方法,包括... 目录Spring Boot项目中使用外部application.yml配置文件启动JAR包一、基本原理

SpringBoot加载profile全面解析

《SpringBoot加载profile全面解析》SpringBoot的Profile机制通过多配置文件和注解实现环境隔离,支持开发、测试、生产等不同环境的灵活配置切换,无需修改代码,关键点包括配置文... 目录题目详细答案什么是 Profile配置 Profile使用application-{profil

Java中InputStream重复使用问题的几种解决方案

《Java中InputStream重复使用问题的几种解决方案》在Java开发中,InputStream是用于读取字节流的类,在许多场景下,我们可能需要重复读取InputStream中的数据,这篇文章主... 目录前言1. 使用mark()和reset()方法(适用于支持标记的流)2. 将流内容缓存到字节数组

Java慢查询排查与性能调优完整实战指南

《Java慢查询排查与性能调优完整实战指南》Java调优是一个广泛的话题,它涵盖了代码优化、内存管理、并发处理等多个方面,:本文主要介绍Java慢查询排查与性能调优的相关资料,文中通过代码介绍的非... 目录1. 事故全景:从告警到定位1.1 事故时间线1.2 关键指标异常1.3 排查工具链2. 深度剖析:

Springboot项目登录校验功能实现

《Springboot项目登录校验功能实现》本文介绍了Web登录校验的重要性,对比了Cookie、Session和JWT三种会话技术,分析其优缺点,并讲解了过滤器与拦截器的统一拦截方案,推荐使用JWT... 目录引言一、登录校验的基本概念二、HTTP协议的无状态性三、会话跟android踪技术1. Cook

java实现多数据源切换方式

《java实现多数据源切换方式》本文介绍实现多数据源切换的四步方法:导入依赖、配置文件、启动类注解、使用@DS标记mapper和服务层,通过注解实现数据源动态切换,适用于实际开发中的多数据源场景... 目录一、导入依赖二、配置文件三、在启动类上配置四、在需要切换数据源的类上、方法上使用@DS注解结论一、导入