#abc357e. E - Reachability in Functional Graph
E - Reachability in Functional Graph
Score : points
问题陈述
有一个有向图,包含 个顶点,编号从 到 ,以及 条边。 每个顶点的出度为 ,从顶点 出发的边指向顶点 。 计算顶点对 的数量,使得从顶点 可以到达顶点 。
这里,如果存在一个顶点序列 ,长度为 ,满足以下条件,则顶点 可以从顶点 到达。特别是,如果 ,则总是可以到达。
- 。
- 。
- 对于每个 ,存在一条从顶点 到顶点 的边。
以上为大语言模型 kimi 翻译,仅供参考。
Problem Statement
There is a directed graph with vertices numbered to and edges.
The out-degree of every vertex is , and the edge from vertex points to vertex .
Count the number of pairs of vertices such that vertex is reachable from vertex .
Here, vertex is reachable from vertex if there exists a sequence of vertices of length that satisfies the following conditions. In particular, if , it is always reachable.
- .
- .
- For every , there is an edge from vertex to vertex .
Constraints
- All input values are integers.
Input
The input is given from Standard Input in the following format:
Output
Print the number of pairs of vertices such that vertex is reachable from vertex .
Sample Input 1
4
2 1 1 4
Sample Output 1
8
The vertices reachable from vertex are vertices .
The vertices reachable from vertex are vertices .
The vertices reachable from vertex are vertices .
The vertex reachable from vertex is vertex .
Therefore, the number of pairs of vertices such that vertex is reachable from vertex is .
Note that the edge from vertex is a self-loop, that is, it points to vertex itself.
Sample Input 2
5
2 4 3 1 2
Sample Output 2
14
Sample Input 3
10
6 10 4 1 5 9 8 6 5 1
Sample Output 3
41