Farmer John's farm has N pastures (2≤N≤50,000), joined by N−1 two-way paths of unit length. Using these paths, the cows can travel from any pasture to any other pasture.
The farm is connected, but Farmer John worries about a blocked path. Blocking one path splits the farm into two groups of pastures, and the cows can then travel inside a group but not between the two groups. So Farmer John builds M extra two-way paths (1≤M≤50,000), each with a positive integer length of at most 109. The cows still use only the original paths, unless one of the original paths becomes blocked.
When an original path becomes blocked, the farm splits into two pieces, and Farmer John picks a single extra path that reconnects the two pieces, so the cows can travel from any pasture to any other pasture again.
For each original path, find the shortest extra path that can replace it.