2204. Distance to a Cycle in Undirected Graph

Updated: 2024-03-12
1 min read
[]

On This Page

LeetCode problem 2204

class Solution:
    def distanceToCycle(self, n: int, edges: List[List[int]]) -> List[int]:
        g = defaultdict(set)
        for a, b in edges:
            g[a].add(b)
            g[b].add(a)
        q = deque(i for i in range(n) if len(g[i]) == 1)
        f = [0] * n
        seq = []
        while q:
            i = q.popleft()
            seq.append(i)
            for j in g[i]:
                g[j].remove(i)
                f[i] = j
                if len(g[j]) == 1:
                    q.append(j)
            g[i].clear()
        res = [0] * n
        for i in seq[::-1]:
            res[i] = res[f[i]] + 1
        return res