Shortest Cycle Through a Node

Asked at GoogleOnsite

Reported by the problem's creator, not verified

You are given a directed graph with n nodes numbered 0 to n - 1, and a list edges where [a, b] is an edge from a to b. The same edge can appear more than once in edges.

Examples

  1. Example 1

    Input: n = 3, edges = [[0,1],[1,2],[2,0]], v = 0

    Output: 3

    Explanation: The edges 0 -> 1, 1 -> 2 and 2 -> 0 form a cycle of 3 edges through node 0.

  2. Example 2

    Input: n = 3, edges = [[0,1],[1,2]], v = 0

    Output: -1

    Explanation: Edges only go forward, so no walk from node 0 ever returns to it.

Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question

Code

Loading editor…

Ln 1, Col 1

Start an attempt to use Coach.