<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 __init__(self):
        self.counter = None
        self.visited = None

    def calLengthOfCycle(self, startNode: int, edges: List[int], length):
        print(startNode)
        if self.visited[startNode] == True:
            return length
        self.visited[startNode] = True
        # -1
        return self.calLengthOfCycle(edges[startNode], edges, length+1)

    def longestCycle(self, edges: List[int]) -> int:


        incomingEdges = [0 for _ in range(len(edges))]
        #self.cycleLength =  [-1 for _ in range(len(edges))]
        self.visited = [False for _ in range(len(edges))]

        for idx in range(0, len(edges)):
            if edges[idx] == -1:
                continue
            incomingEdges[edges[idx]] +=1
        
        roots = []
        for idx in range(0, len(edges)):
            if incomingEdges[idx] == 0:
                roots.append(idx)
        
        for root in roots:
            currentNode = root

            while currentNode != -1:
                self.visited[currentNode] = True
                nextNode = edges[currentNode]
                if nextNode == -1:
                    break #end of the dfs
                incomingEdges[nextNode] -=1
                if incomingEdges[nextNode] != 0:
                    break
                currentNode = nextNode   
        ans = 0
        print(roots)
        print(self.visited)
        for idx in range(0, len(edges)):
            if self.visited[idx] == True:
                continue
            print(self.visited, idx)
            ans = max(ans, self.calLengthOfCycle(idx, edges, 0))
        if ans ==0:
            return -1
        return ans
                



    
    # def calLengthOfCycle(self, node, edges, count) -> int:
    #     count = count+1
    #     self.counter[node] = count
    #     parsedNodes = [node]
    #     node = edges[node]
       
    #     while(node != -1 and self.counter[node] == -1):
    #         print(node)
    #         count = count+1
    #         self.counter[node] = count
    #         parsedNodes.append(node)
    #         node = edges[node]
            
        
    #     if count == 1 or node == -1 or  self.counter[node] == 0:
    #         for inode in parsedNodes:
    #             self.counter[inode] = 0 #no cyle
    #         return 0
        
    #     return count-self.counter[node]+1
            

    # def longestCycle(self, edges: List[int]) -> int:

    #     # incomingEdges = [0 for _ in range(len(edges))]
    #     self.counter =  [-1 for _ in range(len(edges))]

    #     # for idx in range(0, len(edges)):
    #     #     if edges[idx] == -1:
    #     #         continue
    #     #     incomingEdges[edges[idx]] +=1

    #     # print(incomingEdges)
    #     ans = 0
    #     for nodeIdx in range(0, len(edges)):
    #         if edges[nodeIdx] == nodeIdx:
    #             ans = max(ans, 1)
    #             continue
    #         if self.counter[nodeIdx] == -1:
    #             ans = max(ans, self.calLengthOfCycle(nodeIdx, edges, 0))
    #             print(self.counter)

    #     print(self.counter)
    #     if ans == 0:
    #         return -1
    #     return ans
        
961