Under construction

Still being built. Things may change

Journey of the Robber

Time limit4.5sMemory limit1024 MB

Summary
Given a tree of N cities ranked by wealth, find for each city the nearest richer city, breaking distance ties by choosing the poorer one.
Level

Medium7 of 10

Topics
Tree, Union-find, Sorting
Solved
No attempts yet

Problem

Monopolis is a beautiful and wealthy country. Among its notable features is its layout: NN cities are connected by N−1N-1 roads of equal length, so any two cities can be reached from each other.

Another distinctive feature of Monopolis is that each city has one bank, and no two banks hold the same amount of money. Therefore, each city can be given a distinct number from 1 to NN that represents its wealth ranking. City 1 has the least money, and city NN has the most.

Rob is planning a "business trip" to Monopolis. Rob's business is robbing, specifically robbing banks. Rob is an ambitious robber with a strict method: he only targets banks with more money than the one he just robbed. After robbing city ii, he moves to the closest city with more money than ii. If several such cities are at the same distance, he chooses the one with the least money. If no city is richer than ii, he stays in the same city and reflects on his actions.

Rob follows this method strictly, and he asks for your help with his trip. For each city ii, he wants to know which city he would visit next if his first robbery is in city ii.

Input

The first line contains an integer NN (1≤N≤1051 \le N \le 10^5), the number of cities in Monopolis. Each city is identified by a distinct integer from 1 to NN, in increasing order of wealth.

Each of the next N−1N-1 lines contains two integers UU and VV (1≤U,V≤N1 \le U, V \le N, U≠VU \ne V), meaning there is a two-way road between cities UU and VV. The given roads guarantee a path between every pair of cities.

Output

Print a single line with NN integers. The ii-th integer is the next city Rob would visit if his first robbery is in city ii.

Examples3

  1. Example 1

    Input
    6
    1 6
    2 5
    4 5
    3 5
    5 6
    
    Expected output
    6 5 5 5 6 6
    
  2. Example 2

    Input
    5
    5 1
    1 3
    3 2
    2 4
    
    Expected output
    3 3 4 5 5
    
  3. Example 3

    Input
    1
    
    Expected output
    1