## WC '17 Contest 4 S4 - Alpha Nerd

View as PDF

Points: 17 (partial)
Time limit: 1.8s
Memory limit: 64M

Author:
Problem type
##### Woburn Challenge 2017-18 Round 4 - Senior Division

After hours of exciting offscreen action (not described in this contest), 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 nodes, numbered from to . Each of its edges will be given a weight of either or . Iron Man has already decided on the weights of of its edges – in particular, the edge connecting nodes and will have a weight of . The 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 or .

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 a random node and greedily following minimum-weight edges around the graph until he's constructed a path which passes through all nodes. In pseudocode:

1. Set to be a random node between and , inclusive.
2. Mark node as visited.
3. If all nodes have been visited, terminate the algorithm.
4. Find the minimum-weight edge which connects node to some unvisited node (in the case of tied minimum weights, choose a random minimum-weight edge).
5. Add the edge between nodes and to the minimum spanning tree.
6. Set to be equal to .

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, which node is started at in Step of the algorithm, and which tied minimum-weight edges are chosen in Step ).

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 of the points, .

#### Input Specification

The first line of input consists of two space-separated integers, and .
lines follow, the -th of which consists of two space-separated integers, and , for .

#### Output Specification

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
1 2

#### Sample Output 1

1

#### Sample Input 2

5 3
3 1
1 5
2 1

#### Sample Output 2

3

#### Sample Explanations

In the first case, it's possible that the edge between nodes and could receive a weight of , while the edge between nodes and could receive a weight of . It's then possible that Spiderman's algorithm could start at node , then randomly choose the weight- edge leading to node , and finally be forced to choose the weight- edge leading to node before terminating. This spanning tree has a total weight of , while the graph's minimum spanning tree has a total weight of .

In the second case, it's possible that Spiderman's algorithm may produce a spanning tree with weight greater than the minimum spanning tree.