<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>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/08/graph4drawio-5.png" style="width: 335px; height: 191px;" />
<pre>
<strong>Input:</strong> edges = [3,3,4,2,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The longest cycle in the graph is the cycle: 2 -&gt; 4 -&gt; 3 -&gt; 2.
The length of this cycle is 3, so 3 is returned.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/07/graph4drawio-1.png" style="width: 171px; height: 161px;" />
<pre>
<strong>Input:</strong> edges = [2,-1,3,1]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There are no cycles in this graph.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == edges.length</code></li>
	<li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>-1 &lt;= edges[i] &lt; n</code></li>
	<li><code>edges[i] != i</code></li>
</ul>

class Solution {
public:
    int ans = -1;  // Initialize ans to -1 to store the length of the longest cycle.
    
    // DFS function to traverse the graph and detect cycles
    void dfs(int node, vector<int>& edges, vector<bool>& visited, unordered_map<int, int>& dist) 
    {
        visited[node] = true;  // Mark the current node as visited
        
        int neighbour = edges[node];  // Get the neighbour of the current node
        
        // If the neighbour is valid (-1 means no neighbour) and it hasn't been visited yet
        if (neighbour != -1 && !visited[neighbour])
        {
            dist[neighbour] = dist[node] + 1;  // Update the distance of the neighbour
            dfs(neighbour, edges, visited, dist);  // Recursively visit the neighbour
        }
        // If the neighbour is already visited and exists in the distance map, we found a cycle
        else if (neighbour != -1 && dist.count(neighbour)) 
        {
            ans = max(ans, dist[node] - dist[neighbour] + 1);  // Calculate the length of the cycle and update ans
        }
    }
    
    int longestCycle(vector<int>& edges) 
    {
        int n = edges.size();  // Number of nodes
        vector<bool> visited(n);  // Visited array to track visited nodes
        
        // Iterate through all nodes in the graph
        for (int i = 0; i < n; i++)
        {
            if (!visited[i])
            {  // If the node hasn't been visited, perform DFS
                unordered_map<int, int> dist;  // Distance map to track how far we are from the start node
                dist[i] = 1;  // Distance of the start node is 1
                dfs(i, edges, visited, dist);  // Perform DFS from the current node
            }
        }
        
        return ans;  // Return the length of the longest cycle or -1 if no cycle is found
    }
};

227305