Poisonous Labyrinth

시간 제한3초메모리 제한2048 MB

요약
가중치 트리에서 각 독 종류마다 두 병이 놓여 있을 때, 모든 쌍을 마시고 돌아오는 최소 왕복 거리를 주는 시작 정점을 찾는다.
난이도

어려움10점 중 8점

유형
트리, DFS, 동적 계획법, 그리디
정답자
아직 제출이 없습니다

문제

BThero needs to escape from a labyrinth which is represented by a tree with nn vertices and n−1n-1 edges, where each edge has its own length. Additionally, the labyrinth vertices contain 2m2 m poison vials: two vials of each of the mm different types of poison.

When BThero enters a vertex for the first time, he immediately drinks all the vials in that vertex. When he ends up in a vertex where he has been before, there are no more vials to drink there.

When BTHero drinks a vial of some type of poison that he did not yet drink, he is poisoned by that type of poison. To cure it, BThero must find and drink the other vial of the same type of poison.

BThero starts his path in vertex ss, where he immediately drinks all the vials in that vertex. Then he passes through some vertices until he is no longer poisoned, after which he returns to vertex ss and leaves the labyrinth.

It is necessary to find the starting vertex ss such that, if BThero starts his path in this vertex, he will have to travel the minimum total distance, provided that he chooses the optimal route.

입력

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

Each of the next n−1n - 1 lines contains three integers, u_iu\_i, v_iv\_i, and w_iw\_i (1≤u_i,v_i≤n1 \leq u\_i, v\_i \leq n, 1≤w_i≤1091 \leq w\_i \leq 10^9) which describe a bidirectional edge between vertices u_iu\_i and v_iv\_i with length w_iw\_i.

Each of the next mm lines contains two integers a_ja\_j and b_jb\_j (1≤a_j,b_j≤n1 \leq a\_j, b\_j \leq n): the two vertices where the vials with poison of type jj are located. Note that it is possible that a_j=b_ja\_j = b\_j, in which case, when entering the vertex, BThero is poisoned and then cured immediately.

출력

Output a line with a single integer: the minimum distance that BThero will have to travel to cure himself from all poisons if he starts from the optimal vertex.

예제3

  1. 예제 1

    입력
    4 2
    1 2 1
    1 3 10
    1 4 100
    1 3
    2 4
    
    예상 출력
    20
    
  2. 예제 2

    입력
    5 2
    1 2 1
    1 3 10
    1 4 100
    1 5 1000
    1 3
    2 4
    
    예상 출력
    0
    
  3. 예제 3

    입력
    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
    
    예상 출력
    34