No.1878 Counting Union-Find
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.
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。
37zigen
tko919