#P12803. [AMPPZ 2019] Little Worm
[AMPPZ 2019] Little Worm
Description
Little Worm is living on a tree. The tree has vertices (and is a connected, undirected acyclic graph), and Worm occupies the whole path between the vertices and .
Worm would like to move to another path – the one between vertices and – as it is more sunny there. It is known that the paths and have no vertices in common.
To change its position on the tree, Worm can make some moves, which consist of entering a free vertex with Worm’s either end. Formally, if Worm is currently occupying a path between and , it may choose a new vertex adjacent to , which is not on the path . Then Worm frees (stops occupying) , taking instead. In a similar way, Worm can choose a vertex adjacent to , free and occupy . After a single move Worm still occupies some path, and its length does not change.
Worm is aiming to get to the path between and , but being quite lazy, it doesn’t plan for more than moves. Can you help it reach its goal within that limit?
Input Format
The first line of input contains the number of test cases (). The test cases follow, each one in the following format:
- The first line of a test case contains a single integer () – the number of the vertices of a tree. Each of the following lines contains two integers (), describing the endpoints of a single edge.
- In the next line two integers and () are given. These are the endpoints of the path that is Worm’s starting position.
- The next line contains the endpoints of the path which is Worm’s goal, given as two integers and ().
The number of vertices on the path between and match the number of vertices on the path between and . You may also assume that those two paths have no common vertices.
The sum of all values of over all test cases does not exceed .
Output Format
For every test case, if Worm cannot reach its goal in moves, output . Otherwise, output a possible sequence of Worm’s moves in two lines:
- First line: the number of moves (),
- Second line: integers – the required moves.
For , the value should denote the vertex which is entered by Worm in the -th move.
You may output any correct sequence that moves Worm to the goal and has no more than moves (in particular, you do not have to minimize the number of moves). Assume that Worm is symmetrical – it can move in both directions and it can enter the goal path facing either side.
3
6
1 2
1 3
1 4
4 5
4 6
2 3
5 6
15
1 2
1 6
2 3
2 4
2 5
6 7
6 8
5 9
6 10
9 11
9 12
9 13
12 14
14 15
14 13
3 6
6
1 2
1 3
2 4
4 5
5 6
4 6
3 2
-1
7
15 5 2 1 6 7 3
3
2 1 3
京公网安备 11011102002149号