問題一覧 > 通常問題

No.1878 Counting Union-Find

レベル : / AI推定難易度 : 1600 / 実行時間制限 : 1ケース 2.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 38
作問者 : 37zigen / テスター : tko919 👑 cologne
お気に入りにしたユーザー ProblemId : 7551 / 自分の提出
問題文最終更新日: 2022-03-18 21:50:27
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
yukicoder contest 336 (順位表) の他の問題:

Overview

There is a rooted tree $T$ representing a union-find structure produced by some operations.
This union-find did not implement path compression. How many union-find trees \(S\) could have resulted if path compression had been implemented?

That is, for a fixed \(T\), how many \(S\) are there such that we get

  • the rooted tree \(T\) without path compression, and
  • the rooted tree \(S\) with path compression?

Problem

Let $T$ be a rooted tree consisting of vertex $1$, vertex $2$, …, vertex $N$, with vertex $1$ as the root. For each vertex $2 \leq i \leq N $ of $T$, its parent is $p_i$.
It is known that this tree was obtained from $N$ rooted trees with $1$ vertex each, consisting of vertex $1$, vertex $2$, …, vertex $N$ respectively, by performing the following $2$ operations $0$ or more times.

  • union(a, b)
    • Connect root $a$ and root $b$ with an edge, making $a$ the parent of $b$.
  • root(a)
    • Follow the ancestors of vertex $a$ and return the root found.

With path compression, the algorithm of root becomes the following.

  • root(a)
    • Follow the ancestors of vertex $a$ and return the root found. In doing so, change the parent of each ancestor of $a$ (including $a$ itself), except the root, to the root.

Find the number of trees that could have resulted with path compression, modulo $998244353$.

Input

$N$
$p_2$
$p_3$
$\vdots$
$p_N$
  • $1 \leq N \leq 10^5$
  • $1 \leq p_i \leq N$
  • All values in the input are integers.
  • The graph represented by the input is a tree.

Output

Print a newline at the end.

Sample

Sample 1
Input
1
Output
1

Sample 2
Input
4
1
2
2
Output
4

When path compression is implemented, after the tree $T$ is formed by union(2, 3), union(2, 4), union(1, 2),

  • doing nothing gives the first tree from the left,
  • performing root(3) gives the second tree from the left,
  • performing root(4) gives the third tree from the left, and
  • performing root(3) and root(4) gives the fourth tree from the left.

There are other operation sequences, but they all end up producing one of these four trees. For example, the operation sequence union(2, 3), union(3, 4), root(4), union(1, 2) does not match any of those described above, but it produces the tree $T$.

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。