Logo
(追記) (追記ここまで)

10623번 - Breadth-First Search by Foxpower 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 128 MB151786851.908%

문제

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\).

입력

The input is formatted as follows.

n
p2 p3 p4 ... pn

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.

출력

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

제한

예제 입력 1

4
1 1 2

예제 출력 1

6

예제 입력 2

4
1 1 3

예제 출력 2

4

예제 입력 3

11
1 1 3 3 2 4 1 3 2 9

예제 출력 3

25

힌트

출처

Contest > ICPC Japanese Alumni Group > JAG Spring Contest > JAG Spring Contest 2014 A번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /