#BS0000336. Lây nhiễm trên cây (Tree Infection)
Bài tập thuộc lộ trình
Mục lục đề ⌄
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 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: . Second line: integers , where is the direct parent of vertex i.
Output
Print the minimum number of seconds.
Subtasks
- Subtask 1 (20 points): ; ; the input forms a tree rooted at 1.
- Subtask 2 (30 points): ; for every .
- Subtask 3 (50 points): ; ; 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.