Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
a=[int(input()) for _ in range(n-1)]
ki=[[] for _ in range(n)]
for i,x in enumerate(a):
  ki[x-1].append(i+1)

ans=0
import sys
sys.setrecursionlimit(10**7)
def marge(arg):
  ret=0
  while True:
    flg=True
    endflg=True
    for i in range(len(arg)):
      if arg[i]>1:
        arg[i]-=1
        endflg=False
      elif arg[i]==1 and flg:
        arg[i]-=1
        endflg=False
        flg=False
      else:
        pass
    if endflg:
      return ret
    ret+=1

def dfs(v):
  ret=[]
  if len(ki[v])==0:return 1
  for nv in ki[v]:
    ret.append(dfs(nv))
  return marge(ret)+1
print(dfs(0)-1)


Fixed submission:
n=int(input())
a=[int(input()) for _ in range(n-1)]
ki=[[] for _ in range(n)]
for i,x in enumerate(a):
  ki[x-1].append(i+1)

ans=0
import sys
sys.setrecursionlimit(10**7)
from collections import Counter
def marge(arg):
  ret=0
  arg.sort(reverse=True)
  for i,x in enumerate(arg):
    ret=max(ret,i+1+x)
  return ret


def dfs(v):
  ret=[]
  if len(ki[v])==0:return 0
  for nv in ki[v]:
    ret.append(dfs(nv))
  return marge(ret)
print(dfs(0))

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
a=[int(input()) for _ in range(n-1)]
ki=[[] for _ in range(n)]
for i,x in enumerate(a):
  ki[x-1].append(i+1)

ans=0
import sys
sys.setrecursionlimit(10**7)
from collections import Counter
def marge(arg):
  ret=0
  ca=dict(Counter(arg))
  m=max(ca.keys())
  for i in range(1,m+1):
    if i not in ca:
      ca[i]=0
  while True:
    for i in range(1,m+1):
      if i==1 and i in ca and ca[i]:
        ca[i]-=1
      elif i>1:
        if i-1 in ca:
          ca[i-1]+=ca[i]
        else:
          ca[i-1]=ca[i]
        ca[i]=0
        if i==m:m-=1
    ret+=1
    if i==1:
      return ret+ca[1]

def dfs(v):
  ret=[]
  if len(ki[v])==0:return 1
  for nv in ki[v]:
    ret.append(dfs(nv))
  return marge(ret)+1
print(dfs(0)-1)


Fixed submission:
n=int(input())
a=[int(input()) for _ in range(n-1)]
ki=[[] for _ in range(n)]
for i,x in enumerate(a):
  ki[x-1].append(i+1)

ans=0
import sys
sys.setrecursionlimit(10**7)
from collections import Counter
def marge(arg):
  ret=0
  arg.sort(reverse=True)
  for i,x in enumerate(arg):
    ret=max(ret,i+1+x)
  return ret


def dfs(v):
  ret=[]
  if len(ki[v])==0:return 0
  for nv in ki[v]:
    ret.append(dfs(nv))
  return marge(ret)
print(dfs(0))

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
a=[int(input()) for _ in range(n-1)]
ki=[[] for _ in range(n)]
for i,x in enumerate(a):
  ki[x-1].append(i+1)

ans=0
import sys
sys.setrecursionlimit(10**7)
def marge(arg):
  ret=0
  while True:
    flg=True
    endflg=True
    for i in range(len(arg)):
      if arg[i]>1:
        arg[i]-=1
        endflg=False
      elif arg[i]==1 and flg:
        arg[i]-=1
        endflg=False
        flg=False
      else:
        pass
    if endflg:
      return ret
    ret+=1

def dfs(v):
  ret=[]
  if len(ki[v])==0:return 1
  for nv in ki[v]:
    ret.append(dfs(nv))
  return marge(ret)+1
print(dfs(0)-1)


Fixed submission:
n=int(input())
a=[int(input()) for _ in range(n-1)]
ki=[[] for _ in range(n)]
for i,x in enumerate(a):
  ki[x-1].append(i+1)

ans=0
import sys
sys.setrecursionlimit(10**7)
from collections import Counter
def marge(arg):
  ret=0
  arg.sort(reverse=True)
  for i,x in enumerate(arg):
    ret=max(ret,i+1+x)
  return ret


def dfs(v):
  ret=[]
  if len(ki[v])==0:return 0
  for nv in ki[v]:
    ret.append(dfs(nv))
  return marge(ret)
print(dfs(0))