Disruption
Time limit2sMemory limit512 MB
Given a tree and extra weighted edges, for each tree edge report the minimum weight of a non-tree edge whose endpoints lie in different components after removing it.
- Level
Hard8 of 10
- Topics
- Tree, DFS, Union-find, Sorting
- Solved
- No attempts yet
Problem
Farmer John's farm has pastures (), joined by 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 extra two-way paths (), each with a positive integer length of at most . 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.
Input
The first line contains and . Each of the next lines describes an original path with two integers and , the pastures it connects, where and both lie in the range . Each of the remaining lines describes an extra path with three integers , , and , where is the length of the path between pastures and . At most one path runs between any pair of pastures.
Output
Print lines. On the -th line, print the length of the shortest extra path that reconnects the farm when the -th original path of the input becomes blocked. If no extra path can replace it, print -1.