#K59547. Performance Scheduling with Dependencies
Performance Scheduling with Dependencies
Performance Scheduling with Dependencies
You are given n performances and m dependency relations. Each dependency is specified as a pair of integers a and b which means performance a must be scheduled before performance b (i.e. $a \to b$). Your task is to determine an ordering of all performances that satisfies all the dependencies. If no valid ordering exists (due to cyclic dependencies), output IMPOSSIBLE
.
It is guaranteed that the input will be given through standard input (stdin) and the answer should be printed to standard output (stdout). The ordering, if it exists, should be printed as space-separated integers representing the performances.
inputFormat
The first line of input contains two integers n and m ($1 \leq n \leq 10^5$, $0 \leq m \leq 10^5$) representing the number of performances and the number of dependencies, respectively.
The next m lines each contain two integers a and b indicating that performance a must come before performance b.
Note: Input is provided via stdin.
outputFormat
If a valid ordering exists, output the order as n space-separated integers. Otherwise, output IMPOSSIBLE
.
Note: Output should be printed to stdout.
## sample5 4
1 2
1 3
3 4
4 5
1 2 3 4 5