当前位置:网站首页>leetcode:6135. 图中的最长环【内向基环树 + 最长环板子 + 时间戳】
leetcode:6135. 图中的最长环【内向基环树 + 最长环板子 + 时间戳】
2022-07-31 17:28:00 【白速龙王的回眸】

分析
每个点出度最多是1
所以是一个【弱化】的内向基环图
所有联通分量最多一个环
所有点最多一个环
利用时间戳的方式记录每个点上一次访问的时间
如果有重复访问并且起始时间在st后面,意味着是一个新的环
就记录入案中
同时更新时间戳,和这个点最近的时间戳即可
ac code
class Solution:
def longestCycle(self, edges: List[int]) -> int:
# 至多一个出边 -》弱化内向基环树
# 每个联通分量最多只有一个环
# 每个点最多只有一个环
# 时间戳 + 边遍历边改优化
n = len(edges)
time = [0] * n
clock, ans = 1, -1
# x 是起点
for x, t in enumerate(time):
if t: continue # 之前已经被动遍历过了
st = clock
while x >= 0:
if time[x]: # 二次遍历x
if time[x] >= st: # 找到一个新的环
ans = max(ans, clock - time[x])
break
time[x] = clock
clock += 1
x = edges[x]
return ans
总结
图论 + 最大环 + 内向基环 + 时间戳
边栏推荐
- 组合学笔记(六)局部有限偏序集的关联代数,Möbius反演公式
- Huawei's top engineers lasted nine years "anecdotal stories network protocol" PDF document summary, is too strong
- 【luogu P8326】Fliper(图论)(构造)(欧拉回路)
- 每日练习------随机产生一个1-100之间的整数,看能几次猜中。要求:猜的次数不能超过7次,每次猜完之后都要提示“大了”或者“小了”。
- Concurrency, Timing and Relativity
- go记录之——slice
- Last write wins (discards concurrent writes)
- Mariabackup implements incremental data backup for Mariadb 10.3
- Golang 必知必会Go Mod命令
- 如何识别假爬虫?
猜你喜欢

【码蹄集新手村600题】不通过字符数组来合并俩个数字

ECCV 2022 华科&ETH提出首个用于伪装实例分割的一阶段Transformer的框架OSFormer!代码已开源!...
![[pytorch] pytorch automatic derivation, Tensor and Autograd](/img/99/c9632a7d3f70a13e1e26b9aa67b8b9.png)
[pytorch] pytorch automatic derivation, Tensor and Autograd

Smart Trash Can (8) - Infrared Tube Sensor (Raspberry Pi pico)

Jiuqi ny3p series voice chip replaces the domestic solution KT148A, which is more cost-effective and has a length of 420 seconds

This 985 professor is on fire!After 10 years of Ph.D. supervisor, no one has graduated with a Ph.D.!
![[TypeScript] OOP](/img/d7/b3175ab538906ac1b658a9f361ba44.png)
[TypeScript] OOP

35 MySQL interview questions and diagrams, this is also easy to understand

上传图片-微信小程序(那些年的坑记录2022.4)

35道MySQL面试必问题图解,这样也太好理解了吧
随机推荐
Bika LIMS 开源LIMS集—— SENAITE的使用(检测流程)
MATLAB程序设计与应用 2.4 MATLAB常用内部函数
go记录之——slice
spark报错OutOfMemory「建议收藏」
这位985教授火了!当了10年博导,竟无一博士毕业!
全平台GPU通用AI视频补帧超分教程
使用互相关进行音频对齐
useragent在线查找
GateWay实现负载均衡
Go basic part study notes
Masterless Replication System (3)-Limitations of Quorum Consistency
牛客网刷题(二)
35 MySQL interview questions and diagrams, this is also easy to understand
你辛辛苦苦写的文章可能不是你的原创
【愚公系列】2022年07月 Go教学课程 022-Go容器之字典
最新神作!阿里巴巴刚出炉的面试参考指南(泰山版),我直接狂刷29天
21.支持向量机—核函数的介绍
20.支持向量机—数学原理知识
All-platform GPU general AI video supplementary frame super-score tutorial
Istio介绍