Under construction

Still being built. Things may change

Poisonous Labyrinth

Time limit3sMemory limit2048 MB

Summary
Given a weighted tree with m pairs of poison vials, find the start vertex that minimizes the distance needed to cure all poisons and return to it.
Level

Hard8 of 10

Topics
Tree, DFS, Greedy
Solved
No attempts yet

Problem

BThero needs to escape from a labyrinth shaped like a tree with nn vertices and n−1n-1 edges. Each edge has a length. The vertices hold 2m2m poison vials in total: two vials of each of the mm poison types.

When BThero enters a vertex for the first time, he drinks all the vials in that vertex immediately. If he enters a vertex he has already visited, there are no vials left to drink there.

Drinking a vial of a poison type he has not drunk before poisons him. To cure it, he must find and drink the other vial of the same type.

BThero starts at vertex ss and drinks the vials there at once. He then keeps moving through vertices until he is no longer poisoned. After that, he returns to vertex ss and leaves the labyrinth.

Find the starting vertex ss that minimizes the total distance BThero travels along an optimal route.

Input

The first line contains two integers nn and mm (2≤n≤2⋅1052 \le n \le 2 \cdot 10^5, 1≤m≤2⋅1051 \le m \le 2 \cdot 10^5), the number of vertices and the number of poison types.

Each of the next n−1n-1 lines contains three integers uiu_i, viv_i, and wiw_i (1≤ui,vi≤n1 \le u_i, v_i \le n, 1≤wi≤1091 \le w_i \le 10^9). This describes a bidirectional edge between vertices uiu_i and viv_i with length wiw_i.

Each of the next mm lines contains two integers aja_j and bjb_j (1≤aj,bj≤n1 \le a_j, b_j \le n), the two vertices where the vials of poison type jj are located. It is possible that aj=bja_j = b_j. In that case, BThero is poisoned as soon as he enters the vertex and is cured immediately.

Output

Print one integer on a line: the minimum distance BThero must travel to cure all poisons when he starts from the optimal vertex.

Examples3

  1. Example 1

    Input
    4 2
    1 2 1
    1 3 10
    1 4 100
    1 3
    2 4
    
    Expected output
    20
    
  2. Example 2

    Input
    5 2
    1 2 1
    1 3 10
    1 4 100
    1 5 1000
    1 3
    2 4
    
    Expected output
    0
    
  3. Example 3

    Input
    7 4
    1 2 8
    1 3 9
    2 4 10
    2 5 11
    3 6 12
    3 7 13
    2 3
    7 6
    2 1
    4 5
    
    Expected output
    34