Many Pairs

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

요약
각 도시를 루트로 삼아 이웃한 부분트리 두 개 이하를 골랐을 때, 양 끝이 모두 선택 영역에 속하는 조약 비용 합의 최댓값을 모든 도시에 대해 구한다.
난이도

어려움10점 중 8점

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

문제

EJOI-land is a kingdom consisting of NN cities. Each city has a unique index between 11 and NN associated with it. The cities are connected by N−1N - 1 bidirectional roads. It is also guaranteed that you can reach any city from any other city. In other words, EJOI-land has a tree-like structure. There are also KK trading treaties in EJOI-land. Each treaty is defined by a pair of cities (A,B)(A,B) and has cost CC associated with it.

The king decided to test his son's governing abilities as follows:

  • He will choose a city HH and designate it as the prince's headquarters. Suppose that the tree will now be rooted in HH.
  • The prince will choose at most two cities that are neighbours of HH. Now HH and the subtrees of the chosen cities are under his governance.:

The profit he gets is equal to the sum of the costs CC of the treaties under his jurisdiction, for a treaty to be under his jurisdiction, both cities associated with it must be under his governance.

The king still hasn't announced which city will be the prince's headquarters, but the prince still likes to wonder. Thus, for each city, he wonders what is the maximal profit he can get if it were to be chosen as the new headquarters.

Your task is to find the maximal profit for each city.

입력

The first line of input contains two space-separated integers, NN and KK, the number of cities in EJOI-land, and the number of trading treaties, respectively.

The following N−1N - 1 lines, each, contain two space-separated integers UU and VV, meaning that there is road between cities UU and VV.

The following KK lines, each, contain three space-separated integers AA, BB, and CC - being the two cities involved in the treaty, and its cost, respectively.

출력

Output NN space-separated integers, the ii-th integer representing the maximal profit obtainable if city ii were to be chosen as the prince's headquarters.

제한

  • 2≤N,K≤2⋅1052 ≤ N,K ≤ 2 \cdot 10^5.
  • 1≤U,V,A,B≤N1 ≤ U,V ,A,B ≤ N
  • 1≤C≤1061 ≤ C ≤ 10^6

예제1

  1. 예제 1

    입력
    6 4
    6 2
    2 5
    3 6
    1 2
    4 6
    2 5 11
    5 6 16
    4 3 18
    2 3 6
    
    예상 출력
    51 51 51 51 51 33