Grasshopper Route

Given a tree and two vertices s and t, construct the specific valid Grasshopper route defined by a recursive rule on the path components.

Medium7TreeDFSRecursionImplementationNo attempts yetTime limit1sMemory limit512 MB

Problem

A grasshopper is exploring a tree. A tree is an undirected graph in which every two vertices are joined by exactly one path. A grasshopper standing on a vertex can jump to another vertex only if the distance between the two vertices is at most 33, where the distance is the number of edges on the path that joins them. The grasshopper wants to visit every vertex of the tree exactly once, starting at the vertex ss it stands on and finishing at the vertex tt it has chosen.

You are given a tree with nn vertices and two vertices ss and tt. A grasshopper route for ss and tt is an ordering u1,u2,,un\langle u_1, u_2, \ldots, u_n \rangle of all vertices of the tree such that u1=su_1 = s, un=tu_n = t, and the grasshopper can jump from uiu_i to ui+1u_{i+1} for every ii in {1,2,,n1}\{1, 2, \ldots, n-1\}. Every pair of vertices of a tree is joined by a grasshopper route, which was proved in 1960.

In the tree drawn below, 7,6,5,4,1,2,3,8,9,11,12,10\langle 7, 6, 5, 4, 1, 2, 3, 8, 9, 11, 12, 10 \rangle is a grasshopper route for s=7s = 7 and t=10t = 10. The grasshopper can jump from vertex 55 to vertex 44 because the distance between them is 33, but it cannot jump from vertex 55 to vertex 33. A tree can have more than one grasshopper route, so the output section fixes one of them.

The figure shows one grasshopper route from s=7s = 7 to t=10t = 10.

Input

The first line contains an integer nn, the number of vertices of the tree (2n1000002 \le n \le 100\,000). Each of the next n1n - 1 lines contains two integers uu and vv that describe an edge between vertex uu and vertex vv. The vertices are numbered from 11 to nn. The last line contains two distinct integers ss and tt, the start vertex and the end vertex of the route.

Output

A tree can have many grasshopper routes, so print the one that the rule below picks. Print the vertices the grasshopper passes through in nn lines, one vertex per line, in order.

  1. Let x1=s,x2,,xk=tx_1 = s, x_2, \ldots, x_k = t be the unique path from ss to tt in the tree.
  2. Delete the k1k - 1 edges of that path. The tree falls apart into kk components. Call CiC_i the component that contains xix_i, and root CiC_i at xix_i.
  3. For a vertex vv of a rooted tree, define the sequence f(v)f(v) as follows. If vv has no child, f(v)f(v) is the one term sequence vv. Otherwise let c1<c2<<cmc_1 < c_2 < \cdots < c_m be the children of vv in increasing order of vertex number, and let f(v)f(v) be vv, followed by f(c1)f(c_1) written backwards, then f(c2)f(c_2) written backwards, and so on up to f(cm)f(c_m) written backwards.
  4. The answer is f(x1),f(x2),,f(xk1)f(x_1), f(x_2), \ldots, f(x_{k-1}) concatenated in this order, followed by f(xk)f(x_k) written backwards. Each f(xi)f(x_i) is computed inside the component CiC_i.

This ordering is always a grasshopper route.