#C4969. Task Ordering

    ID: 48565 Type: Default 1000ms 256MiB

Task Ordering

Task Ordering

You are given (N) tasks and (M) dependencies between them. Each dependency is represented as a pair ((u,v)), meaning that task (u) must be completed before task (v). Your goal is to determine a valid ordering of tasks such that every task is executed after all its prerequisites. If no valid ordering exists (i.e. the dependency graph contains a cycle), output (IMPOSSIBLE).

The problem is a classical topological sorting challenge where you must take into account the ordering constraints imposed by the dependencies.

inputFormat

The first line of input contains an integer (T), the number of test cases. Each test case is described as follows:

The first line of each test case contains two integers (N) (the number of tasks) and (M) (the number of dependencies). The next (M) lines each contain two integers (u) and (v), representing that task (u) must be completed before task (v).

outputFormat

For each test case, output a single line containing the ordering of tasks separated by spaces if a valid ordering exists. Otherwise, output (IMPOSSIBLE).## sample

1
4 3
1 2
1 3
3 4
1 2 3 4

</p>