<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 {
    //TC :  DFS : O(N+E) n number of nodes and E no of edges
    //SC : O(3N)
public:
    int ans = -1;

    void DFS(vector<int>& edges,vector<int>&visited,vector<int>&pathVisited,vector<int>&dist, int node, int distance,vector<vector<int>>&adj)
    {
        visited[node] = 1;
        pathVisited[node] = 1;
        dist[node] = distance; //distanc of current nodee to reach itself

        for(auto e : adj[node])
        {
            if(visited[e] == -1)
            {
                DFS(edges,visited,pathVisited,dist,e,distance + 1,adj); //distance + 1 becaus th ajacent node will be at 1 dist from parent
            }

            else if(pathVisited[e] == 1)
            {
                ans = max(ans,distance - dist[e] + 1); //standard lngth formula right - left +1 //total distance - distancee at current index +1
            }
        }

        pathVisited[node] = 0;

    }
    int longestCycle(vector<int>& edges) 
    {
        //so as it is directed graph we will find the max and min distance using DFS algorithm

        //now the graph has elements in form of adjecency list only this edge case is -1 so will handle it
        //instead of creating new adjacency list

        //the code will be same as detecting a cycle in directed graph via DFS
        //it will consist of visited and visitedpath vector - same as detecting cycle question
        //thi question will require one new vector - distance vector

        

        int size = edges.size();
        vector<int>visited(size,-1);
        vector<int>pathVisited(size,0);
        vector<int>dist(size,0);

        vector<vector<int>>adj(size);

        for(int j = 0; j < size; j++) //creeate the adjacency list by eliminatinng -1
        {
            if(edges[j] != -1)
            {
                adj[j].push_back(edges[j]);
            }
        }

        for(int i = 0; i < size; i++) //for each vertx in graph call DFS
        {
            if(visited[i] == -1)
            {
                DFS(edges,visited,pathVisited,dist,i,0,adj);
            }
        }

        return ans;
    }
};
169595