Table of Contents

ExplanationImplementation

Official Analysis

Explanation

For the first few test cases where t=ct=c, we can solve the problem by constructing an MST with tt being the weights. If we let T=tT = \sum t and C=cC = \sum c, V=TC=T2V = T \cdot C = T^2 will be minimized when TT is minimized.

To solve the problem completely, we will have to minimize the product of those two sums, V=CTV = C \cdot T. The solution space contains all possible MSTs with their respective TT and CC values. We can view each of those possible solutions as a 2D-point (T,C)(T, C). There exists a curve C(T)=VTC(T) = \frac{V}{T} such that every point on it has the same VV value. As an inverse proportion function, it is convex, so the best solution must lie somewhere on the left-most curve, i.e. on the lower convex hull. Therefore, it suffices to check all points on the lower convex hull of our solution space to find the minimal VV value. Nevertheless, we cannot really calculate the convex hull since it would require all (MN1)\binom{M}{N-1} points to be known. Instead, we use another approach below.

At the beginning, we choose two points on the lower convex hull, AA and BB, as our starting points for our search. AA should be the point where we minimize TT and BB where we minimize CC. We then want to find a point PP which is left-most from line ABAB. The area of the triangle ABPABP is proportional to the distance between the line ABAB and the point PP, given AB|AB| is constant. Therefore, we only have to find out the triangle ABPABP with maximum area. Using the cross product, we get 2AABP=AB×AP=(xBxA)(yPyA)(yByA)(xPxA)=yP(xBxA)xP(yByA)+xA(yByA)yA(xBxA)-2A_{ABP} = AB \times AP = (x_B - x_A)(y_P - y_A) - (y_B - y_A)(x_P - x_A) = y_P(x_B - x_A) - x_P(y_B - y_A) + x_A(y_B - y_A) - y_A(x_B - x_A). Note that xA(yByA)yA(xBxA)x_A(y_B - y_A) - y_A(x_B - x_A) is constant and can be ignored. Thus, AABP-A_{ABP} is proportional to yP(xBxA)xP(yByA)y_P(x_B - x_A) - x_P(y_B - y_A). As our goal was to maximize AABPA_{ABP}, we should here minimize yP(xBxA)xP(yByA)y_P(x_B - x_A) - x_P(y_B - y_A) instead. This can be done by taking this value as the weight of our MST algorithm and then build the MST. The result would be our new point PP. If PP is on the right side of vector ABAB (thus on the left side of line ABAB), it is also on the convex hull. We then use Divide-and-Conquer and solve the same problem with either points AA, PP or PP, BB. For each of these points, we update the current best solution if the new point is better. Otherwise, if PP is on the right side of line ABAB, we do not have to search further with PP since it is not on the lower convex hull anymore.

Since TT and CC are positive integers, the lower convex hull between AA and BB consists of k(N1)tmax=50745k \leq (N-1) \cdot t_{max} = 50745 points. For each of those kk points, we run Kruskal's algorithm in O(MlogM)\mathcal{O}(M \log M). Therefore, the total time complexity is O(kMlogM)\mathcal{O}(kM \log M). This suffices for the given test cases.

C++

Implementation

#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
using pii = pair<int, int>;
Code Snippet: DSU (Click to expand)
struct Edge {
int x;
int y;

Join the USACO Forum!

Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!