#C11731. Minimum Time to Complete Tasks
Minimum Time to Complete Tasks
Minimum Time to Complete Tasks
Given a set of tasks and a list of prerequisite relations among them, determine the minimum number of time units required to complete all tasks. Each task takes exactly one time unit to complete, and multiple tasks can be performed simultaneously if all their prerequisites have been satisfied.
This problem can be modeled using a directed acyclic graph (DAG) where each node represents a task and an edge from task ( u ) to task ( v ) represents that task ( u ) must be completed before task ( v ) can start. If the graph contains a cycle, meaning not all tasks can be completed, output the error message: "There exists a cycle in the graph."
inputFormat
The input is read from standard input (stdin). The first line contains a single integer ( n ) representing the number of tasks. The second line contains an integer ( m ) representing the number of prerequisite pairs. The following ( m ) lines each contain two integers ( u ) and ( v ), indicating that task ( u ) must be completed before task ( v ) can begin.
outputFormat
Output to standard output (stdout) a single integer representing the minimum number of time units required to complete all tasks. If the graph contains a cycle, output exactly the string "There exists a cycle in the graph.".## sample
4
3
1 0
2 1
3 2
4