10595: ABC357 —— E - Reachability in Functional Graph
[Creator : ]
Description
There is a directed graph with $N$ vertices numbered $1$ to $N$ and $N$ edges.
The out-degree of every vertex is $1$, and the edge from vertex $i$ points to vertex $a_i$.
Count the number of pairs of vertices $(u, v)$ such that vertex $v$ is reachable from vertex $u$.
Here, vertex $v$ is reachable from vertex $u$ if there exists a sequence of vertices $w_0, w_1, \dots, w_K$ of length $K+1$ that satisfies the following conditions. In particular, if $u = v$, it is always reachable.
- $w_0 = u$.
- $w_K = v$.
- For every $0 \leq i \lt K$, there is an edge from vertex $w_i$ to vertex $w_{i+1}$.
The out-degree of every vertex is $1$, and the edge from vertex $i$ points to vertex $a_i$.
Count the number of pairs of vertices $(u, v)$ such that vertex $v$ is reachable from vertex $u$.
Here, vertex $v$ is reachable from vertex $u$ if there exists a sequence of vertices $w_0, w_1, \dots, w_K$ of length $K+1$ that satisfies the following conditions. In particular, if $u = v$, it is always reachable.
- $w_0 = u$.
- $w_K = v$.
- For every $0 \leq i \lt K$, there is an edge from vertex $w_i$ to vertex $w_{i+1}$.
Input
The input is given from Standard Input in the following format:
```
$N$
$a_1$ $a_2$ $\dots$ $a_N$
```
```
$N$
$a_1$ $a_2$ $\dots$ $a_N$
```
Output
Print the number of pairs of vertices $(u, v)$ such that vertex $v$ is reachable from vertex $u$.
Constraints
- $1 \leq N \leq 2 \times 10^5$
- $1 \leq a_i \leq N$
- All input values are integers.
- $1 \leq a_i \leq N$
- All input values are integers.
Sample 1 Input
4
2 1 1 4
Sample 1 Output
8
The vertices reachable from vertex $1$ are vertices $1, 2$.
The vertices reachable from vertex $2$ are vertices $1, 2$.
The vertices reachable from vertex $3$ are vertices $1, 2, 3$.
The vertex reachable from vertex $4$ is vertex $4$.
Therefore, the number of pairs of vertices $(u, v)$ such that vertex $v$ is reachable from vertex $u$ is $8$.
Note that the edge from vertex $4$ is a self-loop, that is, it points to vertex $4$ itself.
Sample 2 Input
5
2 4 3 1 2
Sample 2 Output
14
Sample 3 Input
10
6 10 4 1 5 9 8 6 5 1
Sample 3 Output
41