Ancient Distance
题号:NC209696
时间限制:C/C++/Rust/Pascal 5秒,其他语言10秒
空间限制:C/C++/Rust/Pascal 256 M,其他语言512 M
64bit IO Format: %lld

题目描述

As a member of Coffee Chicken, ZYB is a boy with excellent data structure skills. 

Consider the following problem: Give a rooted tree with  vertices. Vertices are numbered from  to , and the root is always vertex . You are allowed to assign at most  key vertices so that the maximum ancient distances among all vertices is as small as possible. Denote the ancient distance of vertex  as: The distance between  and the first key vertex on the path from  to the root. For example, if the tree is 1-2-3 and the set of key vertices is , then the ancient distances of all vertices are .

ZYB then strengthens this problem: Please find the answer for each .  Could you accept ZYB's challenge?

输入描述:

The input contains multiple test cases. 
For each test case, the first line contains a integer , indicating the number of vertices in the tree.
In the second line, there are  integers, the -th integer  means that there is an edge between f_i and .
It's guaranteed that there are at most  test cases with , and the sum of  over all test cases will not exceed .

输出描述:

For each test case, you should output the sum of all answers instead of each of them.
示例1

输入

复制
3
1 2
3
1 1

输出

复制
3
2

说明

The answer for the first test case is {\{2,1,0\}}.
The answer for the second test case is {\{1,1,0\}}.

备注:

If you have any possible solution, try it bravely!