#K85997. Task Execution Order
Task Execution Order
Task Execution Order
You are given n tasks and m dependency relations between these tasks. Each task is described by two integers: the start time and the priority. A dependency (a, b) indicates that task a must be completed before task b can be started.
The execution order of tasks must satisfy the following conditions:
- All dependencies must be fulfilled (i.e. if there is an edge \(a \rightarrow b\), then task \(a\) must appear before task \(b\) in the order).
- Among the tasks that are available to execute (i.e. all prerequisites have been completed), choose the task with the smallest start time. If several tasks have the same start time, choose the one with the higher priority.
If it is impossible to complete all tasks due to cyclic dependencies, output IMPOSSIBLE
.
The ordering can be formulated as finding a topological order of a directed graph \(G=(V, E)\) with an additional custom ordering on the available vertices.
inputFormat
The input is read from standard input and is formatted as follows:
- The first line contains two integers
n
andm
representing the number of tasks and the number of dependencies respectively. - The next
n
lines each contain two integersstart
andpriority
for a task. Task i is described on line i+1. - The following
m
lines each contain two integersa
andb
meaning that task a must be completed before task b can be started.
outputFormat
If a valid ordering exists, output one line with n
integers representing the order of tasks (task indices separated by a space). Otherwise, output a single line with the string IMPOSSIBLE
.
5 4
1 4
2 3
1 1
3 2
2 5
1 2
1 3
2 4
4 5
1 3 2 4 5