Hard
,DFS
,Graph
2360. Longest Cycle in a Graph
You are given a directed graph of n
nodes numbered from 0
to n - 1
, where each node has at most one outgoing edge.
The graph is represented with a given 0-indexed array edges
of size n
, indicating that there is a directed edge from node i
to node edges[i]
. If there is no outgoing edge from node i
, then edges[i] == -1.
Return the length of the longest cycle in the graph. If no cycle exists, return -1
.
A cycle is a path that starts and ends at the same node.
Example 1:
Example 2:
Constraints:
n
== edges.length
n
<= 105edges[i]
< nedges[i]
!= iYen-Chi ChenSun, Mar 26, 2023
Yen-Chi ChenSun, Mar 26, 2023
visited_node 原本ㄉ資料結構選錯一直TLEㄏgpwork4uSun, Mar 26, 2023
照著 Reference 的影片做出來ㄉ
Ron ChenMon, Mar 27, 2023
照著一行超人給的影片做出來惹
MarsgoatMar 30, 2023