#C8575. Minimum Time to Complete Tasks

    ID: 52572 Type: Default 1000ms 256MiB

Minimum Time to Complete Tasks

Minimum Time to Complete Tasks

You are given n tasks and a list of m dependency pairs. Each task takes exactly 1 unit of time to complete. A dependency (a, b) means that task a must be finished before task b can start. Tasks that are independent can be executed concurrently.

The goal is to determine the minimum time required to finish all the tasks. This can be modeled using a directed graph where each node represents a task, and each dependency represents a directed edge. If the graph contains a cycle (i.e., the tasks cannot be completed due to cyclic dependencies), the answer should be -1.

The minimum time corresponds to the number of levels in a topological ordering of the tasks. In LaTeX, the result can be described as follows:

$$\text{Time} = \text{number of levels in the topologically sorted order} $$

If there is a cycle, output ( -1 ).

inputFormat

The first line contains two integers (n) and (m) — the number of tasks and the number of dependencies, respectively. Each of the next (m) lines contains two integers (a) and (b), indicating that task (a) must be completed before task (b) can start.

outputFormat

Print a single integer representing the minimum time required to complete all tasks. If the tasks cannot be completed due to a cycle in the dependencies, print (-1).## sample

5 4
0 1
1 2
2 3
3 4
5