<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 build_graph(self, edges: List[int]) -> Tuple[Dict[int, Set[int]], Dict[int, Set[int]]]:
        graph = {i: set() for i in range(self.n)}
        r_graph = {i: set() for i in range(self.n)}
        for s,e in enumerate(edges):
            if s != -1 and e != -1:
                graph[s].add(e)
                r_graph[e].add(s)
        
        return graph, r_graph
    
    def dfs1(self, cur: int) -> None:
        self.visited1[cur] = True
        for adj in self.graph[cur]:
            if not self.visited1[adj]:
                self.dfs1(adj)
        self.nums[self.idx] = cur
        self.idx += 1
    
    def dfs2(self, cur: int) -> None:
        self.sc[cur] = self.cur_c
        for adj in self.r_graph[cur]:
            if self.sc[adj] == -1:
                self.dfs2(adj)
    
    def longestCycle(self, edges: List[int]) -> int:
        self.n = len(edges)
        self.graph, self.r_graph = self.build_graph(edges)
        self.nums = [-1]*self.n
        self.idx = 0
        self.visited1 = [False]*self.n
        self.sc = [-1]*self.n
        self.cur_c = 0
        
        # first dfs
        for node in range(self.n):
            if not self.visited1[node]:
                self.dfs1(node)
        
        # second dfs
        for i in range(self.n-1, -1, -1):
            node = self.nums[i]
            if self.sc[node] == -1:
                self.dfs2(node)
                self.cur_c += 1
        
        cache = {}
        for i, val in enumerate(self.sc):
            cache[val] = cache.get(val, 0)+1
        
        max_val = max(cache.values())
        return -1 if max_val == 1 else max_val
107865