#K88127. Cycle Detection in Directed Graph
Cycle Detection in Directed Graph
Cycle Detection in Directed Graph
You are given a directed graph with \(V\) vertices and \(E\) edges. Your task is to determine whether the graph contains at least one cycle. A cycle in a directed graph exists if there is a path starting and ending at the same vertex while traversing the direction of edges.
The input is given via standard input and the output should be printed to standard output. If the graph contains a cycle, print YES; otherwise, print NO.
inputFormat
The first line contains two space-separated integers \(V\) and \(E\), representing the number of vertices and edges respectively. Each of the following \(E\) lines contains two space-separated integers \(u\) and \(v\), representing a directed edge from vertex \(u\) to vertex \(v\>.
outputFormat
Output a single line: YES if the graph contains at least one cycle, or NO if it does not.
## sample4 4
0 1
1 2
2 3
3 1
YES