U505050 tree

题目描述

In the autumn campus, the trees are left with only withered branches and leaves. Thus hky, he decides to rebuild a tree. Initially, the tree has only one root node. The process of building the tree involves a total of $n-1$ operations. During the $i$-th operation, he will choose point $i+1$ and connect it to a point $x$ on the tree. He will then calculate the maximum distance from point $i+1$ to any point on the tree, which is $dis_{i} = \max\limits_{1\le j\le i}(d_{i+1,j})$, where $d_{i,j}$ is the distance between points $i$ and $j$ on the tree. Definition: The distance between two points on the tree is the length of the simple path between them, and the length of each edge on tree is $1$. However, he is feeling lazy now. He hopes you can tell him the sum of all results, which is $ans = \sum\limits_{i=1}^{n-1} dis_i$

输入格式

The first line contains an integer $1 \le n \le 2 \times 10^5$, representing the number of nodes in the tree. The second line contains $n-1$ numbers $x_1, x_2, \cdots, x_{n-1}$, indicating that the $i$-th operation connects node $i+1$ to node $x_i$. It is guaranteed that $1 \le x_i \le i$.

输出格式

Output an integer $ans$ representing the answer.

说明/提示

Here is the sample calculation process: $dis_1=1$、$dis_2=2$、$dis_3=3$、$dis_4=3$、$dis_5=4$ $ans = 1+2+3+3+4 = 13$