cp-library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub kobejean/cp-library

:heavy_check_mark: cp_library/alg/graph/floyds_cycle_fn.py

Required by

Verified with

Code

import cp_library.alg.graph.__header__

def floyds_cycle(F, root):
    slow = fast = root
    while F[fast] != -1 and F[F[fast]] != -1:
        slow, fast = F[slow], F[F[fast]]
        if slow == fast:
            cyc = [slow]
            while F[slow] != cyc[0]:
                slow = F[slow]
                cyc.append(slow)
            return cyc
    return None
'''
╺━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╸
             https://kobejean.github.io/cp-library               
'''

def floyds_cycle(F, root):
    slow = fast = root
    while F[fast] != -1 and F[F[fast]] != -1:
        slow, fast = F[slow], F[F[fast]]
        if slow == fast:
            cyc = [slow]
            while F[slow] != cyc[0]:
                slow = F[slow]
                cyc.append(slow)
            return cyc
    return None
Back to top page