当前位置:网站首页>LeetCode 2360. 图中的最长环
LeetCode 2360. 图中的最长环
2022-08-02 06:52:00 【HumbleFool】
基环树
class Solution {
public:
vector<int> p;
vector<bool> st; //是否被搜过
vector<int> in_stk; // 记录节点在当前栈中的深度
int res = -1;
void dfs(int u, int deth)
{
st[u] = true;
in_stk[u] = deth;
int ne = p[u];
if(ne != -1)
{
if(in_stk[ne])
res = max(res, deth + 1 - in_stk[ne]); // 下一个节点是已经搜过了,减去之前搜到过的深度就是环的大小
else if(!st[ne])
dfs(ne, deth + 1);
}
in_stk[u] = 0;
}
int longestCycle(vector<int>& edges) {
p = edges;
int n = edges.size();
st = vector<bool>(n, false);
in_stk = vector<int>(n, 0);
for(int i = 0; i < n; i ++)
if(!st[i])
dfs(i, 1);
return res;
}
};
边栏推荐
猜你喜欢
[Dataset][VOC] Eyewear dataset 6000 in VOC format
Analysis of GCC compiler technology
Clapper that can interact with the audience in real time
反射课后习题及做题记录
HCIP day 3 experiment
The second day HCIP
论文阅读 (64):Weakly-supervised Video Anomaly Detection with Robust Temporal Feature Magnitude Learning
Day 4 of HCIP
_2_顺序表
59:第五章:开发admin管理服务:12:MongoDB的使用场景;(非核心数据,数据量比较大的非核心数据,人脸照片等隐私的小文件;)
随机推荐
File upload vulnerability (2)
埋点开发流程
【图像隐藏】基于matlab混合DWT-HD-SVD数字图像水印方法技术【含Matlab源码 2007期】
实例032:反向输出II
在VMware上安装Metasploitable2
【暑期每日一题】洛谷 P1551 亲戚
分离轴定理SAT凸多边形精确碰撞检测
Unity Shader学习(七)纹理图像的简单使用
PMP新考纲通关秘籍,告别抓瞎
(部分不懂,笔记整理未完成)【图论】差分约束
解决:- SPY: No data found for this date range, symbol may be delisted报错
笔记本开机黑屏提示:ERROR 0199:System Security-Security password retry count exceeded
LeetCode 283. 移动零(简单、数组)
C# FileInfo class
实验8 VLAN综合实验
每周推荐短视频:为什么产品开发需要数字化?如何做到数字化?
PMP新考纲考试内容介绍
实例029:反向输出
awk语法-01-基础语法(命令、选项、内部变量)
逆变器锁相原理及DSP实现