Class Solution
java.lang.Object
g2301_2400.s2360_longest_cycle_in_a_graph.Solution
2360 - Longest Cycle in a Graph.<p>Hard</p>
<p>You are given a <strong>directed</strong> graph of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, where each node has <strong>at most one</strong> outgoing edge.</p>
<p>The graph is represented with a given <strong>0-indexed</strong> array <code>edges</code> of size <code>n</code>, indicating that there is a directed edge from node <code>i</code> to node <code>edges[i]</code>. If there is no outgoing edge from node <code>i</code>, then <code>edges[i] == -1</code>.</p>
<p>Return <em>the length of the <strong>longest</strong> cycle in the graph</em>. If no cycle exists, return <code>-1</code>.</p>
<p>A cycle is a path that starts and ends at the <strong>same</strong> node.</p>
<p><strong>Example 1:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2022/06/08/graph4drawio-5.png" alt="" /></p>
<p><strong>Input:</strong> edges = [3,3,4,2,3]</p>
<p><strong>Output:</strong> 3</p>
<p><strong>Explanation:</strong> The longest cycle in the graph is the cycle: 2 -> 4 -> 3 -> 2.</p>
<p>The length of this cycle is 3, so 3 is returned.</p>
<p><strong>Example 2:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2022/06/07/graph4drawio-1.png" alt="" /></p>
<p><strong>Input:</strong> edges = [2,-1,3,1]</p>
<p><strong>Output:</strong> -1</p>
<p><strong>Explanation:</strong> There are no cycles in this graph.</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == edges.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>-1 <= edges[i] < n</code></li>
<li><code>edges[i] != i</code></li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
longestCycle
public int longestCycle(int[] edges)
-