Choosing a Language

Authors: Nathan Wang, Benjamin Qi

What languages you can use for programming contests.

What Languages Does The USACO Support?

The most popular languages that USACO supports are C++17, Java, and Python 3. C is also supported, but it's essentially a strictly inferior version of C++ and doesn't have the built-in data structures that are often used.

What Are The Differences Between C++11 and C++17?

If you're just starting out, you probably won't be using any C++17-specific features, so submitting in either C++11 or C++17 should suffice. For information about the features introduced in C++11, C++14, and C++17, check the links below.

What Are The Differences Between Python 2 and Python 3?

As the link below mentions, there are many differences between Python 2 and 3. Python 3 is newer and an overwhelming majority of USACO contestants use it over Python 2.

What Language Should I Start Out With?

In general, we recommend the following:

  • If you already know one or more of these languages, just use the one you are most comfortable with.
  • If you don't know any of these languages, you might as well start with C++, as C++ users generally don't need to worry as much about their solutions being a constant factor too slow. Furthermore, most modules currently lack Java and Python support.

Don't overthink choosing a language -- you can always change languages later!

Can I Pass Every Problem in Every Language?

C++ is typically faster than Java, which in turn is typically faster than Python. Although both Python and Java receive two times the C++ time limit in USACO, this is not the case for most other websites (ex. Codeforces, CSES). Even with the extended time limits, Python and Java sometimes have trouble passing.

  • As of the 2022-23 season, USACO staff usually ensure that it is possible to receive full credit with all of C++, Python, and Java on Bronze and Silver problems. However, this is not guaranteed.
  • Python is too slow for most Gold and Platinum problems.
  • We don't have any examples of USACO problems that are impossible to pass with Java, though there are instances where the official C++ code is not fast enough to receive full credit if translated into equivalent Java code.

Example - Wormhole Sort (USACO Silver Jan 2020)

The Java solution presented in the analysis requires over 3s to run (out of a time limit of 4s).

import java.util.*;
public class wormsort {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(""));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
loc = new int[n];
component = new int[n];

A comparable C++ solution runs in less than 800ms:

#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<int> loc, lhs, rhs, weight;
vector<vector<int>> edges;
vector<int> component;
void dfs(int curr, int label) {
if (component[curr] == label) return;

A comparable Python solution only passes the first five test cases:

import sys
sys.stdin = open("", "r")
sys.stdout = open("wormsort.out", "w")
n, m = map(int, input().split())
loc = [0] * n
component = [0] * n

It is possible to optimise this approach to pass all testcases. This takes around 3.8s to run.

def main():
f = open("", "rb")
n, m = map(int, f.readline().split())
loc = [*map(int, f.readline().split())]
edges = [[] for _ in range(n)]
weights = []
def valid(loc, minW):
component = [-1] * n
numcomps = 0

Finally, the approach below uses DSU (a Gold topic), which takes around 1s to run:

# Author: Nicolas Hsu
file = open("")
N, M = map(int, file.readline().split())
P = tuple(map(int, ("0 " + file.readline()).split()))
W = [tuple(map(int, file.readline().split())) for i in range(M)]
W.sort(key=lambda w: -w[2])
par = list(range(N + 1))

What Am I Expected to Know?

You should know how to code in at least one of the languages listed above before continuing onto the Bronze section of this guide. For a more detailed list on what you should know, read the "Expected Knowledge" module.

Module Progress:

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!