## Editorial for DMOPC '21 Contest 4 P5 - Graph Generator

Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Authors: Riolku, X_Ray

There are many different solutions for this subtask. One of them will be addressed here.

Observe that for each generation phase , we essentially add a bridge between a copy of the original graph and the amalgamation of all previous generations. We can greedily build our answer for a generation phase off of the answer for generation phase . In this case, let be our graph after generation phases, and let be an active generator node. Here, let be the distance of the shortest path between an unordered pair of nodes and , and means a node that is a part of graph .

The sum of the shortest paths between all unordered pairs of nodes after generation phases = Sum of the shortest paths between all unordered pairs of nodes such that both and are a part of the graph created after generation phases + Sum of the shortest paths between all unordered pairs of nodes such that both and are in the copy of the original graph + Sum of the shortest paths between all unordered pairs of nodes such that is a part of the graph created after generation phases and is a part of the copy of the original graph. Let these be known as terms respectively. Term is our previous solution, and term can be precomputed. Term can be easily calculated through simple combinatorics (left as an exercise to the reader).

We can also greedily calculate the sum of all paths between the active generator node and all other nodes in the graph, with a transition time of .

We can precompute and in . As each transition takes time, the total time complexity of the algorithm is .

Time Complexity:

Observe that graph is now a complete graph. We can express the graph after generations as a layered -nary tree of graphs , where is the number of generator nodes in graph . The distance between any two nodes on two graphs can be easily calculated in using the graph .

Iterating through all pairs of graphs would inevitably exceed the time limit. However, it is observed that the distance between two graphs after generation phases is at most . We can thus use Combinatorics to compute the answer in .

Time Complexity:

There are a few full solutions. One of them will be addressed here.

First, we should change our way of thinking. For each generation phase, rather than imagining a copy of graph being attached to each of the active generator nodes in the th generation phase, we can imagine one copy of graph being attached to each of the generator nodes in a graph with their bottommost node 1. Thus, we can do something broadly similar to Subtask 1. Here, means the th copy of graph , and means a generator node . Simply put, the sum of the shortest paths between all unordered pairs of nodes after generation phases = Sum of shortest paths between all unordered pairs of nodes such that both nodes are in the same copy of graph + Sum of shortest paths between all unordered pairs of nodes such that both nodes are in the copy of + Sum of shortest paths between all unordered pairs of nodes such that both nodes are in different copies of graph + Sum of shortest paths between all unordered pairs of nodes such that one node is in a copy of graph and the other is in the copy of . Let these be known as terms respectively.

Terms can be quickly precomputed, while term is exactly our answer in the previous iteration. Calculating terms and may seem problematic to calculate, but they can be decomposed into more trivial calculations.

First, let us address Term , the sum of shortest paths between all unordered pairs of nodes such that both nodes are in different copies of graph . We can observe a similar phenomenon as in subtask 1, except we have possible bridges of varying lengths. Rather than repetitively performing the same calculations over and over again, we can efficiently combine the calculations into one single calculation.

Where is the number of nodes in graph .

Next, let us address term : the sum of shortest paths between all unordered pairs of nodes such that one node is in a copy of graph and the other is in the copy of . It turns out that we are performing calculations nearly identical to those in subtask 1: the only node which changes across the calculations is the generator node used in the copy of . Thus, we can combine the calculations into one single calculation.

One problem remains, which is the large term denoting the sum of all paths between the bottommost node 1 and every other node in graph . However, like in subtask 1, this can be greedily calculated with a transition of .

We can precompute , , and in . and have transitions with time complexity. Thus, our transitions have a total time complexity of .

Time Complexity:

#### Alternative Solution

Another solution is to precompute all distances in as normal, and then split up all paths into multiple parts. A path can be contained within one copy of , or span across copies. In the latter case, there are a few components to consider: the bottom of a path, where we go from the source to node , the top, where we go from a source to a generator node, the middle, where we go from a generator node to a source, the LCA node, where we go from a generator node to a generator node, or the transition edge where we transfer across components.

The solution iterates through all heights of the graph and computes the number of copies on this level and the one below it. Using the precomputed distances that we considered above, we can compute how many edges are contributed for each part of all the paths. For example, to compute the contribution given by the parts of the path that are used as a LCA, take the pairwise sum of the generator nodes, multiply by the number of components at this height, and multiply by the number of nodes in one of the sub-graphs under this level, squared. This is admittedly a rather mathy-solution.

Time Complexity: