当前位置:网站首页>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;
}
};
边栏推荐
- Leetcode Weekly 304
- 问个问题,我的Flinkcdc已经跑通了,可以监听msql的binlog了,也能发送kafk
- 第06章 索引的数据结构【2.索引及调优篇】【MySQL高级】
- MySQL-FlinkCDC-Hudi实时入湖
- [npm install error report collection] - npm ERR! code ENOTEMPTY npm ERR! syscall rmdir
- 数据库概论-MySQL的数据表的基本操作
- July 18-July 31, 2022 (Ue4 video tutorials and documentation, 20 hours. Total 1412 hours, 8588 hours left)
- 实验8 VLAN综合实验
- LeetCode 283. 移动零(简单、数组)
- 请教一下,Flink SQL ,JDBC sink 入 mysql 库,想要搞一个自增主键,要怎么写
猜你喜欢
随机推荐
Swagger的简单介绍,集成,以及如何在生产环境中关闭swagger,在测试和开发环境中自动打开
PMP新考纲考试内容介绍
See the picture to understand | How to choose sales indicators to measure the health of business growth
【杂】pip换国内源教程及国内源地址
查看僵尸进程
(2022牛客多校五)D-Birds in the tree(树形DP)
(笔记整理未完成)【图论】图的遍历
每周推荐短视频:为什么产品开发需要数字化?如何做到数字化?
“蔚来杯“2022牛客暑期多校训练营5,签到题KBGHFCD
2022夏暑假每日一题(六)
ADS通信--倍福PLC和C#TextBox控件实现数据绑定的方法
Project development specification
【暑期每日一题】洛谷 P3156 【深基15.例1】询问学号
Submit code process
第06章 索引的数据结构【2.索引及调优篇】【MySQL高级】
请教一下,Flink SQL ,JDBC sink 入 mysql 库,想要搞一个自增主键,要怎么写
堡垒机、堡垒机的原理
gdalinfo: error while loading shared libraries: libgdal.so.30: cannot open shared object file: No su
图腾柱和推挽电路介绍
【机器学习】实验2布置:基于回归分析的大学综合得分预测










