You are given a directed graph containing n vertices labeled from 0 to n-1. Each node can have at most one outgoing edge. The graph is defined by an array edges, where edges[i] is the node that i points to. If edges[i] == -1, then node i does not point to any other node.
Your task is to determine the length of the longest cycle present in the graph.
A cycle means starting from a node, following the directed edges, and eventually returning back to the same node.
If the graph contains no cycles, return -1.
Input: edges = [1,2,0,5,3,4]
Output: 3
Explanation:
There are two cycles: - Cycle 0 → 1 → 2 → 0 (length 3) - Cycle 3 → 5 → 4 → 3 (length 3) The longest cycle length is 3.
Input: edges = [-1, 0, -1, 2]
Output: -1
Explanation:
All paths eventually lead to nodes with no outgoing edges, so no cycle exists.
Accepted:
Submission: