mvst

Description

The variance of an array a is defined as Var(a)=1|a|xa(xμa)2. Here, μa=1|a|xax is the average of the values in a. The variance is a measure of dispersion, meaning it measures how far a set of numbers is spread out from their average value.

Hiura is monitoring a communications network. This network consists of N devices numbered from 0 to N1. There are M two-way communication channels numbered from 0 to M1. The i-th communication channel connects two distinct devices U[i] and V[i] with a wavelength W[i], the wavelength used in the communication channel. It is guaranteed that the communication network is connected. In other words, for every pair 0x<y<N, devices x and y are linked (directly or indirectly) by a series of communication channels.

Hiura, surprisingly, loves conformity. Because of this, he wants to pick a spanning tree of the network with the minimum variance. A spanning tree of the network is a subset of the network which includes all the N devices, is connected, and contains N1 two-way communication channels.

The variance of a spanning tree is defined as the variance of the wavelengths of its communication channels. In other words, if a spanning tree consists of the communication channels numbered b1,b2,,bN1, then the variance of the spanning tree is equal to the variance of the array [W[b1],W[b2],,W[bN1]].

Find the minimum variance over all possible spanning trees of the network.

Implementation Details

You should implement the following procedure.

double solve(int N, int M, std::vector<int> U, std::vector<int> V, std::vector<int> W)
  • N: The number of devices in the network.
  • M: The number of communication channels in the network.
  • U,V,W: arrays of integers of length M, describing the devices that the communication channels connect and the wavelengths the communication channels have.
  • This procedure should return one floating point number containing the minimum possible variance of all possible spanning trees of the network.
  • The return value will be considered correct if the absolute difference between the return value of your function and the correct answer is at most 1.
  • This procedure is called exactly once for each test case.

Constraints

  • 2N500000
  • N1M500000
  • 0U[i]N1 for each i such that 0i<M
  • 0V[i]N1 for each i such that 0i<M
  • 0W[i]1000000 for each i such that 0i<M
  • U[i]V[i] for each i such that 0i<M
  • The network is connected

Subtasks

Subtask Score Additional Constraints
1 3 M10
2 12 M3000; W[i]3000 for each i such that 0i<M
3 14 M200
4 23 M3000
5 33 M100000
6 8 M300000
7 7 No additional constraints.

Examples

Consider the following call.

solve(4, 6, [0, 1, 2, 0, 0, 3], [1, 2, 3, 2, 2, 0], [3, 9, 7, 5, 6, 2])

For this example, the minimum variance spanning tree is achieved when we use the 0-th, 3-rd, and 5-th communication channels.

The wavelengths of the communication channels used is a=[2,3,5].

We can calculate the mean as follows.

μa=13(2+3+5)=103

After that, we can calculate the variance as follows.

Var(a)=13((2103)2+(3103)2+(5103)2)=149

Because the return value is considered correct if the absolute difference is at most one from the correct answer, returning values such as 1, 239, or 2 will also be considered correct.

Sample Grader

Input Format:

N M
U[0] V[0] W[0]
U[1] V[1] W[1]
...
U[M - 1] V[M - 1] W[M - 1]

Output Format:

V

Here, V is the floating point number returned by solve.


Time Limit: 4 Seconds
Memory Limit: 1024MB
Your best score: 0
Source: SEATST 2025 (Day 1)

Subtask Score
1 3
2 12
3 14
4 23
5 33
6 8
7 7
8 0