WC '17 Contest 4 S4 - Alpha Nerd (Hard)

View as PDF

Submit solution

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

Author:
Problem type

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 \le N \le 300\,000) nodes, numbered from 1 to N. Each of its \frac{N \times (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 A_i and B_i (1 \le A_i, B_i \le N) will have a weight of W_i (0 \le W_i \le 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:

  1. Set c to be equal to 1.
  2. Mark node c as visited.
  3. If all N nodes have been visited, terminate the algorithm.
  4. 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).
  5. Add the edge between nodes c and i to the minimum spanning tree.
  6. Set c to be equal to i.
  7. 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)?

Subtasks

In test cases worth 10/25 of the points, N \le 6.

Input Specification

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, A_i, B_i, and W_i, for i = 1 \dots (N-1).

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 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 \leftrightarrow 2 \leftrightarrow 3) has a total weight of 1, while the graph's minimum spanning tree (2 \leftrightarrow 1 \leftrightarrow 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.


Comments

There are no comments at the moment.