Editorial for APIO '13 P2 - Toll
Submitting an official solution before solving the problem yourself is a bannable offence.
Problem
This problem wants us to assign weights to some edges and pick a minimum spanning tree (MST) from the resultant graph to maximize a certain revenue function on the MST. For convenience, we'll call the initially weighted edges "default edges" and the edges we need to price (assign weights to) "Mr G's edges".
Too many MSTs
Given that the number of possible MSTs can be exponential in general graphs, the selection of the MST itself would normally already be intractable. However, in this case it turns out that when the edge weights are largely distinct, the number of MSTs is actually not a lot. In fact, notice that if we fix some edges to be in the MST, and guarantee that the remaining edge weights are all distinct, then there is only one unique MST. This leads directly to the following observation.
Observation 1. Suppose we fix some of Mr G's edges to be in the MST, and remove the rest from the graph. Then there's only one possible MST.
Proof. Contract the connected components formed from Mr G's fixed edges to single vertices. All the edge weights in this graph are now distinct, and the only MST of this graph can be extended to the MST of the original graph by adding in Mr G's edges. □
With this observation, it turns out that once we price those fixed Mr G's edges such that they are included in the MST, there will only be one MST to consider, regardless of the weights of Mr G's edges. This implies that, no matter how we price Mr G's edges, there are only at most possible MSTs: one for each subset of Mr G's edges to be fixed inside.
How to fix?
Now our problem is: How do we price some edges such that they are included in the MST? One trivial way is simply to price all of them at . But our revenue will then also be . Indeed, we want to price them as high as possible, while keeping them in the MST. Let us first figure out the maximum weight we can price a single edge, , to keep it in the MST of any graph.
Observation 2. If an edge is the only edge that has the maximum weight in any cycle of a graph, then cannot be in the MST.
Proof. Suppose the MST contains . Remove from the MST, and we get two disjoint trees that can be connected by an edge from that cycle. The weight of is strictly less than that of , thus we can replace by to obtain an MST of smaller total weight, a contradiction. □
Thus, with slight modification, we can come up with a necessary condition for any edge belonging to Mr G to be in the MST:
Condition 1. The edge must not have the maximum weight in any cycle of the graph, unless it shares the weight with a default edge in the cycle.
Let us price Mr G's edges so that they will all fulfill Condition 1. Now observe that once they do so, they must be in the MST!
Observation 3. As long as Mr G's edges all fulfill Condition 1, they must be in the MST.
Proof. Continually find any cycle in the graph and remove the edge of maximum weight since it cannot be in the MST (breaking ties by preferring to remove default edges). Since Mr G's edges all fulfill Condition 1, for each cycle they are contained in they will never be removed. Eventually there will be no cycles and the graph will form an MST with all of Mr G's edges. □
Fulfilling Condition 1
Let us first start with the graph with none of Mr G's edges, and find its MST using a standard MST algorithm like Kruskal's. We shall add in Mr G's edges one by one, ensuring that the edges fulfill Condition 1 all the time. For the first edge , consider the cycle formed from adding to the initial MST (let's call it the MST-cycle for ). cannot be the only edge with maximum weight in this cycle, so we set its weight to be the same as the maximum edge weight in the current MST-cycle, .
Now, what about the other cycles containing ? It turns out that there is no need to consider them at all!
Observation 4. The maximum edge weight in any cycle containing is no less than , the maximum edge weight in the MST-cycle for .
Proof. Suppose the maximum edge weight in a cycle containing is . Then is greater than all edge weights in both cycles. It is easy to see that even without Mr G's edge , there must be a new cycle containing with edges only from these two cycles. But is the only edge with maximum weight in this new cycle. Hence it cannot be in the initial MST, a contradiction. □
It follows immediately that by pricing at , it cannot be the only edge with maximum weight in any cycle, fulfilling Condition 1.
We now price at to displace the default edge with weight from the MST, forming a resultant spanning tree.
Claim. The resultant tree is an MST for the new graph that includes .
Proof. Consider the MST for the graph with only default edges. Every default edge not in this MST must have the maximum edge weight in some cycle and hence cannot be in the MST for the new graph. Since Mr G's edge fulfills Condition 1, it must be in the MST. Thus the edges in are the only ones that can be in the MST. □
Now that we have a new "initial MST", we can add in the next edge similarly. However, now the MST-cycle for might contain , which we also want to keep inside the MST. If has the maximum weight in this cycle, will definitely displace it and hence we must lower the weight of . Naturally, should be set to at most the next-maximum weight – any larger will mean either or has to go! can then also be set to that same weight, to displace the default edge with that weight.
By lowering the weight of , will still fulfill Condition 1 for all other cycles. The argument that the other cycles containing do not need to be considered is similar to that for ; the only difference is that the initial MST now contains . Thus also fulfills Condition 1. The resultant tree is now an MST for the graph with and added in, by a similar argument.
Hence, in a similar vein, we can go on to price all of Mr G's edges maximally. Eventually, they will all fulfill Condition 1 – and along the way, we have also built up an MST containing all of them!
We now have a simple solution for our original task: Iterate through all subsets of Mr G's edges to fix in the MST by adding in the edges one by one in a depth-first manner. Each time an edge is added in, find its MST-cycle and set its weight to be the maximum default edge weight in the cycle. Mr G's edges in the cycle cannot have weights greater than , so update all their weights too. After adding in the edges for each subset, we have an MST whose revenue can be computed using a simple tree traversal. The runtime is . This should solve subtask 3 to get 56 points.
Compressing the tree
Our initial MST is huge: a total of edges, with up to . However, it turns out that not many of these default edges are important. If we look at our simple solution, the only default edges we need to consider are those that are maximum in some MST-cycle when we add in Mr G's edges. We shall call these "maximal default edges". It turns out that there are only at most of them!
Observation 5. The maximal default edges for a given subset of Mr G's edges must be part of the maximal default edges for the full set of Mr G's edges.
Proof. We can add in Mr G's edges in arbitrary order to obtain the same set of maximal default edges. Thus, when adding in the full set of Mr G's edges, we add in the given subset of Mr G's edges first to obtain the maximal default edges for that subset. □
So how do we find the maximal default edges for the full set of Mr G's edges? We know that they cannot be in the MST with all Mr G's edges, and yet are present in the initial MST with only default edges. Hence, we can easily find them by computing both MSTs and doing a quick comparison between them. This takes time.
Once the maximal default edges are obtained, the initial MST can now be compressed to at most edges connecting components, using a simple union-find data structure. Our simple solution now finds the MST-cycle, prices the weights, and computes the revenue in time. Hence the runtime is . This is expected to obtain 100 points.
Additional remarks
If we do not add in Mr K's edges one by one in a depth-first manner, but instead recompute the pricing for all edges for each subset of Mr K's edges, an additional time is invoked. This gives solutions of and depending on whether the tree compression is done. The former should still solve subtask 3, while the latter only solves subtask 4 to get 78 points.
There is another solution for subtasks 3 and 4 that does not involve adding in Mr G's edges one by one: For each subset of Mr G's edges, compute the MST with the entire subset fixed inside from scratch. Now use all edges that are present in the initial MST but not present in this MST to update the edge weights of Mr G's edges to fulfill Condition 1. The runtime is or depending on whether the compression was done. This approach is more widely used, but more difficult to speed up to the model runtime to get full score. Nevertheless, it is possible to do so using specific data structures to quicken the updating of the edge weights. The only contestant to solve subtask 4 used this method.
Credits
Initial task statement by Raymond Kang Seng Ing.
Refinement of task statement by Assoc Prof Chang Ee-Chien.
Solution sketch by Raymond Kang Seng Ing and Shen Chuanqi.
Verification of proofs by Assoc Prof Chang Ee-Chien and Dr Sung Wing Kin.
Additional testing and verification by Harta Wijaya.
Comments