luogu#P4886. 快递员

    ID: 3874 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>点分治递归O2优化树链剖分洛谷月赛

快递员

Problem Description

In Bob’s city, there are nn post stations. For economic reasons, these stations are connected by n1n - 1 weighted undirected edges. That is, the nn post stations form a tree.

Bob is applying for a courier job. He needs to deliver mm packages. For the ii-th package, it needs to be delivered from uu to vv. Since Bob cannot carry a package for too long, for one delivery, he must first go from the courier center to uu, then return from uu to the courier center, then go from the courier center to vv, and finally return from vv to the courier center. In other words, if the courier center is node cc, then his route is $c \rightarrow u \rightarrow c \rightarrow v \rightarrow c$.

Now Bob wants to choose a node as the courier center so that the maximum distance he needs for any delivery is minimized. Clearly, this maximum distance is an even number. You only need to output the result of dividing the maximum distance by 22.

Input Format

The first line contains two integers n,mn, m, with the meanings as described above.

The next n1n - 1 lines each contain three integers ui,vi,wiu_i, v_i, w_i, representing an edge connecting uiu_i and viv_i with length wiw_i.

The next mm lines each contain two integers ui,viu_i, v_i, representing the start and end locations of a package.

Output Format

Output one integer on a single line, representing the answer.

3 1
1 2 1
2 3 1
1 3

2

Hint

For 25%25\% of the testdata, 1n,m10001 \leq n, m \leq 1000.

For 100%100\% of the testdata, 1n,m1051 \leq n, m \leq 10^5, 1wi10001 \leq w_i \leq 1000.

Translated by ChatGPT 5