当前位置:网站首页>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;
}
};
边栏推荐
- 【红队】ATT&CK - 创建或修改系统进程实现持久化(更新ing)
- (Part of it is not understood, and the notes are not completed) [Graph Theory] Difference Constraints
- 交换--STP协议
- 技术管理三级跳
- Resolving C# non-static field, method or property "islandnum.Program.getIslandCount(int[][], int, int)" requires an object reference
- 查看僵尸进程
- 2022.07.31(LC_6132_使数组中所有元素都等于零)
- OC-NSArray
- 返回文件名问题
- optional
猜你喜欢
结构体大小计算--结构体内存对齐
反射课后习题及做题记录
解决:- SPY: No data found for this date range, symbol may be delisted报错
See the picture to understand | How to choose sales indicators to measure the health of business growth
入门opencv,欢笑快乐每一天
LeetCode 2312. 卖木头块
交换网络----三种生成树协议
【图像去噪】基于matlab双立方插值和稀疏表示图像去噪【含Matlab源码 2009期】
交换部分 VLAN
Splunk Field Caculated 计算字段
随机推荐
以训辅教,以战促学 | 新版攻防世界平台正式上线运营!
Swagger的简单介绍,集成,以及如何在生产环境中关闭swagger,在测试和开发环境中自动打开
LeetCode 283. Shifting Zeros (Simple, Array)
JS初识高阶函数和函数柯里化
FormData上传二进制文件、对象、对象数组
LeetCode SQL 197. 上升的温度
A Preliminary Study on the Basic Principles of Formal Methods
Connection reset by peer problem analysis
_2_顺序表
实例027:递归输出
图腾柱和推挽电路介绍
2022夏暑假每日一题(六)
交换网络----三种生成树协议
速看!PMP新考纲、PMBOK第七版解读
【暑期每日一题】洛谷 P1255 数楼梯
【CNN回归预测】基于matlab卷积神经网络CNN数据回归预测【含Matlab源码 2003期】
吃透Chisel语言.31.Chisel进阶之通信状态机(三)——Ready-Valid接口:定义、时序和Chisel中的实现
【心电信号】基于matlab心率检测【含Matlab源码 1993期】
【机器学习】实验4布置:AAAI会议论文聚类分析
2022.07.31(LC_6133_分组的最大数量)