This problem is equivalent to Minimal Labels from Codeforces. Treat the "label" of a vertex in "Minimal Labels" as the completion time of a course in "Course Schedule II." So it suffices to solve the CF problem (editorial) and then output the inverse permutation.

Implementation

Time Complexity: O(N+logN)\mathcal{O}(N + \log N)

import heapq
n, m = map(int, input().split())
out = [0] * (n + 1) # Number of outgoing nodes
radj = [[] for _ in range(n + 1)] # Reverse graph
for _ in range(m):
a, b = map(int, input().split())
radj[b].append(a)
out[a] += 1

Join the USACO Forum!

Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!