当前位置:网站首页>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.07.31(LC_6132_使数组中所有元素都等于零)
- 每周推荐短视频:为什么产品开发需要数字化?如何做到数字化?
- 笔记本开机黑屏提示:ERROR 0199:System Security-Security password retry count exceeded
- Splunk Field Caculated 计算字段
- 【机器学习】实验2布置:基于回归分析的大学综合得分预测
- ADS通信--倍福PLC和C#TextBox控件实现数据绑定的方法
- 主流定时任务解决方案全横评
- Splunk Filed Alias 字段改名
- 自然语言处理 文本预处理(上)(分词、词性标注、命名实体识别等)
猜你喜欢
随机推荐
牛客编程题中——需要处理输入较大数的题目
【暑期每日一题】洛谷 P1192 台阶问题
LeetCode Algorithm 1374. 生成每种字符都是奇数个的字符串
张驰课堂:六西格玛测量系统的误差分析与判定
【论文精读】Geometric Structure Preserving Warp for Natural Image Stitching
Splunk Field Caculated 计算字段
OC-范畴
2022夏暑假每日一题(六)
apt & apt-get命令
SQL执行顺序
图腾柱和推挽电路介绍
查看端口号占用
Splunk Filed Alias 字段改名
正则表达式的理解学习
PWA 踩坑 - 第一次加载页面后无法获取CacheStorage某些资源
吃透Chisel语言.30.Chisel进阶之通信状态机(二)——FSMD:以Popcount为例
查看僵尸进程
技术管理三级跳
返回文件名问题
实例027:递归输出










