当前位置:网站首页>leetcode-6135:图中的最长环
leetcode-6135:图中的最长环
2022-08-01 07:50:00 【菊头蝙蝠】
leetcode-6135:图中的最长环
题目
题目连接
给你一个 n 个节点的 有向图 ,节点编号为 0 到 n - 1 ,其中每个节点 至多 有一条出边。
图用一个大小为 n 下标从 0 开始的数组 edges 表示,节点 i 到节点 edges[i] 之间有一条有向边。如果节点 i 没有出边,那么 edges[i] == -1 。
请你返回图中的 最长 环,如果没有任何环,请返回 -1 。
一个环指的是起点和终点是 同一个 节点的路径。
示例 1:
输入:edges = [3,3,4,2,3]
输出去:3
解释:图中的最长环是:2 -> 4 -> 3 -> 2 。
这个环的长度为 3 ,所以返回 3 。
示例 2:
输入:edges = [2,-1,3,1]
输出:-1
解释:图中没有任何环。
解题
方法一:内向基环树找环+时间戳
class Solution {
public:
int longestCycle(vector<int>& edges) {
int n=edges.size();
vector<int> time(n,0);
int res=-1;
for(int i=0,clock=1;i<n;i++){
if(time[i]) continue;
for(int x=i,start_time=clock;x>=0;x=edges[x]){
if(time[x]){
if(time[x]>=start_time){
res=max(res,clock-time[x]);
}
break;
}
time[x]=clock++;
}
}
return res;
}
};
边栏推荐
- app 自动化 打开app (二)
- JSON 与 JS 对象的区别
- 基于tika实现对文件类型进行判断
- Case practice --- Resnet classic convolutional neural network (Mindspore)
- flink sql-client,怎么处理源端与目标增加端,sql-client包括映射表与JOB如
- 升级为重量级锁,锁重入会导致锁释放?
- Data Analysis 5
- pytest interface automation testing framework | skip test classes
- 图像基本操作的其他内容
- SAP ABAP ALV+SMARTFORS 表分页 报表打印程序
猜你喜欢
Case practice --- Resnet classic convolutional neural network (Mindspore)
自制一款远程控制软件——VeryControl
日志导致线程Block的这些坑,你不得不防
The log causes these pits in the thread block, you have to prevent
LeetCode 415:字符串相加
Vim扩展内容
特殊的日子,值得纪念
如何使用Photoshop合成星轨照片,夜空星轨照片后期处理方法
Golang: go static file processing
小程序更多的手势事件(左右滑动、放大缩小、双击、长按)
随机推荐
centos 安装php7.4,搭建hyperf,转发RDS
What do the values 1, 2, and 3 in nodetype mean?
Vim简介
C语言学习概览(一)
LeetCode 415:字符串相加
三维坐标系距离
最新的Cesium和Three的整合方法(附完整代码)
How to use Photoshop to composite star trail photos, post-processing method of night sky star trail photos
搜索框字符自动补全
关于App不同方式更新的测试点归纳
Golang:go静态文件处理
七夕来袭——属于程序员的浪漫
【STM32】入门(一):环境搭建、编译、下载、运行
数据分析6
聊一聊ICMP协议以及ping的过程
JVM内存模型之深究模型特征
2022杭电多校第二场1011 DOS Card(线段树)
微信小程序请求封装
HoloView -- Tabular Datasets
基于百度OCR的网站验证码在线识别