<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:
#         def find_loop(root, visited):
#             """Return the first node which belongs to a loop. Otherwise, return -1"""
#             if visited[root] >= 0:
#                 return -1
#             visited[root] = root
#             next = edges[root]
   
#             while next != -1 and visited[next] < 0:
#                 visited[next] = root
#                 next = edges[next]

#             if next != -1 and visited[next] == root:
#                 return next
#             return -1

#         def count_loop(node):
#             """Count the size of a loop""" 
#             next_node = edges[node]
#             count = 1
#             while next_node != node:
#                 count += 1
#                 next_node = edges[next_node]
#             return count

#         n = len(edges)
#         visited = [-1] * n
#         loop_nodes = []
#         for i in range(n):
#             node = find_loop(i, visited)
#             if node > -1:
#                 loop_nodes.append(node)

#         return max([count_loop(node) for node in loop_nodes], default=-1)





# topo sort
class Solution:
    def longestCycle(self, edges: List[int]) -> int:
        n = len(edges)

        # get the in-degree of all nodes
        in_deg_cnts = [0] * n
        for edge in edges:
            if edge == -1: continue
            in_deg_cnts[edge] += 1

        # use approach similar to topological sort to iteratively calculate the effective
        # in-degrees of all nodes, by taking into account the in-degrees of their ancestors
        S = collections.deque(n for (n, idc) in enumerate(in_deg_cnts) if idc == 0)
        while S:
            dst = edges[S.pop()]
            if dst == -1: continue
            in_deg_cnts[dst] -= 1
            if in_deg_cnts[dst] == 0: S.append(dst)
        
        # graph is a DAG, therefore no cycle exists
        if not any(in_deg_cnts): return -1

        # run DFS to find the longest possible cycle length
        res, checked = -1, set()
        for x in range(n):
            if in_deg_cnts[x] == 0 or x in checked: continue

            # NOTE: b/c we skip all nodes that have (effective) in-degrees of 0 in
            #       the preprocessing step above, all nodes added to the `visited`
            #       set must be part of a cycle, which also means all nodes have
            #       exactly one outgoing edge, thus no need to check if `x == -1`
            visited = set()
            while x not in visited:
                visited.add(x)
                x = edges[x]
            res = max(res, len(visited))

            checked.update(visited)

        return res
36116