102526: [AtCoder]ABC252 G - Pre-Order

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Score : $600$ points

Problem Statement

There is a rooted tree with $N$ vertices called Vertex $1$, $2$, $\ldots$, $N$, rooted at Vertex $1$.

We performed a depth-first search starting at the root and obtained a preorder traversal of the tree: $P_1, P_2, \ldots, P_N$.
During the search, when the current vertex had multiple children, we chose the unvisited vertex with the smallest index.

What is a preorder traversal? We start at the root and repeat the following procedure to list the vertices of the tree.
  • If the current vertex $u$ is not recorded yet, record it.
  • Then, if $u$ has an unvisited vertex, go to that vertex.
  • Otherwise, terminate if $u$ is the root, and go to the parent of $u$ if it is not.
  • The list of vertices in the order they are recorded here is the preorder traversal of the tree.

    Find the number of rooted trees consistent with the preorder traversal, modulo $998244353$.
    Two rooted trees (with $N$ vertices and rooted at Vertex $1$) are considered different when there is a non-root vertex whose parent is different in the two trees.

    Constraints

    • $2 \leq N \leq 500$
    • $1 \leq P_i\leq N$
    • $P_1=1$
    • All $P_i$ are distinct.
    • All values in input are integers.

    Input

    Input is given from Standard Input in the following format:

    $N$
    $P_1$ $P_2$ $\ldots$ $P_N$
    

    Output

    Print the number of rooted trees consistent with the preorder traversal, modulo $998244353$.


    Sample Input 1

    4
    1 2 4 3
    

    Sample Output 1

    3
    

    The rooted trees consistent with the preorder traversal are the three shown below, so the answer is $3$.

    Note that the tree below does not count. This is because, among the children of Vertex $2$, we visit Vertex $3$ before visiting Vertex $4$, resulting in the preorder traversal $1,2,3,4$.


    Sample Input 2

    8
    1 2 3 5 6 7 8 4
    

    Sample Output 2

    202
    

    Input

    题意翻译

    存在一棵 $ n $ 个点的树,给定序列 $ P_n $ 表示树的先序遍历,特别地,已知当一个节点有多个儿子的时候会优先遍历编号较小的儿子。求满足条件的树的方案数。对 $ 998244353 $ 取模。

    Output

    分数:$600$分 部分 问题描述 在名为顶点$1$、$2$、$\ldots$、$N$的有根树中,根节点为顶点$1$。 从根节点开始进行了深度优先搜索,并得到了树的先序遍历:$P_1, P_2, \ldots, P_N$。在搜索过程中,当当前顶点有多个子节点时,我们选择了尚未访问过的具有最小索引的子节点。
    什么是先序遍历? 我们从根节点开始,重复以下过程来列出树的顶点。
  • 如果当前顶点$u$尚未记录,则记录它。
  • 然后,如果$u$有未访问的顶点,则转到该顶点。
  • 否则,如果$u$是根节点,则终止;如果它不是根节点,则转到$u$的父节点。
  • 按记录顺序列出的顶点是树的先序遍历。
    找到与先序遍历一致的有根树的数量,对$998244353$取模。如果两棵有根树(具有$N$个顶点,根节点为顶点$1$)在某个非根顶点的父节点不同的情况下被认为是不同的。 部分 限制
    • $2 \leq N \leq 500$
    • $1 \leq P_i\leq N$
    • $P_1=1$
    • 所有$P_i$都不同。
    • 输入中的所有值都是整数。
    部分 输入 输入从标准输入中给出以下格式: $N$ $P_1$ $P_2$ $\ldots$ $P_N$ 部分 输出 打印与先序遍历一致的有根树的数量,对$998244353$取模。 样例输入 1 4 1 2 4 3 样例输出 1 3 与先序遍历一致的有根树如下所示,因此答案为$3$。

    注意,下面的树不计算在内。这是因为,在顶点$2$的子节点中,我们先访问顶点$3$再访问顶点$4$,导致先序遍历为$1,2,3,4$。

    样例输入 2 8 1 2 3 5 6 7 8 4 样例输出 2 202

    加入题单

    上一题 下一题 算法标签: