#C13823. Cycle Detection in Directed Graphs
Cycle Detection in Directed Graphs
Cycle Detection in Directed Graphs
You are given a directed graph. Your task is to determine whether the graph contains a cycle.
The graph is given as follows:
- The first line contains an integer n representing the number of vertices.
- The second line contains an integer m representing the number of directed edges.
- Each of the next m lines contains two strings u and v, indicating a directed edge from vertex u to vertex v.
If the graph contains a cycle, output True
. Otherwise, output False
.
Note: A cycle is defined as a path (of one or more edges) starting and ending at the same vertex.
inputFormat
The input is read from standard input (stdin) and is formatted as follows:
- The first line contains an integer n: the number of vertices in the graph.
- The second line contains an integer m: the number of directed edges in the graph.
- Each of the following m lines contains two space-separated strings representing an edge from the first vertex to the second vertex.
outputFormat
Print a single line to standard output (stdout): True
if the graph contains a cycle, and False
otherwise.
4
4
A B
A C
B D
C D
False
</p>