Reversing a Snake on a Complete Graph
Time limit1sMemory limit128 MB
Given a snake path on a complete graph, decide whether the head and tail can be swapped by a sequence of legal moves.
- Level
Medium5 of 10
- Topics
- Graph, Greedy, Implementation
- Solved
- No attempts yet
Problem
A game company decided to build a new version of the classic game Snake, played on a complete graph. A complete graph is a graph in which every two distinct vertices are joined by an edge.
In this game a snake of length is a directed path through distinct vertices of the complete graph, where is the head and is the tail; consecutive vertices are joined by an edge.
A single move works as follows. The head moves to some vertex that is currently not occupied by the snake (empty). The whole body then shifts forward by one, the tail cell becomes empty, and the snake becomes . Because the graph is complete, may be any currently empty vertex. (The head may not move onto a vertex occupied by the body or the tail.)
A mathematician proved that if the snake can be reversed in finitely many moves — that is, made to occupy exactly the same edges while the head and the tail swap places — then this is equivalent to being able to reach any arrangement of the snake on the graph. Testing the game therefore reduces to deciding, for a given starting arrangement, whether the snake can be reversed.
Reversing the snake means performing moves until the snake becomes (the starting path read backwards).
For each scenario, decide whether the snake can be reversed.
Input
The first line contains the number of scenarios ().
Each scenario follows. A scenario consists of the number of vertices (), the snake length (), and then distinct vertex numbers describing the snake from head to tail. Vertices are numbered from to .
Output
For each scenario, print on its own line YES if the snake can be reversed, or NO otherwise.