PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BS0000336. Lây nhiễm trên cây (Tree Infection)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Lây nhiễm trên cây (Tree Infection)

Tree Infection

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

A rooted tree with root 1 has nn initially healthy vertices. Each second consists of simultaneous spreading followed by injection. During spreading, for every vertex v that has an infected direct child at the beginning of the second, at most one of its healthy direct children may become infected. Then at most one arbitrary healthy vertex may be infected by injection. Infection is permanent. Minimize the time to infect every vertex.

Input

First line: nn. Second line: n−1n-1 integers p2,…,pnp_2,\ldots,p_n, where pip_i is the direct parent of vertex i.

Output

Print the minimum number of seconds.

Subtasks

  • Subtask 1 (20 points): 2≤n≤92\le n\le9; 1≤pi≤n1\le p_i\le n; the input forms a tree rooted at 1.
  • Subtask 2 (30 points): 2≤n≤2⋅1052\le n\le2\cdot10^5; pi=1p_i=1 for every i≥2i\ge2.
  • Subtask 3 (50 points): 2≤n≤2⋅1052\le n\le2\cdot10^5; 1≤pi≤n1\le p_i\le n; the input forms a tree rooted at 1.

Examples

Input 1

7
1 1 1 2 2 4

Output 1

4

Explanation

Inject vertex 2 in second 1; spread to 3 and inject 5 in second 2; spread to 4 and 6 and inject 7 in second 3; inject root 1 in second 4. Four distinct groups require initial injections.

Input 2

6
1 1 1 1 1

Output 2

4

Explanation

Inject one leaf in second 1; spread to one leaf and inject another in each of seconds 2 and 3; inject the root in second 4. Three seconds are insufficient.