#K66182. Cycle Detection in Directed Graph
Cycle Detection in Directed Graph
Cycle Detection in Directed Graph
You are given a directed graph with (V) vertices (numbered from 0 to (V-1)) and its adjacency list representation. Your task is to determine whether the graph contains a cycle. A cycle is a path that starts and ends at the same vertex, and a self-loop is considered a cycle.
The input begins with an integer (V) which denotes the number of vertices. This is followed by (V) lines where each line describes the outgoing edges from a vertex. The line starts with an integer (k) representing the number of outgoing edges from that vertex, followed by (k) space-separated integers listing the destination vertices.
Print "True" if the graph contains a cycle; otherwise, print "False".
For example, consider the following input:
4 0 1 2 1 3 0
This describes a graph with 4 vertices and no cycle, so the output is "False".
inputFormat
The first line of input contains an integer (V) indicating the number of vertices in the graph. The next (V) lines describe the graph’s adjacency list. Each of these lines begins with an integer (k), the number of directed edges from the current vertex, followed by (k) integers representing the adjacent vertices.
outputFormat
Output a single line containing either "True" if the graph contains a cycle or "False" if it does not.## sample
4
0
1 2
1 3
0
False