QOJ.ac

QOJ

Limite de temps : 2.0 s Limite de mémoire : 1024 MB Points totaux : 100

#10873. 寻找二叉树

Statistiques

有一只树懒住在一棵包含 $n$ 个顶点的二叉树中。顶点编号为从 $1$ 到 $n$ 的整数。

这只树懒记录了它所居住的二叉树的前序遍历和后序遍历结果,以便报告其住处。前序遍历和后序遍历的定义如下:

  • 前序遍历:访问当前顶点,然后前序遍历左子树,最后前序遍历右子树。
  • 后序遍历:后序遍历左子树,然后后序遍历右子树,最后访问当前顶点。

树懒意识到,可能存在多棵二叉树产生相同的前序和后序遍历结果,或者它可能记录错误,导致不存在这样的二叉树。你需要根据树懒记录的前序和后序遍历结果,确定有多少棵二叉树符合这些遍历结果,并将答案对 $998\,244\,353$ 取模后输出。

输入格式

第一行包含一个整数 $n$,表示二叉树的顶点数 ($1 \le n \le 5 \cdot 10^5$)。 第二行包含 $n$ 个整数,表示前序遍历结果。 第三行包含 $n$ 个整数,表示后序遍历结果。

输入中提供的前序和后序遍历结果均为 $1$ 到 $n$ 的整数排列,且没有重复元素。

输出格式

输出符合条件的二叉树数量,对 $998\,244\,353$ 取模的结果。

样例

样例输入 1

5
4 1 3 5 2
1 5 2 3 4

样例输出 1

1

样例输入 2

7
1 2 3 4 5 6 7
4 5 3 2 7 6 1

样例输出 2

4

样例输入 3

2
1 2
1 2

样例输出 3

0

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.