luogu#P4556. 【模板】线段树合并 / [Vani 有约会] 雨天的尾巴

【模板】线段树合并 / [Vani 有约会] 雨天的尾巴

Background

Shenhuili has always disliked rainy days.

Scorching weather pierced through the first half of summer, then a heavy rain and the ensuing flood extinguished everything.

Although the small village of Shenhuili’s hometown had a stubborn resistance to floods, several old houses still collapsed, a few old trees were uprooted, and the crops in the fields were left in a mess.

Helpless, Shenhuili and the villagers could only wait for relief rations to survive.

However, the distribution method was special.

Problem Description

There are nn houses in the village, forming a tree. Relief rations are distributed mm times. Each time, two houses (x,y)(x, y) are chosen. For every house on the path from xx to yy (including xx and yy), one bag of type zz relief rations is given.

After all distributions are finished, Shenhuili wants to know, for each house, which type of relief rations is stored the most.

Input Format

The first line contains two positive integers nn and mm.

Lines 22 through nn each contain two integers a,ba, b, indicating that there is an edge between houses aa and bb.

Lines (n+1)(n + 1) through (n+m)(n + m) each contain three integers x,y,zx, y, z, indicating that in one distribution, every house on the path from xx to yy receives one bag of type zz relief rations.

Output Format

Output nn lines. The integer on the ii-th line is the type of relief rations that appears most in house ii. If multiple types are tied for the maximum, output the smallest type id.

If a house has no relief rations, output 0.

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

Hint

  • For 20%20\% of the testdata, it is guaranteed that n,m100n, m \leq 100.
  • For 50%50\% of the testdata, it is guaranteed that n,m2×103n, m \leq 2 \times 10^3.
  • For 100%100\% of the testdata, it is guaranteed that 1n,m1051 \leq n, m \leq 10^5, 1a,b,x,yn1 \leq a, b, x, y \leq n, and 1z1051 \leq z \leq 10^5.

Translated by ChatGPT 5