<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:
    bool dfs(int ind,vector<int> adjlist[],vector<int>&vis,vector<int>&pathvis){
        vis[ind]=1;
        pathvis[ind]=1;
        for(auto &it:adjlist[ind]){
            if(vis[it]==-1){
                if(dfs(it,adjlist,vis,pathvis)) return true;
            }
            else if(pathvis[ind]==1){
                return true;
            }
        }
        pathvis[ind]=-1;
        return false;
    }
    int longestCycle(vector<int>& edges) {
        int n=edges.size();
        vector<int> adjlist[n];
        for(int i=0;i<n;i++){
            if(edges[i] == -1) continue;
            adjlist[i].push_back(edges[i]);
        }
        vector<int> indegree(n,0);
        set<int> topo;
        for(int i=0;i<n;i++){
            for(auto &jt:adjlist[i]) indegree[jt]++;
        }
        queue<int> q;
        for(int i=0;i<n;i++) if(indegree[i]==0) q.push(i);
        while(!q.empty()){
            int node = q.front();
            q.pop();
            topo.insert(node);
            for(auto &it:adjlist[node]){
                indegree[it]--;
                if(indegree[it]==0) q.push(it);
            }
        }
        vector<int> cyclenodes;
        for(int i=0;i<n;i++){
            if(topo.find(i) == topo.end()) cyclenodes.push_back(i);
        }
        
        vector<int> vis(n,-1),pathvis(n,-1);
        int ans=0;
        map<int,int> mpp;
        for(int i=0;i<cyclenodes.size();i++){
            int res=0;
            if(pathvis[cyclenodes[i]]==-1 && mpp.find(cyclenodes[i])==mpp.end()){
                dfs(cyclenodes[i],adjlist,vis,pathvis);
                for(int i=0;i<n;i++){
                    if(pathvis[i] == 1 && mpp.find(i)==mpp.end()) {
                        mpp[i]++;
                        res++; 
                    }
                }
            }
            ans=max(ans,res);
        }
        if(ans==0) return -1;
        return ans;
    }
};
215365