Lepeze
Time limit2sMemory limit1024 MB
Under flip operations on a polygon triangulation, report the fewest flips needed to reach the fan at vertex x and how many shortest flip sequences exist.
- Level
Hard9 of 10
- Topics
- Combinatorics, Tree, Dynamic programming
- Solved
- No attempts yet
Statement
Little Fran received a wooden frame in the shape of a regular polygon as a gift. The polygon has vertices, so he also received wooden sticks, one for each possible diagonal. The vertices are labelled with integers from 1 to in counterclockwise order.
At the start, Fran placed sticks inside the frame. Each stick touches two non-neighboring vertices, and no two sticks cross. In other words, the sticks form a triangulation.
Fran likes to change this configuration with an operation made of two steps:
- Remove a stick.
- Add a new stick so that the result is a new triangulation.
An operation is written as an ordered pair of unordered pairs , which means Fran removed the stick touching vertices and and added a stick touching vertices and .
Fran sometimes asks how many operations it takes to turn the current triangulation into a fan triangulation at vertex , and how many ways there are to do it. A fan triangulation at vertex is one in which all diagonals share the common endpoint .
Let be the minimal number of operations. Let and be two sequences of operations that reach the target in steps. The sequences are different if for some index . Count these sequences modulo .
Input
The first line contains integers and (, ), the number of vertices and the number of events.
Each of the next lines contains integers and (), the labels of the vertices touched by the -th stick.
Each of the next lines starts with an integer (), the event type. If , four integers () follow, and the operation is performed at that moment. The operation is guaranteed to be valid. If , one integer () follows, and you must report the data for the fan triangulation at vertex for the current triangulation.
Output
For every event of type 2, in input order, print two integers on one line: the minimal number of operations, and the number of ways to reach the target triangulation with that many operations, modulo .