atcoder#ABC252G. [ABC252G] Pre-Order
[ABC252G] Pre-Order
Score : points
Problem Statement
There is a rooted tree with vertices called Vertex , , , , rooted at Vertex .
We performed a depth-first search starting at the root and obtained a preorder traversal of the tree: . During the search, when the current vertex had multiple children, we chose the unvisited vertex with the smallest index.
What is a preorder traversal?
-
If the current vertex is not recorded yet, record it.
-
Then, if has an unvisited vertex, go to that vertex.
-
Otherwise, terminate if is the root, and go to the parent of if it is not.
Constraints
- All are distinct.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print the number of rooted trees consistent with the preorder traversal, modulo .
4
1 2 4 3
3
The rooted trees consistent with the preorder traversal are the three shown below, so the answer is .
Note that the tree below does not count. This is because, among the children of Vertex , we visit Vertex before visiting Vertex , resulting in the preorder traversal .
8
1 2 3 5 6 7 8 4
202