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
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
- Set
to be equal to . - Mark node
as visited. - If all
nodes have been visited, terminate the algorithm. - 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). - Add the edge between nodes
and to the minimum spanning tree. - Set
to be equal to . - Return to step
.
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)?
Subtasks
In test cases worth
Input Specification
The first line of input consists of a single integer,
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 2 0
2 3 1
Sample Output 1
1
Sample Input 2
5
4 2 1
1 3 0
4 1 1
3 5 1
Sample Output 2
2
Sample Explanations
In the first case, it's possible that the edge between nodes
In the second case, it's possible that Spiderman's algorithm may produce
a spanning tree with weight
Comments