#C10487. Remove a Longest Road
Remove a Longest Road
Remove a Longest Road
You are given a connected road network of n cities and m bidirectional roads. Each road is described by three integers u, v and w, where w represents the length (or weight) of the road connecting cities u and v.
Your task is to remove exactly one road – specifically one of the roads with the maximum length – such that the remaining network still connects all the cities. In other words, after removal, there should be a path between any two cities.
Note: If there are multiple roads of the same maximum length, remove the first one encountered in the input order.
The problem can be formally described by the formula:
$$\text{Find } r_{max} = \max_{1 \leq i \leq m} \{ w_i \}$$and remove exactly one road with weight \(r_{max}\), then print the remaining roads.
inputFormat
The first line contains two integers n and m (1 \leq n \leq 10^5
, 0 \leq m \leq 10^5
), where n is the number of cities and m is the number of roads.
Each of the following m lines contains three integers u, v, and w (1 \leq u,v \leq n
, 1 \leq w \leq 10^9
), representing a road between cities u and v with weight w.
You should read the input from standard input (stdin
).
outputFormat
Output the remaining roads after removing the one road with the maximum weight. For each road, output a line containing three integers u, v, and w separated by spaces, following the same order as the input except for the removed road.
If no roads remain, output nothing. The output must be written to standard output (stdout
).
5 7
1 2 5
1 3 10
2 3 12
2 4 15
3 4 7
4 5 6
5 1 14
1 2 5
1 3 10
2 3 12
3 4 7
4 5 6
5 1 14
</p>