In this problem, we are given a complete graph with n nodes, and n-3 “special” edges. We need count the number of spanning trees that contain all of these edges. This is an easier version of the div1 medium.

First, if there are any cycles in E, then the answer is zero, since a spanning tree can’t have a cycle.

The actual topology of E doesn’t matter, what matters is the sizes of the connected components it forms. In addition, since E has n-3 edges and no cycles, there will be exactly 3 components left over.

Let the sizes of these components be s_0, s_1, s_2.

To form a spanning tree, we must add two more edges. There are three possible edges we can add, of which we need to choose exactly two of them.
1) We add an edge between s_0, s_1. This can be done in s_0*s_1 ways. (A)
2) We add an edge between s_0, s_2. This can be done in s_0*s_2 ways. (B)
3) We add an edge between s_1, s_2. This can be done in s_1*s_2 ways. (C)

Thus, the final answer is the sum of products of choosing two of the edges, which is A*B + A*C + B*C = (s_0*s_1) * (s_0*s_2) + (s_0*s_1) * (s_1*s_2) + (s_0*s_2) * (s_1*s_2).

The time it takes to find the sizes of the components is similar to using Kruskal’s and union find which takes O(n * \alpha(n)), where \alpha(n) is the inverse ackermann function.

This is a classic problem where we want to find a hamiltonian path in a tournament graph. There may already exist some resources online about it (see this link for example: https://math.stackexchange.com/questions/1682492/prove-that-every-tournament-contains-at-least-one-hamiltonian-path). This is an easier version of the div1 hard.

We will prove an answer always exists by constructing one. We do this by induction.

First, our base case is when n=1, which is just a path with a single node.

Now, consider an arbitrary n > 1, and suppose we have constructed a hamiltonian path of the vertices {0,1,2,..,n-2} and we want to add vertex n-1.

For each node {0,1,2,…,n-2}, color node i red if there is an edge from i to n-1, otherwise color node i blue if there is an edge from n-1 to i.

Suppose the hamiltonian path on {0,1,2,..,n-2} is p_0, p_1, …, p_{n-2}.
If p_0 is blue, we can append node n-1 to the beginning. If p_{n-2} is red, then we can append node n-1 to the end. So, now let’s consider the harder case when p_0 is red and p_{n-2} is blue.

We want to find p_i and p_{i+1} such that p_i is red and p_{i+1} is blue.

This must exist, since the first node in our path is red, and the last node is blue, so at some point, it must switch from red to blue, at which point we can insert n.

Thus, this shows that we can always extend the hamiltonian path by 1, which completes our induction and gives us an algorithm.

To implement this, we can follow the steps in our induction above. There are n times where we add nodes, and each of those steps takes O(n) time (O(n) to classify the previous nodes as red or blue, and O(n) time to insert our new node into this list).

Div I Easy::MinimizeAbsoluteDifferenceDiv1
In this problem, you are given five variables a,b,c,d,e and you want to put four of them in the expression |?/? – ?/?| so the resulting value is minimized. This is a harder version of the div2 easy.

This is an implementation problem. Here, we can try all 5! permutations of replacement, and take the minimum possible value. The time complexity is O(n! * n) where n = 5.

In this problem, you are given a complete graph with n nodes, and some special edges. We want to count the number of spanning trees that contain all of these edges. This is a harder version of the div2 medium.

This is a classic problem (a similar problem can be found here: http://codeforces.com/problemset/problem/156/D). An editorial (only in russian) can be found here: http://codeforces.com/blog/entry/4005?locale=en, which I’ll briefly summarize. The first few steps are similar as in the div2 solution.

Namely, if there are any cycles in E, the answer is zero.

Otherwise, the actual topology of E doesn’t matter, what matters is the size of each connected component.

The proof can be seen as a generalization of Caley’s formula on the number of trees with n nodes. You can also see this math stack exchange link for some more information: https://math.stackexchange.com/questions/2550064/proof-of-generalized-cayleys-formula

The complexity analysis is similar to div2 medium, where the time is dominated by needing to find the sizes of the components, which is O(n * \alpha(n)), where \alpha(n) is the inverse ackermann function.

This is also a somewhat classic problem where we want to find a hamiltonian circuit in a tournament graph. There may already exist some resources online about it (see this link for example: https://math.stackexchange.com/questions/1682492/prove-that-every-tournament-contains-at-least-one-hamiltonian-path). This is an harder version of the div2 hard.

Another approach is as follows.

Let’s add the nodes in one by one. We will proceed by induction.

After each step, instead of maintaining a hamiltonian circuit, we will have a list of hamiltonian circuits for each strongly connected components of the nodes 0,1,2,…,i-1. In addition, since this is a tournament graph, the SCCs from a line from left to right, with all SCC edges in the left pointing to the right.

Now, to add node i, we will find the leftmost SCC that i has an edge to and the rightmost SCC that i has an edge to. Call these SCCs indices A and B respectively.

If A > B, then that means we need to insert a new SCC with only node i in between A and B.
If A == B, then that means we need to insert node i into SCC A, we can brute force where to insert i (there must exist such an insertion point because there is an edge to and from i in this component).

If A < B, then we can merge all components from A to B, and also merge their cycles together (we use node i to connect B -> i -> A to make a big cycle). Since we already have cycles in each component, we can rotate each one so that the appropriate node is at the beginning/end.

At the end, if there is only one SCC, we can return the cycle of that component, otherwise, return the empty list.