Alpha Nerd (Hard)
After hours of exciting offscreen action (not described in this problem), the Infinity War has just about reached its conclusion, with Thanos defeated and the Avengers preparing to give a series of inspirational speeches! One important question remains – which heroes should receive the most credit for the victory? With so many superheroes crammed into a single adventure, there simply won't be enough recognition to go around!
For instance, who should receive credit for being the Avengers' alpha nerd, playing the most important tech-related role? Iron Man is the obvious choice, but Spiderman has been studying computer science for a whole few months and wants a shot at the title as well! This prospect annoys Iron Man, so he plans to give Spiderman a competitive programming problem to put him in his place. He'll even give him a nice, easy one – computing the weight of a graph's minimum spanning tree.
Iron Man will generate a complete, undirected, weighted graph with N (2 ≤ N ≤ 300,000) nodes, numbered from 1 to N. Each of its N×(N − 1)/2 edges will be given a weight of either 0 or 1. Iron Man has already decided on the weights of N − 1 of its edges – in particular, the edge connecting nodes Ai and Bi (1 ≤ Ai, Bi ≤ N) will have a weight of Wi (0 ≤ Wi ≤ 1). The N − 1 fixed edges are all distinct, and there are no cycles amongst them. Each of the remaining edges in the graph will randomly receive a weight of either 0 or 1.
Spiderman is feeling confident about this challenge, having just learned about Prim's algorithm for constructing minimum spanning trees in school. Unfortunately, he's misremembered the algorithm! His version involves starting at the first node and greedily following minimum-weight edges around the graph until he's constructed a path which passes through all N nodes. In pseudocode:
- Set c to be equal to 1.
- Mark node c as visited.
- If all N nodes have been visited, terminate the algorithm.
- Find the minimum-weight edge which connects node c to some unvisited node i (in the case of tied minimum weights, choose a random minimum-weight edge).
- Add the edge between nodes c and i to the minimum spanning tree.
- Set c to be equal to i.
- Return to step 2.
This algorithm will always terminate and produce a valid spanning tree on the graph, but said spanning tree may not end up having the minimum possible weight at all. To some extent, this depends on how lucky Spiderman gets (in terms of what edge weights are randomly generated for the graph and which tied minimum-weight edges are chosen in Step 4 of the algorithm).
Assuming Spiderman gets as unlucky as possible, what could be the largest possible difference between the weight of the spanning tree returned by his algorithm, and the weight of the same graph's minimum spanning tree (which Prim's actual algorithm would return)?
In test cases worth 10/25 of the points, N ≤ 6.
The first line of input consists of a single integer, N.
N − 1 lines follow, the i-th of which consists of three space-separated integers, Ai, Bi, and Wi, for i = 1..(N − 1).
Output a single integer, the maximum possible difference between the algorithm's answer and the weight of the graph's actual minimum spanning tree.
Sample Input 1
3 1 2 0 2 3 1
Sample Output 1
Sample Input 2
5 4 2 1 1 3 0 4 1 1 3 5 1
Sample Output 2
In the first case, it's possible that the edge between nodes 1 and 3 could receive a weight of 0. It's then possible that Spiderman's algorithm could randomly choose the weight-0 edge leading to node 2, and then be forced to choose the weight-1 edge leading to node 3 before terminating. This spanning tree (1 ↔ 2 ↔ 3) has a total weight of 1, while the graph's minimum spanning tree (2 ↔ 1 ↔ 3) has a total weight of 0.
In the second case, it's possible that Spiderman's algorithm may produce a spanning tree with weight 2 greater than the minimum spanning tree.
Point Value: 25 (partial)
Time Limit: 4.00s
Memory Limit: 64M
Added: Apr 23, 2018
C++03, PAS, C, HASK, ASM, RUBY, PYTH2, JAVA, PHP, SCM, CAML, PERL, C#, C++11, PYTH3