#K85997. Task Execution Order

    ID: 36765 Type: Default 1000ms 256MiB

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:

  1. The first line contains two integers n and m representing the number of tasks and the number of dependencies respectively.
  2. The next n lines each contain two integers start and priority for a task. Task i is described on line i+1.
  3. The following m lines each contain two integers a and b 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.

## sample
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