AT_abc478_f.Min-First Search

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:1024MB

AC君温馨提醒

该题目为【atcoder】题库的题目,您提交的代码将被提交至atcoder进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

For a tree TT with NN vertices called vertex 11, vertex 22, …\ldots, vertex NN, let f(T)f(T) be the permutation PP of (1,2,…,N)(1,2,\ldots,N) obtained by the following procedure.

  • Initially, let the sequence PP be the empty sequence ()(), and let the set SS be {1}\lbrace1\rbrace.
  • Repeat the following NN times.
    • Let xx be the minimum value of SS. Remove xx from SS, and append xx to the end of PP.
    • Then, let v1,v2,…,vkv_1, v_2, \ldots, v_k be the vertices adjacent to vertex xx in TT. For i=1,2,…,ki=1,2,\ldots,k, if viv _ i is not contained in PP, add viv _ i to SS.

You are given a permutation Q=(Q1,Q2,…,QN)Q=(Q _ 1,Q _ 2,\ldots,Q _ N) of (1,2,…,N)(1,2,\ldots,N). Find the number, modulo 998244353998244353, of trees TT such that f(T)=Qf(T)=Q. Here, two trees are distinguished when there exists a pair of vertices (u,v)(u,v) such that there is an edge between vertex uu and vertex vv in one tree and there is no edge in the other tree.

对于一棵包含 NN 个顶点的树 TT,其顶点分别记为顶点 11、顶点 22、…\ldots、顶点 NN。定义 f(T)f(T) 为通过以下过程得到的 (1,2,…,N)(1,2,\ldots,N) 的一个排列 PP:

  • 初始时,令序列 PP 为空序列 ()(),集合 S={1}S = \lbrace1\rbrace。
  • 重复以下操作 NN 次:
    • 令 xx 为集合 SS 中的最小值。将 xx 从 SS 中移除,并将其追加到序列 PP 的末尾。
    • 设 v1,v2,…,vkv_1, v_2, \ldots, v_k 为树 TT 中与顶点 xx 相邻的所有顶点。对每个 i=1,2,…,ki = 1,2,\ldots,k,若 viv_i 不在 PP 中,则将 viv_i 加入 SS。

现给定 (1,2,…,N)(1,2,\ldots,N) 的一个排列 Q=(Q1,Q2,…,QN)Q = (Q_1, Q_2, \ldots, Q_N)。求满足 f(T)=Qf(T) = Q 的树 TT 的数量(对 998244353998244353 取模)。此处,若存在一对顶点 (u,v)(u,v),使得某棵树中包含边 (u,v)(u,v) 而另一棵树中不包含该边,则认为这两棵树不同。

输入格式

The input is given from Standard Input in the following format:

NN
Q1Q _ 1 Q2Q _ 2 …\ldots QNQ _ N

输入从标准输入中按以下格式给出:

NN
Q1Q _ 1 Q2Q _ 2 …\ldots QNQ _ N

输出格式

Output the number, modulo 998244353998244353, of trees TT such that f(T)=Qf(T)=Q.

输出满足 f(T)=Qf(T)=Q 的树 TT 的数量,结果对 998244353998244353 取模。

输入输出样例

  • 输入#1

    5
    1 3 4 2 5

    输出#1

    8
  • 输入#2

    20
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

    输出#2

    868586527
  • 输入#3

    30
    1 16 23 14 18 10 12 21 27 30 17 28 4 9 20 13 22 11 19 3 25 26 6 7 2 8 5 24 15 29

    输出#3

    910753763

说明/提示

Sample 1 Explanation:
For example, if TT is the following tree, then f(T)=Qf(T)=Q.

The procedure to find f(T)f(T) proceeds as shown in the following figure.

Including this tree, the following eight trees satisfy the condition.

Thus, output 8.

Sample 2 Explanation:
Output modulo 998244353998244353 the number of trees satisfying the condition.

Constraints

  • 1≤N≤2×1051\le N\le2\times10 ^ 5
  • 1≤Qi≤N (1≤i≤N)1\le Q _ i\le N\ (1\le i\le N)
  • Qi≠Qj (1≤i<j≤N)Q _ i\ne Q _ j\ (1\le i\lt j\le N)
  • Q1=1Q _ 1=1
  • All input values are integers.

样例 1 解释:
例如,若 TT 是如下所示的树,则 f(T)=Qf(T)=Q。

求 f(T)f(T) 的过程如以下图示所示。

包含该树在内,共有如下八棵满足条件的树。

因此,输出 8。

样例 2 解释:
输出满足条件的树的数量对 998244353998244353 取模的结果。

约束条件

  • 1≤N≤2×1051\le N\le2\times10 ^ 5
  • 1≤Qi≤N (1≤i≤N)1\le Q _ i\le N\ (1\le i\le N)
  • Qi≠Qj (1≤i<j≤N)Q _ i\ne Q _ j\ (1\le i\lt j\le N)
  • Q1=1Q _ 1=1
  • 所有输入值均为整数。

输入解题思路,AI测评打分。不知道怎么写?

首页