We need to parse the problem. We have a connected undirected graph with n nodes and m edges, each with a speed limit s_i. We must choose a spanning tree (n-1 edges) to keep, and we can modify the speed limits of the kept edges by increments/decrements of 1 (each change counts as 1). Goal: after modifications, the maximum speed limit among the kept edges is exactly k. We want to minimize total number of changes.
We can choose which edges to keep (i.e., which spanning tree) and how to modify each kept edge.
Observations: The problem reduces to: find a spanning tree T, and assign new values s'_e for each e in T, such that max_e s'e = k, and minimize sum{e in T} |s'_e - s_e|, over all spanning trees.
But we can also consider that we can change edges arbitrarily (increase or decrease) at cost equal to absolute difference. So given a tree T, the optimal modifications to achieve max = k is: we need to ensure that after changes, max is exactly k. That means for each edge, we can set its final value to something between? Actually we need max exactly k, not less than k. So we must have at least one edge with final value exactly k, and all others ≤ k. And we want to minimize total changes.
Given T, the optimization is: we can lower any edge > k down to k (or less) but we could also raise edges < k up to k? But raising edges < k would increase cost and might be unnecessary because we need max exactly k; if there is already an edge with s >= k, we could lower it to exactly k, and lower any edges > k, and maybe not raise any edges. However, if all edges have s < k, then we need to raise at least one edge to k (or higher then lower? But raising to exactly k is cheaper than raising above then lowering? Actually raising to exactly k is the minimal for that edge). So the optimal per T: we need to ensure that after changes, there is at least one edge with value = k and all others ≤ k. Since we can lower edges > k and raise edges < k.
We can think: For each edge e with original s_e, the cost to make it ≤ k is max(0, s_e - k) if we lower it to k? Actually if we set final value to something ≤ k, the minimal cost is min_{0 ≤ t ≤ k} |s_e - t|. That's: if s_e ≤ k, we can leave it unchanged (t = s_e) cost 0. If s_e > k, the minimal to make it ≤ k is to set it to k (since any t ≤ k, the distance is minimized at t = k because s_e > k, so distance = s_e - k). So to make it ≤ k, minimal cost is max(0, s_e - k).
To make an edge exactly k, cost is |s_e - k|. But we might need at least one edge to be exactly k. However, note that if we lower an edge from >k to k, we achieve exactly k for that edge. If we raise an edge from <k to k, we also achieve exactly k. So among the edges we keep, we need at least one edge to have final value exactly k. If there is already an edge with s_e = k, we can keep it unchanged (cost 0) and have that edge as max. But we also need to ensure that no edge exceeds k; any edge with s_e > k must be lowered to at most k; but if we lower it to exactly k, it could also serve as the max. However, if we lower an edge from >k to exactly k, the cost is s_e - k. That's the same as the cost to lower it to ≤ k (the minimal to not exceed). So the extra cost to make an edge exactly k (compared to just making it ≤ k) is zero for edges with s_e ≥ k? Actually for s_e ≥ k, we can set it to exactly k at cost s_e - k, which is the same as making it ≤ k (since the minimal cost to bring to ≤ k is s_e - k). So for edges with s_e ≥ k, we can achieve both "≤ k" and "=k" with same cost (by setting to exactly k). For edges with s_e < k, making it ≤ k costs 0 (by leaving as is), but making it =k costs k - s_e (raising). So we need to select at least one edge to serve as the "k" edge. If we have any edge with s_e ≥ k, we can choose one of them to be exactly k at no extra cost beyond the lowering cost that we would already pay to ensure it's ≤ k (if we are lowering it, we'd set it to k). But careful: For edges with s_e ≥ k, we would lower them to k anyway (or maybe lower them further to something < k? But that would increase cost because we would lower more than necessary; the minimal cost for that edge to be ≤ k is s_e - k, achieved by setting it to k. So yes, if we want to minimize total cost, for any edge with s_e > k, the optimal final value is exactly k, because any lower would cost more. For edges with s_e = k, we keep at k (cost 0). So essentially, for edges with s_e ≥ k, the cost to bring to ≤ k is s_e - k, and that yields final value k. So each such edge automatically ends at k, which satisfies the "exactly k" condition if we keep at least one such edge.
If there is no edge with s_e ≥ k in the tree, i.e., all edges have s_e < k, then we need to raise at least one edge to k. The minimal additional cost is to pick the edge with smallest (k - s_e) (i.e., the largest s_e) and raise it to k, costing k - s_e. For other edges, since s_e < k, we can leave them unchanged (cost 0) because they are already ≤ k.
Thus, for a given spanning tree T, the minimal cost to achieve max = k is:
Let A = set of edges in T with s_e ≥ k. Let B = set with s_e < k.
If A is non-empty: total cost = sum_{e in T} max(0, s_e - k) (since for edges in B, cost 0; for edges in A, cost s_e - k). Because we already have at least one edge that becomes k (by lowering) and thus max exactly k. Note: what about edges with s_e exactly k? That's included in A (≥ k) and cost 0. So formula holds.
If A is empty: total cost = (min_{e in T} (k - s_e))? Actually we need to raise one edge to k; we could also raise multiple but minimal is to raise only one. So total cost = (k - max_{e in T} s_e) because we choose the edge with largest s_e to raise to k, costing k - max_s. All other edges remain unchanged (cost 0). However, we must also consider if there are edges with s_e > k? That case is covered by A non-empty. So for A empty, total cost = k - max_{e in T} s_e.
But wait: what if there is an edge with s_e > k but we decide not to use it as the max? Actually we must lower it to ≤ k; we could lower it to something below k (cost more) but that would be suboptimal. So the optimal for that edge is to lower to k. So if A is non-empty, we can achieve exactly k without extra cost beyond lowering. So the formula for A non-empty is sum_{e in T} max(0, s_e - k). That includes edges in A: cost s_e - k; edges in B: 0.
But what if we have multiple edges in A, we get many edges lowered to k, but cost is sum of s_e - k for all edges with s_e > k. That seems necessary because they must be lowered to at most k, and the minimal is to lower them to k.
Now the problem: we can choose T to minimize this cost.
Thus we need to find a spanning tree that minimizes:
If tree contains at least one edge with s >= k: cost = sum_{e in T, s_e > k} (s_e - k) [edges with s_e = k contribute 0]. (We'll denote c_e = max(0, s_e - k) for all edges.)
If tree contains no edge with s >= k: cost = k - max_{e in T} s_e.
But careful: In the first case, if there is an edge with s >= k, we also need to ensure that after lowering those edges to k, the max is exactly k, but if there are also edges with s < k, max = k, fine. So cost is sum_{e in T} c_e.
In the second case, all s_e < k. Then the max after no modifications would be max_s (which is < k). To achieve max exactly k, we need to increase some edge(s). The minimal is to raise the edge with largest s_e to k, costing k - max_s. But could we also raise a different edge? Raising the one with largest s_e gives the smallest increase. So cost = k - max_{e in T} s_e.
Thus the total cost for tree T is:
cost(T) =
if (max_{e in T} s_e >= k) then sum_{e in T} c_e
else (k - max_{e in T} s_e)
where c_e = max(0, s_e - k).
We need to find min cost over all spanning trees.
Observation: For edges with s > k, c_e is positive; edges with s <= k have c_e = 0. Edges with s = k have c_e = 0 and also count as >= k.
Thus the cost function is not simply sum of c_e over tree because of the else case where we might not have any edge with s >= k. In that case, we incur cost k - max_s, which could be less than sum of c_e (since all c_e are zero) but positive.
The trade-off: We could include an edge with s >= k, even if its c_e is high, to switch to the sum-of-c_e regime. However, including such an edge might also bring other edges with positive c_e that we might otherwise avoid. But maybe we can choose a tree that avoids edges with s > k as much as possible, but if we avoid all edges with s >= k, then we are in the else regime with cost k - max_s, where max_s is the maximum s among edges in the tree, all < k. That cost could be small if we can get a tree with max_s close to k (i.e., using the largest possible <k edges). Or we could include exactly one edge with s >= k (maybe the one with smallest c_e) and avoid other high c_e edges, so that sum of c_e = c of that edge (plus any other unavoidable positive c edges). But maybe we can avoid all edges with s > k if we include an edge with s = k (c=0) - that would be perfect: then cost = sum of c_e over tree, but since all edges with s>k we can avoid, cost = 0. So if there exists a spanning tree using only edges with s <= k, and includes at least one edge with s = k, then cost 0. More generally, if there is any spanning tree with all edges s <= k and max_s = k (i.e., contains a k edge), then cost 0. But if all spanning trees that contain a k edge also must contain some edges with s>k? Possibly.
Alternatively, if there is a spanning tree that uses only edges with s <= k, but none of them is exactly k, then max_s < k, and cost = k - max_s > 0. Could we do better by including an edge with s > k? Possibly if the cost sum_{c_e} < k - max_s. So we need to compare.
Therefore, we need to compute:
Option 1: Best spanning tree with all edges s < k (i.e., no edge with s >= k). Actually we need to allow s = k? That would be >= k and puts us in case 1, but cost could be 0 if also no s > k edges. But let's treat separately.
Better to think in terms of: The cost function depends on whether the tree contains an edge with s >= k. So we can consider two scenarios:
Scenario A: Choose tree T that contains at least one edge with s >= k. Then cost = sum_{e in T} c_e.
Scenario B: Choose tree T that contains no edge with s >= k (i.e., all s < k). Then cost = k - max_{e in T} s_e.
We then take min over both.
Now we need to find the minimum possible cost under each scenario efficiently.
First, define edges:
- For each edge e: weight w_e = s_e, and also compute c_e = max(0, s_e - k).
Also note edges with s_e = k have c_e = 0 and are in the >= k set.
Now scenario A: minimize sum_{e in T} c_e subject to T being a spanning tree and T containing at least one edge with s_e >= k.
If we ignore the "at least one edge with s>=k" constraint, the minimum sum of c_e over all spanning trees is just the MST with respect to c_e. But note that c_e = 0 for all edges with s_e <= k (including those with s = k). So the MST with respect to c_e will try to use as many zero-cost edges as possible. In fact, the MST using c_e as edge weight will first include all edges with c_e = 0 (i.e., s_e <= k) if they can connect the graph. If the graph is connected using only edges with s_e <= k, then the MST cost will be 0. However, if there is a cut that cannot be covered by zero-cost edges, we need to include some positive c_e edges. The MST with respect to c_e will give the minimal sum of c_e. But does that MST guarantee that we have at least one edge with s>=k? Not necessarily: if the graph can be spanned by edges with s <= k (all c_e = 0), then the MST cost is 0 and the tree might consist entirely of edges with s < k (or <=k). If it includes an edge with s = k, then it's fine (cost 0 and satisfies scenario A because it has at least one edge with s>=k). But if all edges in that zero-cost spanning tree have s < k (i.e., no edge with s = k), then such a tree would be scenario B? Actually scenario A requires at least one edge with s>=k. If the zero-cost MST uses only edges with s < k, it does not satisfy scenario A. However, the cost in scenario A formula would still be sum c_e = 0 if we used that tree, but we are not allowed because the condition for using scenario A is that the tree contains an edge with s>=k; otherwise the correct cost is k - max_s, which is > 0. So we must enforce the constraint. However, if we can find a tree with sum c_e = 0 and that tree contains at least one edge with s = k, then scenario A cost 0, which is obviously minimal. If the zero-cost MST uses only s<k edges, then scenario A cost with that tree is not allowed. But maybe there exists another tree with sum c_e = 0 that includes a k-edge. That depends on connectivity: If there exists any spanning tree that uses only edges with s <= k and includes at least one edge with s = k, then we can achieve cost 0. So we need to check if we can span the graph using edges with s <= k and also include at least one edge with s = k. That's equivalent to: Is there a spanning tree that contains a specific edge (any edge with s = k) but only uses edges with s <= k? More generally, we can try to see if we can achieve cost 0: either we have a spanning tree consisting only of edges with s <= k that has max_s = k (i.e., includes a k edge), or we have a tree that includes an edge with s > k but with c_e = 0? No, c_e >0 for s>k. So cost 0 only if we can have a spanning tree with all edges having s <= k and at least one with s = k. Because if we include an s > k edge, its c_e >0, cost >0. So the minimal cost overall could be 0 if such a tree exists.
Otherwise, we need to compute min cost from both scenarios.
Thus algorithm:
- Check if there exists a spanning tree using only edges with s <= k that includes at least one edge with s = k. Actually if there exists a spanning tree composed solely of edges with s <= k (i.e., ignoring edges with s > k) that connects all vertices, and there exists at least one edge with s = k in that tree, then answer 0.
But careful: It could be that the graph can be connected using only edges with s <= k, but the spanning tree might not include a k-edge if we don't have any k-edge at all, or if all k-edges are not necessary for connectivity. But we could choose a tree that includes a k-edge if there is at least one k-edge and it is possible to include it while still having a spanning tree using only <=k edges. That is: we need to know if the subgraph of edges with s <= k is connected; if yes, then we can choose any spanning tree in that subgraph. If that subgraph is connected and there is at least one edge with s = k, then we can simply include that edge in the tree. However, if the subgraph is connected but there are no edges with s = k (i.e., all edges have s < k or > k), then any tree in that subgraph has max_s < k, so scenario B applies; we cannot get cost 0 from scenario A because we lack a k-edge. So cost 0 only if there exists at least one edge with s = k AND the subgraph of edges with s <= k is connected (so we can span the graph using only <=k edges, and include the k-edge). But what if the subgraph of edges with s <= k is not connected? Could we still have a spanning tree with all edges <=k? No, because then it wouldn't connect all vertices; we would need at least one edge with s > k to connect components, which would have c_e > 0. So then cost >0. So condition for cost 0: (1) the subgraph induced by edges with s <= k is connected, and (2) there is at least one edge with s = k. Because then we can pick a spanning tree within that subgraph (all edges <= k) that includes the k-edge, and then cost = sum c_e = 0. Actually do we need to ensure that the spanning tree includes at least one edge with s = k? If the subgraph is connected, we can choose a spanning tree that includes a specific k-edge if we want. But what if the only k-edges are bridges? They might be required anyway. But as long as there is at least one k-edge and the subgraph is connected, we can construct a tree that includes that edge: just take any spanning tree of the subgraph; if it doesn't include that edge, we can add it and remove another edge to maintain tree (since it's connected, there is a cycle). But we need to ensure that the edge we add is present. Actually, if the subgraph is connected, it contains a spanning tree. If that tree doesn't contain any k-edge, we could try to exchange an edge to include a k-edge while keeping the tree within the subgraph (since the subgraph contains all edges <= k). Since the subgraph is connected, there exists a spanning tree that includes any particular edge? Not necessarily: if we want a specific edge e0, we can consider building a tree that includes e0: we can take e0, then run Kruskal including other edges, but we need to ensure connectivity. Since the subgraph is connected, there exists some spanning tree containing e0 if and only if e0 is not a cut-edge? Actually, in a connected graph, an edge is contained in some spanning tree if and only if it is not a bridge? Wait, all edges are in some spanning tree? In a connected graph, for any edge, there exists a spanning tree that contains that edge, provided that edge is not a self-loop? Actually, there is a known property: In a connected graph, an edge is contained in some spanning tree if and only if removing it does not disconnect the graph? Let's think: Suppose we have a connected graph G. Take any spanning tree T. If the edge e is not in T, adding e to T creates a cycle. Then we can remove any other edge in that cycle to get a new spanning tree that contains e. So indeed, for any edge e that is not a self-loop and the graph is connected, we can find a spanning tree that contains e (as long as e is not a self-loop). The process: start with any spanning tree T. If e not in T, add e, then remove any other edge in the cycle (which exists). This yields a spanning tree containing e. So yes, any edge in a connected graph belongs to at least one spanning tree. Condition: e must not be a bridge? Actually, if e is a bridge, then every spanning tree must include e because otherwise the graph would be disconnected. So it's definitely included. If e is not a bridge, we can still include it by the exchange argument. So indeed, in any connected graph, every edge is in some spanning tree. Therefore, if the subgraph of edges with s <= k is connected, and there exists at least one edge with s = k (which is in that subgraph), then there exists a spanning tree of that subgraph (i.e., using only <=k edges) that contains that k-edge. So cost 0 achievable. Good.
Thus, check: if (graph restricted to edges with s <= k) is connected, and there is at least one edge with s = k, answer 0.
But wait: What if there is an edge with s = k but the subgraph of edges with s <= k is not connected? Then we cannot have a spanning tree using only <=k edges, because we need edges with s > k to connect components. Then any spanning tree will contain at least one edge with s > k, which has c_e > 0, so cost > 0. However, it might be possible that we include that k-edge and also some >k edges, and total cost sum c_e could be small. So we need to compute scenario A and B.
Now, scenario A: minimize sum_{e in T} c_e such that T contains at least one edge with s >= k.
If the minimum sum c_e over all spanning trees (without constraint) is achieved by a tree that happens to have at least one edge with s >= k, then that minimum is candidate. But if the unconstrained MST with respect to c_e yields a tree with no edge >=k (i.e., all edges s < k), then that tree is not allowed in scenario A. However, maybe there exists another tree with sum c_e slightly higher that includes a >=k edge, and that could be better than scenario B? So we need to compute the minimum sum c_e among spanning trees that include at least one edge with s >= k.
Similarly, scenario B: minimize (k - max_s) among trees with all edges s < k. That is, we want a spanning tree with maximum s_e as large as possible but still < k (i.e., max_s as close to k as possible). Actually, we want to minimize k - max_s, which is equivalent to maximize max_s among trees with all edges s < k. So we want the maximum possible maximum edge weight in a spanning tree, subject to all edge weights < k. But careful: The tree must be spanning and only use edges with s < k. So we are restricted to the subgraph of edges with s < k (strictly less than k). If that subgraph is connected, we can choose any spanning tree within it. The maximum possible max_s in a spanning tree is the minimum over all spanning trees of the maximum edge? Actually we want to find a tree that maximizes the minimum? Wait, we want a tree that has its largest edge as large as possible. Among all spanning trees of a given graph, the maximum possible value of the maximum edge weight is simply the maximum edge weight present in the graph (since we could include that edge if it's in some spanning tree). But we need a spanning tree that includes that particular edge, and all other edges can be any. So the maximum possible max_s in a spanning tree of the subgraph (edges < k) is the maximum edge weight among edges < k that can be part of a spanning tree of the subgraph. If the subgraph is connected, then for any edge e in it, there exists a spanning tree containing e (as argued). Therefore, if the subgraph (edges with s < k) is connected, we can achieve max_s = the maximum s among edges with s < k. So the minimal cost in scenario B would be k - (max s among edges with s < k). However, if the subgraph of edges with s < k is not connected, then we cannot have a spanning tree using only edges < k; we would need at least one edge with s >= k to connect components. But that would then fall into scenario A, because the tree would contain a >=k edge. So scenario B is only feasible if the subgraph of edges with s < k is connected (i.e., the graph can be spanned using only edges with s < k). Wait, scenario B requires that the tree contains no edge with s >= k. That means the tree must be a spanning tree of the graph using only edges with s < k. So the subgraph induced by edges with s < k must be connected. If it is not connected, then any spanning tree must include at least one edge with s >= k to connect the components. So scenario B is only possible if the subgraph of edges with s < k is connected. However, is it possible that the subgraph of edges with s < k is disconnected but we can still have a tree with all edges < k? No, because if the subgraph is disconnected, you cannot connect all vertices using only those edges. So scenario B is infeasible in that case. So if the <k subgraph is disconnected, we must have at least one edge with s >= k, so only scenario A is possible.
Thus, overall:
Let E_lo = edges with s < k.
Let E_eq = edges with s = k (c=0, and also in >=k set).
Let E_hi = edges with s > k (c = s - k > 0).
We have all edges.
Define subgraph G_lo = (V, E_lo). This is the graph of edges strictly less than k.
Define subgraph G_leq = (V, E_lo ∪ E_eq) = edges with s ≤ k.
Now:
If G_leq is connected and E_eq non-empty -> answer 0.
Else, we need to compute:
Option_A = minimum over spanning trees T that contain at least one edge from E_eq ∪ E_hi (i.e., s ≥ k) of sum_{e in T} c_e.
Option_B = if G_lo is connected, then cost_B = k - max_{e in E_lo} s_e (since we can pick a tree that includes the edge with maximum s among E_lo). However, is it always exactly that? We need to maximize the maximum edge weight in the tree. Since we can include the largest edge if it is in some spanning tree. But careful: The tree must be spanning and use only edges from E_lo. If G_lo is connected, then there exists a spanning tree that contains any particular edge (as argued). So we can choose the edge with the largest s in E_lo and include it in a spanning tree. Then the max_s of that tree is that largest s. But could we achieve a higher max_s by having a tree where the maximum edge is not necessarily the global maximum, but maybe we could have a tree that uses some edges with s = something but the maximum edge weight is the same as the global max? Actually the maximum edge weight among all edges in E_lo is the highest possible, because any tree's max cannot exceed the global max. So yes, we can achieve max_s = M = max_{e in E_lo} s_e. So cost_B = k - M, provided G_lo is connected.
But is there any nuance? Suppose the edge with max s is a bridge in G_lo, then it's automatically included in any spanning tree of G_lo? Actually if it's a bridge, then any spanning tree of G_lo must include it, so certainly we can get max_s = M. If it's not a bridge, we can still include it by constructing a tree that includes it. So yes, as long as G_lo is connected, we can have a spanning tree whose max edge is M. However, we also need that the tree doesn't contain any edge with s ≥ k, but M < k by definition of E_lo, so fine.
Thus cost_B = k - M if G_lo is connected; otherwise, option_B is not available (infinite).
Now Option_A: we need to compute min sum c_e over spanning trees that contain at least one edge from E_geq = E_eq ∪ E_hi (s ≥ k). Since c_e = 0 for E_eq and positive for E_hi, the sum c_e is just the sum over edges with s > k in the tree.
Observation: c_e is zero for edges with s ≤ k. So essentially, we want to find a spanning tree that includes at least one edge with s ≥ k, and among such trees, minimize the total sum of (s_e - k) over edges with s > k (since edges with s = k contribute 0). This is like: we need to ensure the tree contains at least one "special" edge (with s ≥ k), while the cost is sum of positive weights over edges with s > k.
We can think of this as: find MST with respect to c_e, but if that MST uses no special edges (i.e., all edges have s < k), then we need to consider the next best tree that includes at least one special edge.
Given that c_e = 0 for s ≤ k, the MST with respect to c_e will try to use as many zero-cost edges as possible. If the zero-cost edges (s ≤ k) can connect the graph, then the MST cost is 0, and that tree might or might not contain a special edge. It will contain a special edge if there is any s = k edge used, or if zero-cost edges include s = k? But all s ≤ k edges have c=0, including s = k. So if the subgraph G_leq is connected, the MST can be built solely from edges with s ≤ k, giving cost 0. In that case, Option_A could be 0 if that tree contains at least one edge with s ≥ k (i.e., s = k). That's exactly the condition we already checked for answer 0: G_leq connected and there exists an edge with s = k. So if G_leq is connected but no s = k edges (i.e., all edges in G_leq are s < k), then the MST cost is 0, but the tree does not contain any s ≥ k edge. However, we could still possibly get a tree with cost 0 that contains a special edge? There is no special edge with s = k (since none exist), so to include a special edge we would need to include an s > k edge, which has positive c. So no zero-cost tree with a special edge. So Option_A would be >0.
If G_leq is not connected, then any spanning tree must use at least one edge with s > k to connect the components (since only edges with s > k are the remaining edges; note that edges with s = k are also in E_leq? Actually E_leq includes s ≤ k, so if G_leq is disconnected, that means even with all s ≤ k edges, the graph is disconnected. So we need some edges with s > k to connect the components. So any spanning tree will have at least one edge from E_hi (since s = k edges don't exist or are also in G_leq, but they are part of G_leq; if G_leq is disconnected, then there is no spanning tree using only edges with s ≤ k, so any spanning tree must include at least one edge with s > k. Thus, the condition "contains at least one edge with s ≥ k" is automatically satisfied. Therefore, Option_A is simply the MST with respect to c_e, because any spanning tree must include some hi edges, and the MST minimizes sum c_e. However, is it true that the MST with respect to c_e will be the minimum sum c_e? Yes, because we are minimizing sum of c_e over all spanning trees, and all spanning trees are allowed (they all satisfy the condition automatically because they must contain some hi edge). So Option_A = MST cost with weights c_e.
But careful: In the case where G_leq is disconnected, we are forced to use at least one hi edge, but MST with c_e will also use some hi edges to connect components. Since c_e for hi edges > 0, the MST will choose the hi edges with smallest c_e (i.e., smallest s_e - k) to connect components, which makes sense. So Option_A = total sum of c_e of those selected hi edges.
Now, if G_leq is connected but there is at least one s = k edge, we already have answer 0, so done. But if G_leq is connected and there is no s = k edge, then G_leq = G_lo (since all s ≤ k edges are s < k). Actually if there is no s = k edge, then G_leq = G_lo. Then the MST with respect to c_e has cost 0 and uses only lo edges. But that tree doesn't contain any special edge. So we cannot use that tree for scenario A. However, we could consider including an s > k edge to satisfy the condition, but that would incur additional cost. So we need to compute the minimum cost tree that includes at least one edge from E_hi (since no s = k edges exist). Because G_leq = G_lo is connected, we can build a tree using only lo edges (cost 0). But to have a tree with a hi edge, we must replace at least one lo edge with a hi edge, but also possibly remove another edge to keep it a tree. We need to find the minimum increase in sum c_e relative to the MST (which is 0) to incorporate at least one hi edge.
So for Option_A in the case G_leq is connected but without any s = k edges, we need to compute: min over spanning trees that contain at least one hi edge of sum c_e. Since c_e = 0 for lo edges, sum c_e = sum over hi edges in the tree of (s_e - k). So we want a tree that possibly uses some lo edges and at least one hi edge, minimizing total extra from hi edges.
This is reminiscent of: start with MST on zero-cost edges (which forms a forest? Actually G_lo is connected, so the subgraph on lo edges is connected; the MST with respect to c_e would pick any spanning tree of G_lo, cost 0). To include a hi edge, we must add it and remove some other edge to break the cycle. Since the current tree is already spanning, adding a hi edge creates a cycle. We can then remove any edge on that cycle to get another spanning tree. The total cost change will be c(hi) - c(removed). Since c(removed) is 0 (if it's a lo edge), we get cost = c(hi). However, we might also consider using multiple hi edges if it reduces cost? But since hi edges have positive cost, using more would increase cost. So the best is to include exactly one hi edge, and keep as many lo edges as possible. But we must ensure that after adding the hi edge and removing one lo edge, the tree remains spanning and still uses only lo edges apart from that hi edge. That's possible if there is a cycle containing the hi edge and some lo edges. Since the lo edges form a connected spanning subgraph (a tree? Actually we have many lo edges, we can pick a particular spanning tree T_lo). Adding a hi edge (u, v) creates a unique cycle in T_lo ∪ {hi}. The cycle consists of the hi edge plus the unique path in T_lo between u and v. All edges on that path are lo edges (cost 0). To get a spanning tree containing the hi edge, we can remove any edge on that path. So the new tree's cost = c(hi) (since all other edges cost 0). So the cost of such a tree is exactly c(hi). But could we get a tree with cost less than the smallest c(hi)? Possibly if we use multiple hi edges but remove some hi edges? That doesn't make sense. The minimum possible cost for a tree containing at least one hi edge is thus the minimum c(hi) over all hi edges, provided that by adding that hi edge to some spanning tree of lo edges we can form a valid spanning tree. But is it always possible to include a specific hi edge and keep the rest lo? The resulting tree must be spanning and connected. Since G_lo is connected, we can pick any spanning tree T_lo. Adding hi edge creates a cycle; we remove any lo edge on that cycle. The resulting graph is a tree (connected, n-1 edges) that includes hi edge. That's valid as long as the hi edge is not already in T_lo (obviously it's not, since hi edge is not lo). So yes, for any hi edge, we can construct a spanning tree that includes it and has cost c(hi). So Option_A in this case would be min_{e in E_hi} c_e.
But wait: is it always possible that the resulting tree uses only that hi edge and lo edges? Yes, because we remove a lo edge. So cost = c(hi). However, what if there is a hi edge that, when added to T_lo, creates a cycle that includes another hi edge? Not possible because T_lo only contains lo edges. So the cycle consists of the hi edge plus a path of lo edges. So we can always remove a lo edge to keep tree. So cost = c(hi). But is there any scenario where using a hi edge forces us to also include another hi edge because the lo edges alone cannot connect the graph after removal? Actually, we start from a spanning tree T_lo that uses only lo edges. We add hi edge, creating exactly one cycle. Remove one edge on that cycle (any) to break the cycle. The resulting graph is a spanning tree that includes hi edge and n-2 other lo edges. It's connected because we removed an edge from a cycle, so the graph remains connected. So it's valid. So indeed, we can achieve cost = c(hi) for any hi edge. However, we must also consider if there are any hi edges with c_e maybe 0? That would be s = k, but we are in case where there is no s=k edge, so all hi edges have s>k => c>0.
Thus Option_A = min_{e in E_hi} (s_e - k) in this case.
But is that always the minimal? Could we get a tree with cost less than min c(hi) by using a combination of multiple hi edges such that the total sum is less? That seems impossible because any tree with at least one hi edge has at least the smallest positive c. However, if there exists a hi edge with negative c? No. So min c(hi) is lower bound. So Option_A = min c(hi).
But we also need to consider the possibility that there might be no hi edges at all. But if G_leq is connected and there are no s = k edges and no hi edges, then all edges are lo (s < k). That would mean all edges have s < k. Then scenario A is impossible because we cannot have a tree with any edge >=k. So only scenario B is possible. That matches our earlier analysis.
Thus summarizing:
Case 1: There exists a spanning tree using only edges with s ≤ k (i.e., G_leq is connected) and there exists at least one edge with s = k. Then answer 0.
Otherwise:
-
Compute Option_B (if G_lo is connected): cost_B = k - M, where M = maximum s among edges with s < k. (Note: if there are no edges with s < k? Then G_lo is empty, not connected unless n=1 but n>=2. So if no lo edges, scenario B infeasible.)
-
Compute Option_A:
-
If G_leq is not connected: Then any spanning tree must contain at least one edge from E_hi (since no s=k edges? Actually maybe there are s=k edges, but G_leq includes s=k edges. Wait: G_leq = edges with s ≤ k. If G_leq is not connected, then there is no spanning tree using only ≤k edges. So any spanning tree must contain at least one edge with s > k (since edges with s = k are already part of G_leq and cannot connect the components). Therefore, all spanning trees have at least one hi edge. So we can take MST with respect to c_e. Compute the minimum sum of c_e over all spanning trees. That MST will use some lo edges (cost 0) and some hi edges to connect components, picking the cheapest hi edges (smallest c). So Option_A = MST cost (with weights c_e). This MST can be computed via Kruskal, but we need to handle up to 2e5 edges per test case, sum over test cases 2e5. So we can sort edges by c_e, and run DSU. However, note that c_e = 0 for many edges; we can handle efficiently.
-
If G_leq is connected (but we are not in case answer 0, so that means either there is no s=k edge; because if there were s=k edge, answer would be 0). So G_leq = G_lo (all edges ≤ k are actually < k). Then MST with c_e has cost 0, but it's not allowed for scenario A because it has no s≥k edge. So we need to find the minimum cost tree that includes at least one hi edge. That cost is min_{e in E_hi} c_e. However, is there any nuance that perhaps the MST with c_e might include a hi edge even though G_leq is connected? Since G_leq is connected, it is possible to have a spanning tree with only lo edges (cost 0). MST algorithm would pick lo edges first (since c=0) and never pick hi edges because they have positive cost. So the MST cost is 0 and uses no hi edges. So Option_A = min c(hi). But we also need to consider the possibility that there are no hi edges at all: then Option_A is impossible (infinite). That would mean all edges are s < k, which implies G_leq is connected (since the whole graph is connected, but G_leq = all edges? Wait, if all edges are s < k, then G_leq = whole graph, and it's connected (given graph is connected). Then we are in scenario where there is no s=k edge, and no hi edges. Then answer is just Option_B (since scenario A impossible). And Option_B is k - M (since all edges are < k). So that fits.
Now, also consider the case where G_leq is connected and there are s=k edges but we earlier gave answer 0. So that case is covered.
Thus algorithm:
For each test case:
- Input n, m, k.
- Store edges with (u, v, s).
- Compute max_s_lo = maximum s among edges with s < k (if any).
- Determine connectivity of subgraphs:
- We need to know if G_lo (edges with s < k) is connected? But we might only need G_leq connectivity? Let's derive needed checks.
We need:
-
Check if there exists a spanning tree using only edges with s ≤ k (i.e., G_leq connected). This is needed for answer 0 (if also there is an edge with s = k) and also for the case where Option_A is min c(hi) (when G_leq connected but no s=k edges). Also, for Option_B we need G_lo connected.
-
Check if there exists at least one edge with s = k (maybe needed).
-
For Option_A when G_leq not connected: compute MST cost with c_e.
-
For Option_A when G_leq connected but no s=k edges: compute min c(hi).
But careful: In scenario A, we require at least one edge with s ≥ k. If G_leq is connected, it's possible that there are s = k edges but we already handled answer 0. But if there are s = k edges but G_leq is not connected? That can't happen because G_leq includes s=k edges; if there is at least one s=k edge, G_leq could still be disconnected if the graph's connectivity requires edges > k. So we need to treat that scenario separately: G_leq is not connected, but there exist s=k edges. In that case, answer 0 is not possible because even with s=k edges, we cannot span the graph with only ≤k edges. However, scenario A: any spanning tree must contain at least one hi edge (since G_leq is disconnected). So Option_A = MST cost with c_e. But also note that trees could include s=k edges (cost 0) and hi edges. The MST with c_e will automatically include s=k edges if they help, because c=0. But does the MST with c_e necessarily produce a tree with at least one s≥k edge? Since there are hi edges needed, yes. So Option_A is MST cost. However, could we have a tree that includes a s=k edge but no hi edge? That would require G_leq to be connected, which it's not. So any tree must include at least one hi edge. So Option_A = MST cost.
Now, what about Option_B in the case G_leq not connected? Option_B is not feasible because G_lo might still be connected? Wait, Option_B requires a spanning tree with all edges s < k. That's more restrictive than G_leq; if G_leq is not connected, could G_lo be connected? Possibly if there are s=k edges but no s>k edges? But if G_leq is not connected, that means even with all edges s ≤ k, the graph is disconnected. That could be because the only edges connecting some components are s>k, or maybe because there are s=k edges but still insufficient? For G_leq to be disconnected, there exist at least two vertices that are only connected via paths that include edges with s>k. However, it's possible that G_lo (edges s<k) is connected, but G_leq (edges s≤k) is the same as G_lo if there are no s=k edges? Actually if there are s=k edges, then G_leq includes those, so G_lo is a subset. So if G_leq is disconnected, it's possible that G_lo is still connected? Let's consider: Suppose we have two components that are connected by a single edge with s=k. Then G_leq would be connected because that edge is included. So G_leq disconnected implies that even including s=k edges, the graph is disconnected. That means the only connections between some parts are through edges with s>k. Therefore, G_lo (which excludes both s=k and s>k) would be even less connected, so definitely disconnected. So if G_leq is disconnected, then G_lo is also disconnected. Because adding more edges (s=k) cannot disconnect the graph. So G_lo is a subgraph of G_leq; if G_leq is disconnected, G_lo is also disconnected. So Option_B is infeasible whenever G_leq is disconnected.
Thus, Option_B is only feasible when G_lo is connected. That also implies G_leq is connected? Not necessarily: G_lo connected means all vertices can be connected using only edges with s<k. That automatically means G_leq is connected because adding s=k edges doesn't hurt. So if G_lo is connected, then G_leq is connected. So Option_B feasible implies G_leq connected. So we have two main connectivity states:
- G_lo connected: then G_leq connected.
- G_lo not connected but G_leq connected: This can happen if there are s=k edges that bridge the components that are otherwise only connected by edges with s>k? Let's examine: Suppose we have two components that are not connected by any s<k edges, but there is an edge with s=k connecting them. Then G_lo is disconnected, but G_leq is connected because the s=k edge bridges them. So G_lo disconnected but G_leq connected is possible. That is the scenario where there is at least one s=k edge and maybe some s>k edges as well, but the s=k edges provide connectivity. In that case, G_leq is connected, and there is at least one s=k edge. This is exactly the case for answer 0? Not necessarily: If G_leq is connected and there is at least one s=k edge, then answer 0 is achievable (as argued). Because we can construct a spanning tree using only edges with s ≤ k (since G_leq connected) that includes a s=k edge (possible). And that tree would have cost 0. So in such a scenario, answer = 0. So we don't need to consider Option_B or Option_A further because answer is 0.
Thus, Option_B is only relevant when G_lo is connected and there is no s=k edge? Actually if G_lo is connected, then G_leq is also connected. If also there is an s=k edge, then answer 0 is reachable because we have connectivity with ≤k edges and a k edge exists. So answer 0. So Option_B is only considered when G_lo is connected but there is no s=k edge (so all edges ≤ k are actually < k). Then we have two options: scenario A (include a hi edge) or scenario B (all lo edges). But scenario B requires using only lo edges (which we can, since G_lo connected). Then cost_B = k - M (max s among lo edges). Scenario A (include at least one hi edge) gives cost_A = min_{e in E_hi} (s_e - k) if there exists any hi edge. But wait: if there are hi edges, then min c(hi) may be less than k - M? We need to compare.
If there are no hi edges, then only Option_B is valid, answer = k - M.
Now, what about the case where G_lo is not connected but G_leq is connected? As argued, that implies existence of s=k edges (since otherwise G_leq = G_lo). And if G_leq is connected and there is at least one s=k edge, answer = 0. So that case is covered.
Now, the remaining case is when G_leq is not connected. Then answer is Option_A = MST cost with c_e. Since Option_B is impossible. Also note that there might be s=k edges but they are not enough to connect the graph; still MST cost.
Now, we also need to consider the scenario where G_leq is connected but there are s=k edges and also hi edges. Answer 0, so done.
Now, we also need to consider the scenario where G_leq is connected, no s=k edge, and there are hi edges. Then answer = min(Option_B, Option_A) = min(k - M, min_c_hi). But careful: Option_A = min_c_hi? Is that always achievable? As argued, we can take a spanning tree consisting of a spanning tree of G_lo (which exists because G_lo is connected) and replace one edge with a hi edge. That gives a tree with exactly one hi edge, cost = c(hi). That tree includes a hi edge (s > k) and thus is valid for scenario A. So Option_A is at most min_c_hi. Could there be a tree with even lower cost? Possibly if we use multiple hi edges but the sum is less than the smallest c? No, because any tree with at least one hi edge has sum c >= min_c_hi (since all hi edges have positive c, and we need at least one). So min_c_hi is the lower bound. So Option_A = min_c_hi.
But there is a nuance: What if the hi edge, when added to a spanning tree of G_lo, creates a cycle, and we remove a lo edge. That's fine. However, we must ensure that after removal, the tree still satisfies that max exactly k? Wait, for scenario A, cost formula is sum c_e. Since we only have one hi edge with c = s - k > 0, and all other edges are lo (c=0). So cost = s - k. The resulting tree has max s = max( max_s_lo, s_hi ). Since s_hi > k, the max is s_hi. After we modify the edges, we will lower the hi edge to k? Wait, the cost formula sum c_e is the number of changes needed: we lower the hi edge from s_hi to k (cost s_hi - k), and we don't change lo edges. The max becomes k (since we lowered hi edge to k). But is that correct? In scenario A, we have at least one edge with s >= k, and the cost is sum_{e} max(0, s_e - k). So if we have a hi edge with s_hi > k, we lower it to k, cost s_hi - k. The lo edges are unchanged (s < k). So the max after changes is k (due to the lowered hi edge). So that works. Also, if we had multiple hi edges, we would lower each to k, cost