Recount Cable Commitments
Classify edges across all minimum-cost spanning trees of a weighted graph
An election office must connect its recount stations using a set of secure cables. Stations are numbered from 0 to n - 1.
Each record in cables is [u, v, cost], describing an undirected cable between two different stations. Different records may connect the same pair of stations; they are distinct cable choices. Costs may be negative because some installations receive subsidies.
A valid installation selects exactly n - 1 cable records and connects every station. Among valid installations, the office will choose one with the minimum possible total cost. The supplied network is guaranteed to allow a valid installation.
For every cable record, determine its status across all minimum-cost valid installations:
0: it appears in none of them;1: it appears in some, but not all, of them;2: it appears in every one of them.
Return an integer list in the original order of cables. For a single station with no cables, return an empty list.
Examples
Example 1
Input: n = 3, cables = [[0,1,1],[1,2,1],[0,2,2]] Output: [2,2,0]
The two cost-1 cables must both be selected to connect the three stations at minimum cost. Selecting the cost-2 cable would increase the total.
Example 2
Input: n = 4, cables = [[0,1,1],[1,2,1],[2,0,1],[2,3,5]] Output: [1,1,1,2]
Any two cables from the cost-1 triangle can be selected. The cable leading to station 3 is required in every valid installation.
Example 3
Input: n = 2, cables = [[0,1,-2],[1,0,-2],[0,1,0]] Output: [1,1,0]
Either of the two subsidized cables can connect the stations at minimum cost. The unsubsidized cable is never selected.
Constraints
- 1 <= n <= 1500
- 0 <= cables.length <= 2500
- Each cable is [u, v, cost], with 0 <= u, v < n and u != v.
- -1000000 <= cost <= 1000000
- Parallel cables are allowed and are treated as distinct records.
- The undirected network is connected. When n = 1, cables is empty.
An iterative bridge traversal avoids recursion-depth limits. The intended complexity is O(n + m log m) time and O(n + m) auxiliary space, where m is the number of cables. Bridge detection must skip the incoming edge record, not every edge to the parent vertex.
Hints
Show hint 1Hint 1
Process costs in increasing order. Before examining one cost, contract everything connected using strictly cheaper cables.
Show hint 2Hint 2
Within one cost group, a cable whose endpoints are already contracted together is unusable. For the remaining cables, determine which edges are bridges, taking care to distinguish parallel edge records.
Follow-up questions
What an interviewer might ask once you have a working solution.
- How would you also construct one minimum-cost installation, choosing a deterministic result when several exist?
- If the network were not guaranteed to be connected, how would you classify cables across all minimum-cost spanning forests?
Practice this with an AI interviewer
Explain your approach out loud, write Python or JavaScript, run it against hidden tests (including large inputs), and get a scored debrief.
Start this problem