PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#LCA0000047. Trò chơi nóng và lạnh (Hot and Cold)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Trò chơi nóng và lạnh (Hot and Cold)

Hot and Cold

Source: DMOJ

Version: Phuoc Hung OJ Extended

Problem Statement

A game takes place on an unweighted undirected tree with nn vertices. There are ss independent seekers.

In game ii, its seeker visits every vertex on the unique path from aia_i to bib_i, including both endpoints. The target for that game is tit_i. At each visited vertex vv, the game leader announces the distance, in edges, from vv to tit_i.

For each vertex, calculate the sum of all distances announced there throughout the games. An unvisited vertex has sum 00.

Input

  • The first line contains n,sn,s.
  • The next n−1n-1 lines each contain an undirected edge u,vu,v.
  • Each of the next ss lines contains ai,bi,tia_i,b_i,t_i: start, end and target of one game.

Output

Print nn integers, in vertex order 1,2,…,n1,2,\ldots,n, representing the sum of distances announced at each vertex.

Subtasks

General constraints: 1≤n,s≤1051\le n,s\le10^5; 1≤ai,bi,ti,u,v≤n1\le a_i,b_i,t_i,u,v\le n. Tổng có thể vượt giới hạn số nguyên 32-bit.

  • Subtask 1 (20 points): 1≤n≤401\le n\le 40; 1≤q≤501\le q\le 50.

  • Subtask 2 (30 points): 1≤n≤10001\le n\le 1000; 1≤q≤10001\le q\le 1000.

  • Subtask 3 (50 points): 1≤n≤1000001\le n\le 100000; 1≤q≤1000001\le q\le 100000.

Examples

Input

5 3
1 2
2 3
2 4
1 5
3 5 1
5 4 1
2 4 1

Output

0 3 2 4 2

Explanation

Game 1 visits 3−2−1−53-2-1-5 with target 11 and contributes 2,1,0,12,1,0,1 at those vertices. Game 2 visits 5−1−2−45-1-2-4 and adds 1,0,1,21,0,1,2. Game 3 visits 2−42-4 and adds 1,21,2 there. The totals for vertices 11 to 55 are 0,3,2,4,20,3,2,4,2.