当前位置:网站首页>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
总结
图论 + 最大环 + 内向基环 + 时间戳
边栏推荐
- MySQL---聚合函数
- Verilog实现占空比为5/18的9分频
- 牛客 HJ17 坐标移动
- Last write wins (discards concurrent writes)
- [Source code analysis] BeanFactory and FactoryBean
- 阿里三面:MQ 消息丢失、重复、积压问题,如何解决?
- [TypeScript]OOP
- 多主复制下处理写冲突(1)-同步与异步冲突检测及避免冲突
- useragent怎么获取
- This 985 professor is on fire!After 10 years of Ph.D. supervisor, no one has graduated with a Ph.D.!
猜你喜欢
动态规划(一)
智能垃圾桶(八)——红外对管传感器(树莓派pico)
Three aspects of Ali: How to solve the problem of MQ message loss, duplication and backlog?
ThreadLocal
全平台GPU通用AI视频补帧超分教程
新型电信“套路”,我爸中招了!
认识异常 (看完这篇你就懂了)
After Effects tutorial, How to adjust overexposed snapshots in After Effects?
关于柱状图的经典画法总结
All-platform GPU general AI video supplementary frame super-score tutorial
随机推荐
iNeuOS工业互联网操作系统,设备运维业务和“低代码”表单开发工具
35道MySQL面试必问题图解,这样也太好理解了吧
每日练习------随机产生一个1-100之间的整数,看能几次猜中。要求:猜的次数不能超过7次,每次猜完之后都要提示“大了”或者“小了”。
BOW/DOM(上)
20.支持向量机—数学原理知识
Jiuqi ny3p series voice chip replaces the domestic solution KT148A, which is more cost-effective and has a length of 420 seconds
2022年Android 面经总结(附含面试题 | 源码 | 面试资料)
go记录之——slice
adb shell error error: device unauthorized
IP protocol from 0 to 1
几款永久免费内网穿透,好用且简单(内网穿透教程)
多数据中心操作和检测并发写入
京东获取商品历史价格信息 API
TestCafe之如何进行调试
35 MySQL interview questions and diagrams, this is also easy to understand
All-platform GPU general AI video supplementary frame super-score tutorial
UserAgent 解析
【luogu P8326】Fliper(图论)(构造)(欧拉回路)
The article you worked so hard to write may not be your original
MySQL---聚合函数