Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**5)
from collections import defaultdict

n = int(input())
e = [[] for i in range(n)]
for i in range(1,n):
    x = int(input())
    e[x-1].append(i)
num = {}
def dfs(x):
    if x in num:
        return num[x]
    count = len(e[x])
    if count == 0:
        return 0
    d = defaultdict(int)
    ma = 0
    for nex in e[x]:
        v = dfs(nex)
        d[v] += 1
        ma = max(ma,v)
    now = count
    bef = 0
    for v in range(ma,-1,-1):
        if d[v] > 0:
            bef += d[v]
            now = max(now,bef+v)
    num[x] = now
    return now

print(dfs(0))

Fixed submission:
import sys
sys.setrecursionlimit(10**5)
from collections import defaultdict

n = int(input())
e = [[] for i in range(n)]
for i in range(1,n):
    x = int(input())
    e[x-1].append(i)

def dfs(x):

    count = len(e[x])
    if count == 0:
        return 0
    d = defaultdict(int)
    ma = 0
    s = set()
    for nex in e[x]:
        v = dfs(nex)
        d[v] += 1
        s.add(v)
        ma = max(ma,v)
    now = count
    bef = 0
    for v in sorted(list(s),reverse=True):
        bef += d[v]
        now = max(now,bef+v)
    return now

print(dfs(0))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**5)
from collections import defaultdict

n = int(input())
e = [[] for i in range(n)]
for i in range(1,n):
    x = int(input())
    e[x-1].append(i)

def dfs(x):
    count = len(e[x])
    if count == 0:
        return 0
    d = defaultdict(int)
    ma = 0
    for nex in e[x]:
        v = dfs(nex)
        d[v] += 1
        ma = max(ma,v)
    now = count
    bef = 0
    for v in range(ma,-1,-1):
        if d[v] > 0:
            bef += d[v]
            now = max(now,bef+v)
    return now

print(dfs(0))

Fixed submission:
import sys
sys.setrecursionlimit(10**5)
from collections import defaultdict

n = int(input())
e = [[] for i in range(n)]
for i in range(1,n):
    x = int(input())
    e[x-1].append(i)

def dfs(x):

    count = len(e[x])
    if count == 0:
        return 0
    d = defaultdict(int)
    ma = 0
    s = set()
    for nex in e[x]:
        v = dfs(nex)
        d[v] += 1
        s.add(v)
        ma = max(ma,v)
    now = count
    bef = 0
    for v in sorted(list(s),reverse=True):
        bef += d[v]
        now = max(now,bef+v)
    return now

print(dfs(0))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**5)
from collections import defaultdict

n = int(input())
e = [[] for i in range(n)]
for i in range(1,n):
    x = int(input())
    e[x-1].append(i)

def dfs(x):
    count = len(e[x])
    if count == 0:
        return 0
    d = defaultdict(int)
    ma = 0
    for nex in e[x]:
        v = dfs(nex)
        d[v] += 1
        ma = max(ma,v)
    now = count
    bef = 0
    for v in range(ma,-1,-1):
        if d[v] > 0:
            bef += d[v]
            now = max(now,bef+v)
    return now

print(dfs(0))

Fixed submission:
import sys
sys.setrecursionlimit(10**5)
from collections import defaultdict

n = int(input())
e = [[] for i in range(n)]
for i in range(1,n):
    x = int(input())
    e[x-1].append(i)

def dfs(x):

    count = len(e[x])
    if count == 0:
        return 0
    d = defaultdict(int)
    ma = 0
    s = set()
    for nex in e[x]:
        v = dfs(nex)
        d[v] += 1
        s.add(v)
        ma = max(ma,v)
    now = count
    bef = 0
    for v in sorted(list(s),reverse=True):
        bef += d[v]
        now = max(now,bef+v)
    return now

print(dfs(0))

Buggy python submission (verdict: Runtime Error):
n = int(input())
e = [[] for i in range(n)]
for i in range(1,n):
    x = int(input())
    e[x-1].append(i)

def dfs(x):
    count = len(e[x])
    if count == 0:
        return 0
    now = 0
    for nex in e[x]:
        now = max(now,dfs(nex))    
    return max(count,now+1)

print(dfs(0))

Fixed submission:
import sys
sys.setrecursionlimit(10**5)
from collections import defaultdict

n = int(input())
e = [[] for i in range(n)]
for i in range(1,n):
    x = int(input())
    e[x-1].append(i)

def dfs(x):

    count = len(e[x])
    if count == 0:
        return 0
    d = defaultdict(int)
    ma = 0
    s = set()
    for nex in e[x]:
        v = dfs(nex)
        d[v] += 1
        s.add(v)
        ma = max(ma,v)
    now = count
    bef = 0
    for v in sorted(list(s),reverse=True):
        bef += d[v]
        now = max(now,bef+v)
    return now

print(dfs(0))