#K35187. Course Schedule Ordering

    ID: 25476 Type: Default 1000ms 256MiB

Course Schedule Ordering

Course Schedule Ordering

Given a set of courses and their prerequisite relationships, determine a valid order in which all courses can be taken such that each course's prerequisites are completed before the course itself. If no such order exists due to cyclic dependencies, print the error message "Cannot complete all courses."

This problem can be modeled as finding a topological ordering in a directed graph. An edge from course (u) to course (v) indicates that course (u) is a prerequisite for course (v). Use algorithms like Kahn's algorithm to obtain the ordering. Input is to be taken from STDIN and results should be printed to STDOUT.

inputFormat

The first line of input contains two integers (c) and (r), where (c) is the number of courses and (r) is the number of prerequisite relationships. Each of the next (r) lines contains two integers representing a course and its prerequisite.

outputFormat

If it is possible to complete all courses, output a valid order as a sequence of course numbers separated by a space. Otherwise, output the string "Cannot complete all courses.".## sample

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