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 MBA 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 3, 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 s it stands on and finishing at the vertex t it has chosen.
You are given a tree with n vertices and two vertices s and t. A grasshopper route for s and t is an ordering ⟨u1,u2,…,un⟩ of all vertices of the tree such that u1=s, un=t, and the grasshopper can jump from ui to ui+1 for every i in {1,2,…,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⟩ is a grasshopper route for s=7 and t=10. The grasshopper can jump from vertex 5 to vertex 4 because the distance between them is 3, but it cannot jump from vertex 5 to vertex 3. 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=7 to t=10.
The first line contains an integer n, the number of vertices of the tree (2≤n≤100000). Each of the next n−1 lines contains two integers u and v that describe an edge between vertex u and vertex v. The vertices are numbered from 1 to n. The last line contains two distinct integers s and t, the start vertex and the end vertex of the route.
A tree can have many grasshopper routes, so print the one that the rule below picks. Print the vertices the grasshopper passes through in n lines, one vertex per line, in order.
This ordering is always a grasshopper route.