Schedule the Courses
Courses have prerequisites (edge u → v means u must precede v). Print the lexicographically smallest valid ordering, or IMPOSSIBLE on a cyclic dependency.
Input: the first line has N and M; each of the next M lines has a directed edge u → v (1-based).
Output: the smallest topological order (space-separated), or IMPOSSIBLE.
Constraints:
- 1 ≤ N ≤ 104
- 0 ≤ M ≤ 2×104
- multiple edges and self-loops may appear
Sample Tests
Input 1
4 3 1 2 3 2 3 4
Output 1
1 3 2 4
Input 2
2 2 1 2 2 1
Output 2
IMPOSSIBLE
Discussion (opens after you solve — no spoilers before)
No comments yet.