当前位置:网站首页>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;
}
};
边栏推荐
猜你喜欢

59:第五章:开发admin管理服务:12:MongoDB的使用场景;(非核心数据,数据量比较大的非核心数据,人脸照片等隐私的小文件;)
![[npm install error report collection] - npm ERR! code ENOTEMPTY npm ERR! syscall rmdir](/img/c5/2c42e26e577506573985b30669ca6c.png)
[npm install error report collection] - npm ERR! code ENOTEMPTY npm ERR! syscall rmdir

See the picture to understand | How to choose sales indicators to measure the health of business growth

每周推荐短视频:为什么产品开发需要数字化?如何做到数字化?
![(Part of it is not understood, and the notes are not completed) [Graph Theory] Difference Constraints](/img/e0/385579fc8657db8b175318bd739908.gif)
(Part of it is not understood, and the notes are not completed) [Graph Theory] Difference Constraints

HCIP day 3 experiment
——设备树的概述(硬件、目标、效果、文件类型)](/img/c6/6c2321bfcd184886e1cb59664bec11.png)
[21天学习挑战赛——内核笔记](一)——设备树的概述(硬件、目标、效果、文件类型)

张驰咨询:企业实施精益管理的最大障碍,只把精益作为一种工具和方法

【故障诊断分析】基于matlab FFT轴承故障诊断(包络谱)【含Matlab源码 2002期】

数据库概论之MySQL表的增删改查1
随机推荐
跨阻放大器
HCIP day 3 experiment
Link with Game Glitch(spfa判负环)
Redis 常用命令和基本数据结构(数据类型)
SimpleChannelInboundHandler使用总结
2022年数据泄露平均成本高达435万美元,创历史新高!
【CNN回归预测】基于matlab卷积神经网络CNN数据回归预测【含Matlab源码 2003期】
LeetCode SQL 197. 上升的温度
HCIP day one
The second day HCIP
apt & apt-get命令
从云计算到函数计算
【机器学习】实验5布置:AAAI会议论文聚类分析
论文《Deep Multifaceted Transformers for Multi-objective Ranking in Large-Scale E-commerce Recommender》
笔记本开机黑屏提示:ERROR 0199:System Security-Security password retry count exceeded
聊天机器人如何提升独立站的营销水平?
Gradle系列——Gradle插件(基于Gradle文档7.5)day3-2
(部分不懂,笔记整理未完成)【图论】差分约束
张驰课堂:六西格玛测量系统的误差分析与判定
技术管理三级跳
