atcoder#ARC112C. [ARC112C] DFS Game

[ARC112C] DFS Game

Score : 500500 points

Problem Statement

Takahashi and Aoki will play a game using a rooted tree with nn vertices, numbered 11 through nn. The root of the tree is Vertex 11. For each v=2,,nv=2,\dots,n, the parent of Vertex vv is pvp_v. Initially, there is one coin on each vertex. Additionally, there is a piece on Vertex 11.

Takahashi and Aoki will alternately do the following operation, with Takahashi going first:

  • If there is a coin on the vertex occupied by the piece: get the coin and end his turn.
  • If there is no coin on the vertex occupied by the piece: move the piece (or end the game) as follows.- If there are vertices with coins on them among the children of the vertex occupied by the piece, choose one such vertex, move the piece to that vertex, and end his turn.
    • Otherwise, if the piece is on the root, end the game; if not, move the piece to the parent of the vertex occupied by the piece and end his turn.
  • If there are vertices with coins on them among the children of the vertex occupied by the piece, choose one such vertex, move the piece to that vertex, and end his turn.
  • Otherwise, if the piece is on the root, end the game; if not, move the piece to the parent of the vertex occupied by the piece and end his turn.

Both Takahashi and Aoki will play optimally to maximize the number of coins they will get. Find the number of coins Takahashi will get.

Constraints

  • 2n1052\le n \le 10^5
  • 1pv<v1\le p_v \lt v

Input

Input is given from Standard Input in the following format:

nn

p2p_2 \dots pnp_n

Output

Print the number of coins Takahashi will get when the two players play optimally.

10
1 2 3 4 5 6 7 8 9
10

There is no choice for the players, and the game will always go as follows, so Takahashi will get every coin.

  • Takahashi gets the coin on Vertex 11.
  • Aoki moves the piece to Vertex 22.
  • Takahashi gets the coin on Vertex 22.
  • Aoki moves the piece to Vertex 33.
  • \vdots
  • Takahashi gets the coin on Vertex 1010.
  • Aoki moves the piece to Vertex 99.
  • Takahashi moves the piece to Vertex 88.
  • Aoki moves the piece to Vertex 77.
  • \vdots
  • Takahashi moves the piece to Vertex 22.
  • Aoki moves the piece to Vertex 11.
  • The game ends.
5
1 2 3 1
2

First, Takahashi gets the coin on Vertex 11. Then, Aoki can move the piece to Vertex 22 or Vertex 55. If he moves it to Vertex 22, Aoki will eventually get just the coin on Vertex 55. On the other hand, if he moves it to Vertex 55, Aoki will eventually get the coins on Vertex 22, 33, and 44. Since Aoki will play optimally to maximize the number of coins he will get, he will choose to move the piece to Vertex 55. Thus, Takahashi will get two coins.

10
1 1 3 1 3 6 7 6 6
5