#C1946. Course Completion
Course Completion
Course Completion
You are given a total number of courses labeled from 0 to numCourses - 1, and a list of prerequisite pairs. Each pair [a, b] indicates that to enroll in course a, you must first complete course b. Your task is to determine if it is possible for a student to finish all courses. In other words, check whether there exists an ordering of courses such that all prerequisites are met.
This problem can be modeled using a directed graph where an edge from b to a indicates that course b must be taken before course a. The student can complete all courses if and only if the graph has no cycle. One standard algorithm to solve this is Kahn's algorithm for topological sorting.
The problem can be formally stated as follows:
Given an integer \(n\) (representing the number of courses) and a list of prerequisite pairs, determine if it is possible to finish all courses. Return True
if it is possible, otherwise return False
.
Note: In the input, the courses are numbered from 0 to \(n-1\).
inputFormat
The input is given via stdin in the following format:
- The first line contains an integer numCourses, the total number of courses.
- The second line contains an integer m, the number of prerequisite pairs.
- The next m lines each contain two integers separated by a space, representing a prerequisite pair in the format:
course prerequisite
.
outputFormat
Output a single line via stdout which is either True
or False
indicating whether it is possible to finish all courses.
4
0
True
</p>