<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:
    def longestCycle(self, edges: List[int]) -> int:
        # since each one has <= 1 outgoing edge, this is just finding largest SCC

        n = len(edges)

        ll = [100000 for _ in range(n)]

        self.t = 0
        seen = [0] * n # 0 for unseen, 1 for in progress, 2 for done
        
        self.current = 1
        self.ans = 1
        def dfs(node):
            self.t += 1

            ll[node]=self.t

            seen[node] = 1
            
            nxt = edges[node]
            if nxt == -1 or seen[nxt] == 2:
                seen[node] = 2
                return
            
            elif seen[nxt] == 0:
                dfs(nxt)

            if ll[nxt] < ll[node]:
                ll[node] = min(ll[nxt], ll[node])
                self.current += 1
            else:
                self.current = 1

            self.ans = max(self.ans, self.current)
            seen[node] = 2
        
        for i in range(n):
            if seen[i] == 0:
                dfs(i)
        
        return self.ans if self.ans != 1 else -1




        # seen = [0] * n
        # # 0 for not seen, 1 for in progress, 2 for done

        # sets = []
        # self.ans = 1



        # def postorder(node):
        #     seen[node] = 1


        #     nxt = edges[node]
            
        #     if nxt == -1:
        #         # no outgoing edges. therefore not a cycle
        #         seen[node] = 2
        #         return
        #     if seen[nxt] == 2:
        #         seen[node] = 2
        #         return
        #     elif seen[nxt] == 1:
                
        #         seen[node] = 2
        #         defs(nxt,2)
        #         self.ans = max(self.ans,2)
        #     else:
        #         postorder(nxt)
        #     seen[node] = 2

        
        # def defs(node,cur):
        #     nxt = edges[node]
            
        #     if nxt == -1:
        #         # no outgoing edges. therefore not a cycle
        #         return
            
        #     if seen[nxt] == 1:
        #         defs(nxt,cur+1)
        #         self.ans = max(cur+1,self.ans)
        #     seen[node] = 2


        # for i in range(n):
        #     if seen[i] == 0:
        #         postorder(i)
        #     elif seen[i] == 1:
        #         print("WTF")
        # return self.ans if self.ans != 1 else -1
945