#K15876. Task Dependency Ordering

    ID: 24454 Type: Default 1000ms 256MiB

Task Dependency Ordering

Task Dependency Ordering

You are given a list of tasks. Each task has a unique identifier, a type and possibly a list of prerequisite tasks. A task can be independent or dependent. A dependent task may require one or more other tasks (its prerequisites) to be completed before it can be executed.

Your goal is to output an order in which the tasks can be completed such that all dependencies are satisfied. When more than one task is available for execution (i.e. with zero pending prerequisites), choose the one with the lexicographically smallest identifier.

Constraints:

  • The number of tasks \( t \) satisfies \(1 \le t \le 1000\).
  • Each task ID is a string with length between 1 and 10.
  • The total number of characters in all task identifiers does not exceed \(10^5\).
  • There are no circular dependencies unless it is impossible to complete the tasks.

If it is impossible to complete all tasks (due to a circular dependency), output IMPOSSIBLE.

inputFormat

The input is read from standard input and has the following format:

Line 1: An integer \( t \), the number of tasks.
Next \( t \) lines: Each line describes a task in the following space-separated format:
  ID Type [prerequisite1 prerequisite2 ...]

Where:

  • ID is a string (unique task identifier).
  • Type is either dependent or independent.
  • The list of prerequisites (only for dependent tasks) consists of space separated task IDs. For independent tasks, this list is empty. </pre>

outputFormat

Output a single line to standard output. If it is possible to complete all tasks, print the task identifiers in a valid order, separated by a single space. If it is impossible, print IMPOSSIBLE.

## sample
5
task1 dependent task2
task2 independent
task3 dependent task1 task2
task4 independent
task5 dependent task4 task1
task2 task4 task1 task3 task5