Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(2000000)
n = int(input())
a=[[]for i in range(n)]
for i in range(n-1):
  x = int(input())
  x-=1
  a[x].append(i+1)
  
dp=[0]*n
ya = [[0,i] for i in range(n)]
def dfs(x,y):
  ya[x][0]=y
  for i in a[x]:
    dfs(i,y+1)
dfs(0,0)
ya.sort(key=lambda x:-x[0])
tmp=[]
for i in range(n):
  bb=ya[i][1]
  for j in a[bb]:
    tmp.append(dp[j])
  tmp.sort(key=lambda x:-x)
  dai =0
  for j in range(len(tmp),0,-1):
    cc=tmp.pop()
    cc+=j
    dai=max(dai,cc)
  dp[bb]=dai
print(dp[0])

Fixed submission:
import sys
sys.setrecursionlimit(2000000)
n = int(input())
a=[[]for i in range(n)]
for i in range(n-1):
  x = int(input())
  x-=1
  a[x].append(i+1)
  
dp=[0]*n
ya = [[0,i] for i in range(n)]
def dfs(x,y):
  ya[x][0]=y
  for i in a[x]:
    dfs(i,y+1)
dfs(0,0)
ya.sort(key=lambda x:-x[0])
tmp=[]
for i in range(n):
  bb=ya[i][1]
  for j in a[bb]:
    tmp.append(dp[j])
  tmp.sort(key=lambda x:-x)
  dai =0
  for j in range(len(tmp),0,-1):
    cc=tmp.pop()
    cc+=j
    dai=max(dai,cc)
  dp[bb]=dai
print(dp[0])

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(2000000)
n = int(input())
a=[[]for i in range(n)]
for i in range(n-1):
  x = int(input())
  x-=1
  a[x].append(i+1)
  
dp=[0]*n
huka = [0]*n
def dfs(x,y):
  huka[x]=y
  for i in a[x]:
    dfs(i,y+1)
dfs(0,0)
ya = []
for i in range(n):
  ya.append([huka[i],i])
ya.sort(key=lambda x:-x[0])
for i in range(n):
  bb=ya[i][1]
  tmp=[]
  for j in a[bb]:
    tmp.append(dp[j])
  tmp.sort(key=lambda x:-x)
  dai =0
  for i in range(1,len(tmp)+1):
    tmp[i-1]+=i
    dai=max(dai,tmp[i-1])
  dp[bb]=dai
print(dp[0])

Fixed submission:
import sys
sys.setrecursionlimit(2000000)
n = int(input())
a=[[]for i in range(n)]
for i in range(n-1):
  x = int(input())
  x-=1
  a[x].append(i+1)
  
dp=[0]*n
ya = [[0,i] for i in range(n)]
def dfs(x,y):
  ya[x][0]=y
  for i in a[x]:
    dfs(i,y+1)
dfs(0,0)
ya.sort(key=lambda x:-x[0])
tmp=[]
for i in range(n):
  bb=ya[i][1]
  for j in a[bb]:
    tmp.append(dp[j])
  tmp.sort(key=lambda x:-x)
  dai =0
  for j in range(len(tmp),0,-1):
    cc=tmp.pop()
    cc+=j
    dai=max(dai,cc)
  dp[bb]=dai
print(dp[0])

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(2000000)
n = int(input())
a=[[]for i in range(n)]
for i in range(n-1):
  x = int(input())
  x-=1
  a[x].append(i+1)
  
dp=[0]*n
huka = [0]*n
def dfs(x,y):
  huka[x]=y
  for i in a[x]:
    dfs(i,y+1)
dfs(0,0)
ya = []
for i in range(n):
  ya.append([huka[i],i])
ya.sort()
ya=ya[::-1]
for i in range(n):
  bb=ya[i][1]
  tmp=[]
  for j in a[bb]:
    tmp.append(dp[j])
  tmp.sort()
  tmp=tmp[::-1]
  for i in range(1,len(tmp)+1):
    tmp[i-1]+=i
  if tmp==[]:
    tmp.append(0)
  dp[bb]=max(tmp)
print(dp[0])

Fixed submission:
import sys
sys.setrecursionlimit(2000000)
n = int(input())
a=[[]for i in range(n)]
for i in range(n-1):
  x = int(input())
  x-=1
  a[x].append(i+1)
  
dp=[0]*n
ya = [[0,i] for i in range(n)]
def dfs(x,y):
  ya[x][0]=y
  for i in a[x]:
    dfs(i,y+1)
dfs(0,0)
ya.sort(key=lambda x:-x[0])
tmp=[]
for i in range(n):
  bb=ya[i][1]
  for j in a[bb]:
    tmp.append(dp[j])
  tmp.sort(key=lambda x:-x)
  dai =0
  for j in range(len(tmp),0,-1):
    cc=tmp.pop()
    cc+=j
    dai=max(dai,cc)
  dp[bb]=dai
print(dp[0])

Buggy python submission (verdict: Runtime Error):
n = int(input())
a=[[]for i in range(n)]
for i in range(n-1):
  x = int(input())
  x-=1
  a[x].append(i+1)
  
dp=[0]*n
huka = [0]*n
def dfs(x,y):
  huka[x]=y
  for i in a[x]:
    dfs(i,y+1)
dfs(0,0)
ya = []
for i in range(n):
  ya.append([huka[i],i])
ya.sort()
ya=ya[::-1]
for i in range(n):
  bb=ya[i][1]
  tmp=[]
  for j in a[bb]:
    tmp.append(dp[j])
  tmp.sort()
  tmp=tmp[::-1]
  for i in range(1,len(tmp)+1):
    tmp[i-1]+=i
  if tmp==[]:
    tmp.append(0)
  dp[bb]=max(tmp)
print(dp[0])

Fixed submission:
import sys
sys.setrecursionlimit(2000000)
n = int(input())
a=[[]for i in range(n)]
for i in range(n-1):
  x = int(input())
  x-=1
  a[x].append(i+1)
  
dp=[0]*n
ya = [[0,i] for i in range(n)]
def dfs(x,y):
  ya[x][0]=y
  for i in a[x]:
    dfs(i,y+1)
dfs(0,0)
ya.sort(key=lambda x:-x[0])
tmp=[]
for i in range(n):
  bb=ya[i][1]
  for j in a[bb]:
    tmp.append(dp[j])
  tmp.sort(key=lambda x:-x)
  dai =0
  for j in range(len(tmp),0,-1):
    cc=tmp.pop()
    cc+=j
    dai=max(dai,cc)
  dp[bb]=dai
print(dp[0])