This page is still under construction.

Parts of this page are still being built. What you see may change.

Disruption

Time limit2sMemory limit512 MB

Summary
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 NN pastures (2≤N≤50,0002 \leq N \leq 50{,}000), joined by N−1N-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 MM extra two-way paths (1≤M≤50,0001 \leq M \leq 50{,}000), each with a positive integer length of at most 10910^9. 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 NN and MM. Each of the next N−1N-1 lines describes an original path with two integers pp and qq, the pastures it connects, where p≠qp \neq q and both lie in the range 1…N1 \ldots N. Each of the remaining MM lines describes an extra path with three integers pp, qq, and rr, where rr is the length of the path between pastures pp and qq. At most one path runs between any pair of pastures.

Output

Print N−1N-1 lines. On the ii-th line, print the length of the shortest extra path that reconnects the farm when the ii-th original path of the input becomes blocked. If no extra path can replace it, print -1.

Examples3

  1. Example 1

    Input
    6 3
    1 2
    1 3
    4 1
    4 5
    6 5
    2 3 7
    3 6 8
    6 4 5
    
    Expected output
    7
    7
    8
    5
    5
    
  2. Example 2

    Input
    4 1
    1 2
    1 3
    1 4
    2 3 5
    
    Expected output
    5
    5
    -1
    
  3. Example 3

    Input
    5 3
    1 2
    2 3
    3 4
    4 5
    1 3 4
    2 5 9
    1 5 6
    
    Expected output
    4
    4
    6
    6