#C8853. Job Scheduling with Dependencies

    ID: 52881 Type: Default 1000ms 256MiB

Job Scheduling with Dependencies

Job Scheduling with Dependencies

You are given a set of jobs numbered from 1 to \(J\) and a list of \(D\) dependency pairs. Each dependency is represented as a pair \((u,v)\), meaning that job \(u\) must be completed before job \(v\) can start.

Your task is to determine whether it is possible to execute all the jobs in a valid order that respects the given dependencies. If a valid order exists, output one such ordering; otherwise, output Impossible.

This problem can be modeled as a directed graph where each job is a node and each dependency is a directed edge. A standard approach to solving this is by performing a topological sort on the graph. Note that if the graph contains a cycle, then no valid ordering exists, and the output should be Impossible.

inputFormat

The first line of input contains two integers \(J\) and \(D\) where \(J\) is the number of jobs and \(D\) is the number of dependencies.

The following \(D\) lines each contain two integers \(u\) and \(v\), indicating that job \(u\) must be completed before job \(v\).

outputFormat

If it is possible to complete all jobs while respecting the dependencies, output a single line containing a valid order of jobs (separated by spaces). Otherwise, output a single line containing the word Impossible.

## sample
4 0
1 2 3 4

</p>