Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

A - Breadth-First Search by Foxpower

Problem Statement

Fox Ciel went to JAG Kingdom by bicycle, but she forgot a place where she parked her bicycle. So she needs to search it from a bicycle-parking area before returning home.

The parking area is formed as a unweighted rooted tree $T$ with $n$ vertices, numbered $1$ through $n$. Each vertex has a space for parking one or more bicycles. Ciel thought that she parked her bicycle near the vertex $1$, so she decided to search it from there by the breadth-first search. That is, she searches it at the vertices in the increasing order of their distances from the vertex $1$. If multiple vertices have the same distance, she gives priority to the vertices in the order of searching at their parents. If multiple vertices have the same parent, she searches at the vertex with minimum number at first.

Unlike a computer, she can't go to a next vertex by random access. Thus, if she goes to the vertex $j$ after the vertex $i$, she needs to walk the distance between the vertices $i$ and $j$. BFS by fox power perhaps takes a long time, so she asks you to calculate the total moving distance in the worst case starting from the vertex $1$.

Input

The input is formatted as follows.

$n$
$p_2$ $p_3$ $p_4$ $\cdots$ $p_n$

The first line contains an integer $n$ ($1 \le n \le 10^5$), which is the number of vertices on the unweighted rooted tree $T$.
The second line contains $n-1$ integers $p_i$ ($1 \le p_i < i$), which are the parent of the vertex $i$.
The vertex $1$ is a root node, so $p_1$ does not exist.

Output

Print the total moving distance in the worst case in one line.

Sample Input 1

4
1 1 2

Output for the Sample Input 1

6

Sample Input 2

4
1 1 3

Output for the Sample Input 2

4

Sample Input 3

11
1 1 3 3 2 4 1 3 2 9

Output for the Sample Input 3

25

Solution in Ruby (judge verdict: Runtime Error)

n = gets.to_i
g = {}
(1..n).map {|i| g[i] = []}
depth = {1 => 0}
gets.split.map(&:to_i).each_with_index do |x, i|
	g[x] << i+2
	depth[i+2] = depth[x] + 1
end

bfs_order = []
queue = [1]
idx = 0
until queue.empty?
	u = queue.shift
	bfs_order << u
	g[u].each {|v| queue << v}
end

def compute_euler_tour_index(graph, u, next_idx, et)
	et[u] = [next_idx]
	next_idx += 1
	graph[u].each do |v|
		next_idx = compute_euler_tour_index(graph, v, next_idx, et)
		et[u] << next_idx
		next_idx += 1
	end
	return next_idx
end

et = {}
compute_euler_tour_index(g, 1, 0, et)

ary = []
et.each do |k, is|
	is.each do |i|
		ary[i] = depth[k]
	end
end

class SegmentTree
	def initialize(arr, l = 0, r = arr.size - 1)
		@l = l
		@r = r
		if l == r
			@min = arr[l]
		else
			c = (l + r) / 2
			@left_child = self.class.new(arr, l, c)
			@right_child = self.class.new(arr, c+1, r)
			lm = @left_child.min
			rm = @right_child.min
			@min = (lm < rm ? lm : rm)
		end
	end

	def query(l, r)
		return @min if l == @l && r == @r
		if r <= @left_child.r
			return @left_child.query(l, r)
		elsif @right_child.l <= l
			return @right_child.query(l, r)
		else
			lm = @left_child.query(l, @left_child.r)
			rm = @right_child.query(@right_child.l, r)
			return lm < rm ? lm : rm
		end
	end

	attr_reader :min, :l, :r
end

tree = SegmentTree.new(ary)

total_dist = 0
bfs_order.each_cons(2) do |u, v|
	is = (et[u] + et[v]).sort
	lca_depth = tree.query(is[0], is[-1])
	total_dist += (ary[et[u][0]] + ary[et[v][0]] - lca_depth * 2)
end
p total_dist