Explanation
We apply meet in the middle technique by splitting the continuous chain of songs into chains of four songs and the center one. We'll compute for each node all the subsets of four different artists such that there is a path going into the this node. Similarly, by reversing the graph, compute the paths going out of the node. In the combining step, loop through all nodes and fix one as the center node. Having precomputed the two groups of subsets (ingoing and outgoing paths), we want to find two sets that are disjoint, i.e no common element. Using Inclusion-Exclusion count for a given set in the first group the number of sets in the second group with one common element. The upperbound for the number of sets is .
Implementation
Time Complexity:
C++
#include <algorithm>#include <array>#include <iostream>#include <numeric>#include <unordered_map>#include <vector>using namespace std;typedef array<char, 4> State;
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!