#P3549. Byteasar's Milk Bar Tour
Byteasar's Milk Bar Tour
Byteasar's Milk Bar Tour
Byteasar lives in Byteburg – a city famous for having a milk bar at every corner. One day, he devised a "Milk Overload Plan": he wishes to visit each milk bar exactly once. Ideally, he wants to design a route so that when heading from one milk bar to the next, the distance does not exceed two city blocks (i.e. the shortest path between the corresponding intersections is at most 2).
The city has intersections numbered from 1 to N, and all streets are bidirectional. Between every pair of intersections there is a unique simple path (i.e. the network forms a tree). Byteasar will start from intersection 1 and finish at intersection N. Your task is to find any ordering (a permutation of the intersections) satisfying Byteasar’s requirement – that is, for every two consecutive intersections u and v in the route, the distance between u and v in the tree is either 1 or 2. If no such ordering exists, output -1.
Note: When two intersections are not directly connected by a street, they can still be consecutive in the route if they have a common neighboring intersection in the tree.
inputFormat
The first line contains an integer N (the number of intersections).
Each of the next N-1 lines contains two integers u and v (1 ≤ u, v ≤ N) describing a bidirectional street between intersections u and v.
outputFormat
If a valid route exists, output a single line containing a permutation of the integers from 1 to N (space-separated) where the first number is 1 and the last number is N, and the distance between every two consecutive numbers is at most 2 in the tree. Otherwise, output -1.
sample
2
1 2
1 2