Pondo Euler Tour
View as PDFYou are given a tree with vertices, rooted at vertex
. Output the Euler tour of
this tree.
Start a depth-first search at . Record a vertex when you first enter it, visit all of
its children, then record it again when you leave. If a vertex has several children, visit
them in increasing label order.
You only need the first and last visit of each vertex, so the tour has length .
Input
The first line contains a single integer .
Each of the next lines contains two integers
and
, denoting an undirected
edge between
and
.
Vertices are numbered through
. The edges form a tree.
Output
Print a single line of space-separated integers: the Euler tour, starting at
.
Constraints
and
Example 1
Input
8
1 2
2 3
2 4
1 5
5 6
6 7
6 8
Output
1 2 3 3 4 4 2 5 6 7 7 8 8 6 5 1
Explanation
The tree, rooted at , looks like this:
1
/ \
2 5
/ \ \
3 4 6
/ \
7 8
The search enters , then visits child
before child
. It records each vertex on
entry and again on exit, which produces the tour above.
Example 2
Input
10
3 10
3 5
1 3
5 7
2 5
2 4
5 8
6 8
6 9
Output
1 3 5 2 4 4 2 7 7 8 6 9 9 6 8 5 10 10 3 1
Comments