This page is still under construction.

Parts of this page are still being built. What you see may change.

Traveling Saga

Time limit3sMemory limit256 MB

Summary
Print the order in which an apple from vertex 1 visits every tree vertex by always moving to the farthest unvisited vertex, breaking ties by largest index.
Level

Medium7 of 10

Topics
Tree, Segment tree, Greedy
Solved
No attempts yet

Problem

An unweighted tree with nn vertices holds one apple, starting at vertex 1.

The apple wants to visit every vertex exactly once. At each step it moves to the unvisited vertex farthest from its current position. If several vertices tie for farthest, it chooses the largest index.

Print the visit order.

Input

Line 1: nn (1≤n≤2500001 \le n \le 250000).

Next n−1n-1 lines: edges ss and ee (1≤s,e≤n1 \le s, e \le n, s≠es \ne e). The graph is a tree.

Output

Print the visited vertex numbers separated by spaces.

Examples1

  1. Example 1

    Input
    7
    1 2
    2 3
    2 4
    5 1
    6 5
    7 5
    
    Expected output
    1 7 4 6 3 5 2