Shortest Cycle Through a Node
Asked at Google
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
Example 1
Input: n =
3, edges =[[0,1],[1,2],[2,0]], v =0Output:
3Explanation: The edges 0 -> 1, 1 -> 2 and 2 -> 0 form a cycle of 3 edges through node 0.
Example 2
Input: n =
3, edges =[[0,1],[1,2]], v =0Output:
-1Explanation: 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.