Editorial for SAC '22 Code Challenge 5 Junior P4 - Course Requirements


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Author: maxcruickshanks

Subtask 1

It suffices to brute force through all possible permutations of courses and output any valid one.

Time Complexity: \mathcal{O}(N!)

Subtask 2

Realize that this problem is a classical topological sorting problem.

Maintain a queue for all the courses that have no prerequisites. Once a course has been satisfied, satisfy all the other courses that had this as a prerequisite, and, if that newly satisfied course does not have any other prerequisites, push it into the queue. Finally, output the order of courses in that queue.

Time Complexity: \mathcal{O}(N + \sum_{i=1}^N C_i)


Comments

There are no comments at the moment.