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

#LCA0000094. Loại hàng cứu trợ phổ biến nhất (Rainy Tails)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Loại hàng cứu trợ phổ biến nhất (Rainy Tails)

Rainy Tails

Source: Luogu

Version: Phuoc Hung OJ Extended

Problem Statement

A village contains nn houses connected by n−1n-1 undirected roads forming a tree. Each of mm relief-distribution events (x,y,z)(x,y,z) delivers one bag of type zz to every house on the unique path from xx to yy, including both endpoints.

For each house, output the type ID with the highest total number of bags. If several types tie for the maximum frequency, choose the smallest type ID. Output 0 for houses that received no bags.

Input

The first line contains n,mn,m. Next are n−1n-1 undirected edges u,vu,v, followed by mm events x,y,zx,y,z.

Output

Print nn lines. Line vv is the most frequent type ID in house vv, or 0 if the house has no bags.

Subtasks

  • Subtask 1 (20 points): 1≤n≤351\le n\le 35; 1≤m≤351\le m\le 35; 1≤u,v,x,y≤n1\le u,v,x,y\le n; 1≤z≤1051\le z\le10^5; edges form a tree.

  • Subtask 2 (30 points): 1≤n≤12001\le n\le 1200; 1≤m≤12001\le m\le 1200; 1≤u,v,x,y≤n1\le u,v,x,y\le n; 1≤z≤1051\le z\le10^5; edges form a tree.

  • Subtask 3 (50 points): 1≤n≤1000001\le n\le 100000; 1≤m≤1000001\le m\le 100000; 1≤u,v,x,y≤n1\le u,v,x,y\le n; 1≤z≤1051\le z\le10^5; edges form a tree.

Examples

Input

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

Output

3
3
7
7
3

Explanation

Event 1 delivers type 77 along 2−1−3−42-1-3-4, event 2 delivers type 33 along 2−1−3−52-1-3-5, and event 3 delivers type 77 along 4−3−54-3-5. Houses 1,21,2 tie between types 3,73,7 and choose 33. Houses 3,43,4 choose type 77. House 55 also ties and chooses 33.