Grammy Sorting
Time limit1sMemory limit1024 MB
Given a connected graph with a permutation on vertices, use at most 10000 path rotations from A to make every vertex lie on a strictly increasing A-to-B path, or report -1.
- Level
Hard10 of 10
- Topics
- Graph, Simulation, Implementation
- Solved
- No attempts yet
Problem
Grammy has a connected undirected graph with vertices numbered . Among them are two special vertices and . Each vertex has a number written on it, where is a permutation of .
Grammy thinks the numbers on the vertices are too chaotic. She wants to reorder them so that, for each vertex , there is a path that satisfies all of the following:
- The path starts at and ends at .
- The path contains vertex .
- The numbers along the path are strictly increasing.
In each operation, Grammy can only choose a simple path that starts at and ends at an arbitrary vertex. She then shifts the numbers on that path one position toward the start and moves the first number to the last position. Formally, if the vertices on the chosen simple path hold numbers from start to end, then after the operation they hold .
Grammy can perform no more than operations. Determine whether she can reorder the numbers as required. If she can, output a solution.
Input
The first line contains four integers , , , (, , , ).
The second line contains integers (). It is guaranteed that is a permutation.
Each of the next lines contains two integers and (, ), meaning there is a bidirectional edge between and . The graph is connected, and there is at most one edge between any pair of vertices.
Output
If Grammy cannot reorder the numbers as required, output -1.
Otherwise, output an integer () on the first line, the number of operations. On each of the next lines, output an integer , the number of vertices on the chosen simple path, followed by integers (, ), the vertices of the path. The must be distinct and form a path in .
You do not need to minimize . If there are multiple solutions, output any one of them.