当前位置:网站首页>LeetCode 2360. The longest cycle in a graph
LeetCode 2360. The longest cycle in a graph
2022-08-02 07:49:00 【HumbleFool】
基环树

class Solution {
public:
vector<int> p;
vector<bool> st; //Has it been searched
vector<int> in_stk; // Records the depth of the node in the current stack
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]); // The next node is already searched,Subtract the previously searched depth to get the ring size
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;
}
};
边栏推荐
- (2022牛客多校五)B-Watches(二分)
- 企业实训复现指导手册——基于华为ModelArts平台的OpenPose模型的训练和推理、基于关键点数据实现对攀爬和翻越护栏两种行为的识别、并完成在图片中只标注发生行为的人
- Wuhan 2022 organizing of the high-performance computing added new ecological development of high-performance computing
- 【CNN回归预测】基于matlab卷积神经网络CNN数据回归预测【含Matlab源码 2003期】
- docker 安装mysql
- MQ带来的一些问题、及解决方案
- 条件构造器~wapper
- MySQL-FlinkCDC-Hudi实时入湖
- SQL server 2014 怎么一次性导出多个查询结果?
- 【图像去噪】基于matlab双立方插值和稀疏表示图像去噪【含Matlab源码 2009期】
猜你喜欢
随机推荐
【暑期每日一题】洛谷 P1551 亲戚
有关 sql中的 concat()函数问题,如何拼接
图腾柱和推挽电路介绍
Vscode connect to remote server "Acquiring the lock on the/home / ~ 'problem
【图像隐藏】基于matlab混合DWT-HD-SVD数字图像水印方法技术【含Matlab源码 2007期】
获取间隔的日期列表工具类
解决:- SPY: No data found for this date range, symbol may be delisted报错
海缆探测仪TSS350(二)
【ROS基础】rosbag 的使用方法
optional
FaceBook社媒营销高效转化技巧分享
【暑期每日一题】洛谷 P1255 数楼梯
电商库存系统的防超卖和高并发扣减方案
返回文件名问题
新产品立大功 伟世通第二季度营收双增
实例028:递归求等差数列
实例029:反向输出
请教一下,Flink SQL ,JDBC sink 入 mysql 库,想要搞一个自增主键,要怎么写
张驰课堂:六西格玛测量系统的误差分析与判定
OC-NSArray










