#C4866. Minimum Task Completion Time
Minimum Task Completion Time
Minimum Task Completion Time
You are given n tasks labeled from 1 to n. Some tasks have dependencies; specifically, if there is a dependency (u, v), it means that task u must be completed before task v can begin. Multiple tasks can be executed concurrently in one time unit provided that all their prerequisites have been completed.
Your goal is to determine the minimum number of time units required to complete all tasks. In each time unit, you may execute any number of tasks that are currently available with no remaining dependencies. If the dependency graph contains a cycle, it is impossible to complete all tasks, and you should return -1
.
The problem can be formulated using a layered topological sort. Formally, if we let T be the set of tasks and D the set of directed edges representing dependencies, the answer is the minimum number t such that all tasks can be assigned a level (time unit) satisfying:
\( \text{level}(v) = \max_{(u,v) \in D}\{\text{level}(u)\} + 1 \)
with the base case that tasks with no prerequisites have a level of 1.
inputFormat
The input is read from standard input (stdin) and is formatted as follows:
The first line contains two integers n and m, where n is the number of tasks (1 ≤ n ≤ 10^5) and m is the number of dependencies.
Each of the following m lines contains two integers u and v (1 ≤ u, v ≤ n) representing a dependency where task u must be completed before task v.
outputFormat
Output a single integer to standard output (stdout): the minimum number of time units required to complete all tasks. If it is impossible to complete all tasks due to a circular dependency, output -1.## sample
4 3
1 2
2 3
3 4
4