-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1135.py
38 lines (29 loc) · 901 Bytes
/
1135.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import sys
def input():
return sys.stdin.readline().rstrip()
def main():
def make_dp(curr_node: int):
if len(child_list[curr_node]) == 0:
dp[curr_node] = 0
return
child_dp_list = []
for child_node in child_list[curr_node]:
make_dp(child_node)
child_dp_list.append(dp[child_node])
child_dp_list.sort(reverse=True)
INF = 0
min_time = INF
for dir_time, sub_time in enumerate(child_dp_list, start=1):
min_time = max(min_time, dir_time + sub_time)
dp[curr_node] = min_time
n = int(input())
parent_list = list(map(int, input().split()))
child_list = [[] for node in range(n)]
for i in range(1, n):
child_list[parent_list[i]].append(i)
dp = [-1] * n
ROOT = 0
make_dp(ROOT)
print(dp[ROOT])
if __name__ == '__main__':
main()