ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules

2226. County of Squirrels

Time limit: 1.0 second
Memory limit: 256 MB
“In the country... cities... roads... the only way... Maybe I should just tell them that there is a tree?” thought Vadim...
In the county of squirrels, there are N trees, and there are N−1 paths connecting some pairs of trees so that from any tree, you can reach any other tree by a sequence of paths. If we number all the paths from 1 to N−1 and all the trees from 1 to N, then the i-th path is trodden between trees ui and vi and has a length of li meters.
The squirrel Vadim is looking for a place to store supplies for the winter. This needs to be done in such a way that no other squirrel from the county can find these supplies. To do this, Vadim came up with a simple strategy to shake off any curious relatives without running too much himself. He will start at an arbitrary tree, then evaluate the lengths of all the paths leading from this tree and will run along the shortest one. Upon reaching the next tree, he will repeat this operation, excluding trees he has already visited that day. Vadim will continue this action until he arrives at a tree from which all paths lead to trees he has already visited. In the hollow of this tree, Vadim will set up his “warehouse.”
The squirrel Nastya has learned about Vadim’s strategy, and she needs to find out which tree Vadim will hide his supplies in. Unfortunately, she does not know which tree Vadim started his journey from. Help Nastya find the corresponding tree for each of the trees in the county.

Input

The first line contains an integer N — the number of trees in the county of squirrels (2 ≤ N ≤ 105).
In the following N−1 lines, there are three integers ui, vi, and li — the numbers of the trees connected by the i-th path and its length (1 ≤ ui, viN, uivi, 1 ≤ li ≤ 109). It is guaranteed that the lengths of all paths are distinct.

Output

Output N integers fi — the number of the tree where Vadim’s “warehouse” is located if he started near the i-th tree.

Sample

inputoutput
5
1 2 109
2 3 547
2 4 652
5 3 318
5 1 5 1 1
Problem Author: Vadim Barinov
Problem Source: Ural Championship 2025