Kattis - Quantum Superposition
Authors: Andrew Wang, Benjamin Qi
Time Complexity:
Main Idea: Find all possible lengths of routes in both universes. Then we can preprocess all possible sums of lengths to answer each query in time.
Finding All Possible Lengths of Routes
For each node, store all possible lengths of a route that ends at it in a set.
We can do this via DP on the topological sort. When we consider a node, we can
add 1 to all the lengths reaching a previous node and insert them into the set
for the current node. Using a bitset
rather than a set
is faster (and gives
slightly shorter code).
We can repeat this process for both universes to find the total lengths of all paths reaching the end node.
Finding All Possible Sums
Once we know all possible path lengths for each universe, we can find all possible sums of lengths. Just loop through both universe's route lengths and add them together.
C++
#include <bitset>#include <iostream>#include <queue>#include <vector>using namespace std;static const int MAX_N = 1000;static const int MAX_Q = 2000;int n[2], m[2];
Java
import java.io.*;import java.util.*;public class QuantumSuperposition {static final int MAX_N = 1000;static final int MAX_Q = 2000;static int[] n = new int[2];static int[] m = new int[2];static ArrayList<Integer>[][] g = new ArrayList[2][MAX_N + 1];
Python
MAX_N = 1000MAX_Q = 2000from collections import dequen = [0, 0]m = [0, 0]g = [[[] for _ in range(MAX_N + 1)] for _ in range(2)]back = [[[] for _ in range(MAX_N + 1)] for _ in range(2)]dp = [[[False for _ in range(MAX_Q + 1)] for _ in range(MAX_N + 1)] for _ in range(2)]
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!