PrevNext

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.

  • It is guaranteed to be able to receive full credit on all Bronze contests with Python, C++, and Java.
  • Python lacks a data structure that keeps its keys in sorted order (the equivalent of set in C++), which is required for some Silver problems.
  • It is almost always possible to receive full credit on all contests with Java, though there are exceptions.

Example - Wormhole Sort

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

import java.io.*;
import java.util.*;
public class wormsort {
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new FileReader("wormsort.in"));
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.setrecursionlimit(1000000)
sys.stdin = open("wormsort.in","r")
sys.stdout = open("wormsort.out","w")
n,m = map(int,input().split())
loc = [0]*n
component = [0]*n
edges = [[] for i in range(n)]

After some optimizations, this solution still passes only the first seven test cases:

import sys
sys.stdin = open("wormsort.in","r")
sys.stdout = open("wormsort.out","w")
n,m = map(int,input().split())
loc = [0]*n
edges = [[] for i in range(n)]
def valid():

We are not sure whether it is possible to modify the above approach to receive full credit (please let us know if you manage to succeed)! Of course, it is possible to pass this problem in Python using DSU (a Gold topic):

# Author: Nicolas Hsu
file = open('wormsort.in')
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))
def find(u):

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:

PrevNext