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 houses in the village, forming a tree. Relief rations are distributed times. Each time, two houses are chosen. For every house on the path from to (including and ), one bag of type 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 and .
Lines through each contain two integers , indicating that there is an edge between houses and .
Lines through each contain three integers , indicating that in one distribution, every house on the path from to receives one bag of type relief rations.
Output Format
Output lines. The integer on the -th line is the type of relief rations that appears most in house . 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 of the testdata, it is guaranteed that .
- For of the testdata, it is guaranteed that .
- For of the testdata, it is guaranteed that , , and .
Translated by ChatGPT 5