CSES - Course Schedule II

Authors: Benjamin Qi, Paul Chen


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.

C++

#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
vector<int> out(n + 1); // Number of outgoing nodes
vector<vector<int>> radj(n + 1); // Reverse graph

Java

import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
Kattio io = new Kattio();
int n = io.nextInt();
int m = io.nextInt();
List<List<Integer>> graph = new ArrayList<>();
for (int i = 0; i <= n; i++) { graph.add(new LinkedList<>()); }

Python

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!