#P4374. [USACO18OPEN] Disruption P

    ID: 3321 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>2018线段树USACO深度优先搜索,DFS树链剖分,树剖

[USACO18OPEN] Disruption P

Description

Farmer John is proud of his well-connected farm. The farm consists of NN pastures (2N50,0002 \leq N \leq 50,000) with N1N-1 bidirectional roads connecting them, each of length 11 unit. Farmer John notices that from any pasture to any other, one can travel via some sequence of roads.

Although FJ's farm is currently connected, he worries about what would happen if a road were blocked, since this would split the farm into two disjoint sets of pastures, and the cows could move only within each set but not between sets. So FJ builds MM additional bidirectional roads (1M50,0001 \leq M \leq 50,000), each with a positive integer length of at most 10910^9. The cows can still use the original roads to travel unless some of them are blocked.

If one of the original roads is blocked, the farm is divided into two disjoint regions. FJ will then choose one of his additional roads that can restore connectivity between these two regions to replace the original one, so that the cows can again travel from any pasture to any other pasture.

For each original road on the farm, help FJ pick the shortest replacement road.

Input Format

The first line contains NN and MM. The next N1N-1 lines each describe an original road with integers pp and qq, where pqp \neq q are the two pastures it connects (within 1N1 \ldots N). The remaining MM lines each describe an additional road with three integers pp, qq, and rr, where rr is the road's length. There is at most one road between any two pastures.

Output Format

For each of the original N1N-1 roads, in the order they appear in the input, output the length of the shortest replacement road that can reconnect the farm if that road is blocked. If no suitable replacement road exists, output 1-1.

6 3
1 2
1 3
4 1
4 5
6 5
2 3 7
3 6 8
6 4 5
7
7
8
5
5

Hint

Author: Brian Dean.

Translated by ChatGPT 5