#K37272. Detect Cyclic Dependencies in a Directed Graph
Detect Cyclic Dependencies in a Directed Graph
Detect Cyclic Dependencies in a Directed Graph
You are given a directed graph with \(N\) vertices and \(M\) edges. Your task is to determine whether the graph contains a cycle. If the graph has at least one cycle, print CYCLE
; otherwise, print NO CYCLE
. This problem can be solved using topological sorting techniques (such as Kahn's algorithm) which check if every vertex is visited in the ordering.
Example:
Input: 3 3 1 2 2 3 3 1</p>Output: CYCLE
In the above example, the edge sequence (1,2), (2,3), (3,1) forms a cycle. In contrast, if the input graph is acyclic, the output should be NO CYCLE
.
inputFormat
The first line contains two integers (N) and (M) — the number of vertices and edges respectively. Each of the next (M) lines contains two integers (u) and (v), representing a directed edge from vertex (u) to vertex (v).
outputFormat
Print a single line: CYCLE
if the graph contains at least one cycle, or NO CYCLE
if it is acyclic.## sample
3 3
1 2
2 3
3 1
CYCLE