CT053

Professor Byte’s Course Queue

MediumL6 · Pointer PhantomAcceptance: 0.0%XP: 100

Professor Byte wants the smallest-numbered available course taught first, while respecting prerequisites. Print that deterministic course order, or -1 if a prerequisite cycle makes graduation impossible. Input: n m followed by m directed prerequisite pairs a b (a must precede b), vertices 1..n. Output: lexicographically smallest valid order, or -1.

Example 1:

Input: 4 3 1 3 2 3 3 4
Output: 1 2 3 4

Example 2:

Input: 3 3 1 2 2 3 3 1
Output: -1

Example 3:

Input: 3 0
Output: 1 2 3

Constraints:

1≤n≤200000; 0≤m≤300000.

Tags:

graphs topological-sort priority-queue
Loading...
Test Cases:No test cases
No test cases available.