当前位置:网站首页>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
总结
图论 + 最大环 + 内向基环 + 时间戳
边栏推荐
- Automated testing - web automation - first acquaintance with selenium
- Write a database document management tool based on WPF repeating the wheel (1)
- 全平台GPU通用AI视频补帧超分教程
- 35道MySQL面试必问题图解,这样也太好理解了吧
- 【网络通信三】研华网关Modbus服务设置
- Last write wins (discards concurrent writes)
- ECCV 2022 华科&ETH提出首个用于伪装实例分割的一阶段Transformer的框架OSFormer!代码已开源!...
- Multi-datacenter operation and detection of concurrent writes
- AcWing 1282. Search Keyword Problem Solution ((AC Automata) Trie+KMP)+bfs)
- GP 6 overall architecture study notes
猜你喜欢
随机推荐
【Yugong Series】July 2022 Go Teaching Course 022-Dictionary of Go Containers
多主复制下处理写冲突(3)-收敛至一致的状态及自定义冲突解决逻辑
Combinatorics Notes (6) Associative Algebra of Locally Finite Partially Ordered Sets, Möbius Inversion Formula
10 Ways to Keep Your Interface Data Safe
Flink_CDC搭建及简单使用
[Network Communication 3] Advantech Gateway Modbus Service Settings
Introduction of Jerry voice chip ic toy chip ic_AD14NAD15N full series development
【luogu P8326】Fliper(图论)(构造)(欧拉回路)
2022年Android 面经总结(附含面试题 | 源码 | 面试资料)
Intelligent bin (9) - vibration sensor (raspberries pie pico implementation)
AcWing 1282. Search Keyword Problem Solution ((AC Automata) Trie+KMP)+bfs)
牛客网刷题(一)
九齐ny3p系列语音芯片替代国产方案KT148A性价比更高420秒长度
Smart Trash Can (8) - Infrared Tube Sensor (Raspberry Pi pico)
JS基础小练习
迁移学习——Domain Adaptation
JD.com searches for products by keyword API
【源码解析】BeanFactory和FactoryBean
MySQL---聚合函数
【luogu P8326】Fliper (Graph Theory) (Construction) (Eulerian Circuit)









