Asya And Kittens
Contest: Codeforces Round 541 (Div. 2)
Difficulty rating: 1700
Tags: constructive algorithms, dsu

Asya loves animals very much. Recently, she purchased $$$n$$$ kittens, enumerated them from $$$1$$$ and $$$n$$$ and then put them into the cage. The cage consists of one row of $$$n$$$ cells, enumerated with integers from $$$1$$$ to $$$n$$$ from left to right. Adjacent cells had a partially transparent partition wall between them, hence there were $$$n - 1$$$ partitions originally. Initially, each cell contained exactly one kitten with some number.

Observing the kittens, Asya noticed, that they are very friendly and often a pair of kittens in neighboring cells wants to play together. So Asya started to remove partitions between neighboring cells. In particular, on the day $$$i$$$, Asya:

- Noticed, that the kittens $$$x_i$$$ and $$$y_i$$$, located in neighboring cells want to play together.
- Removed the partition between these two cells, efficiently creating a single cell, having all kittens from two original cells.

Since Asya has never putted partitions back, after $$$n - 1$$$ days the cage contained a single cell, having all kittens.

For every day, Asya remembers numbers of kittens $$$x_i$$$ and $$$y_i$$$, who wanted to play together, however she doesn't remember how she placed kittens in the cage in the beginning. Please help her and find any possible initial arrangement of the kittens into $$$n$$$ cells.

Input format:
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 150\,000$$$) — the number of kittens.

Each of the following $$$n - 1$$$ lines contains integers $$$x_i$$$ and $$$y_i$$$ ($$$1 \le x_i, y_i \le n$$$, $$$x_i \ne y_i$$$) — indices of kittens, which got together due to the border removal on the corresponding day.

It's guaranteed, that the kittens $$$x_i$$$ and $$$y_i$$$ were in the different cells before this day.

Output format:
For every cell from $$$1$$$ to $$$n$$$ print a single integer — the index of the kitten from $$$1$$$ to $$$n$$$, who was originally in it.

All printed integers must be distinct.

It's guaranteed, that there is at least one answer possible. In case there are multiple possible answers, print any of them.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
The answer for the example contains one of several possible initial arrangements of the kittens.

The picture below shows how the cells were united for this initial arrangement. Note, that the kittens who wanted to play together on each day were indeed in adjacent cells.

Example input:
5
1 4
2 5
3 1
4 5

Example output:
3 1 4 2 5

Editorial:
In this problem we are given a disjoint set union process with $$$n - 1$$$ steps, merging $$$n$$$ initial 1-element sets into one $$$n$$$-element set. We have to put elements into a linear array of cells, so that the cells to be joined at each step of the process were immediate neighbours (i.e. not separated by other cells).
This problem can be solved in $$$O(n\log n)$$$ or in $$$O(n\alpha(n))$$$ (where $$$\alpha(n)$$$ is the inverse Ackermann function) via standard disjoint-set data structure, additionally storing lists of elements in each set.
The simplest solution is based on a set-size version of rank heuristic:
• storing mapping from item to id (representative) of its current set, and the inverse mapping from set to the list of its elements
• when we have to merge two sets $$$A$$$ and $$$B$$$, we make the smaller set part of the larger set and update mappings, assigning new set ids for elements in $$$O(min(|A|, |B|))$$$ and concatenating the lists (can be done in $$$O(1)$$$ or in $$$O(min(|A|, |B|))$$$)
This gives us $$$O(n\log n)$$$: element can not change its set more than $$$log n$$$ times, because the change leads to (at least) doubling of the element's set size.
In order to get $$$O(n\alpha(n))$$$, we have to use the disjoint set structure with both path compression and rank heuristics, plus concatenation of lists should be done in $$$O(1)$$$.