Detect Cycle in Directed Graph
Mediumjavapythonccppjavascript
Given a directed graph, determine whether it contains a directed cycle.
Input Format
The first line contains two integers V and E. Each of the next E lines contains a directed edge u v (from u to v).
Output Format
Print 1 if a directed cycle exists, otherwise print 0.
Example 1
Input
3 3 0 1 1 2 2 0
Output
1
Explanation: 0 -> 1 -> 2 -> 0 forms a directed cycle.
- 1 <= V <= 100000
- 0 <= E <= 200000
- 0 <= u, v < V