Buggy python submission (verdict: Runtime Error):
import collections
n, m = [int(i) for i in input().split()]
todc = collections.defaultdict(lambda: [])
lendc = collections.defaultdict(lambda: -1)

for k in range(m):
    i, j = input().split()
    todc[int(j)] += [int(i)]

def maxlen(n):
    if len(todc[n]) == 0:
        lendc[n] = 0
        return 0
    else:
        m = 0
        for i in todc[n]:
            if lendc[i] == -1:
                lendc[i] = maxlen(i)
            m = max(m, lendc[i]+1)
        return m

k = 0
for i in range(1, n+1):
    if lendc[i] == -1:
        lendc[i] = maxlen(i)
    k = max(k, lendc[i])
print(k)

Fixed submission:
import collections
n, m = [int(i) for i in input().split()]
todc = collections.defaultdict(lambda: [])
lendc = collections.defaultdict(lambda: -1)


for k in range(m):
    i, j = [int(n) for n in input().split()]
    todc[j] += [i]


def maxlen(n):
    if len(todc[n]) == 0:
        lendc[n] = 0
        return 0
    else:
        m = 0
        for i in todc[n]:
            if lendc[i] == -1:
                lendc[i] = maxlen(i)
            m = max(m, lendc[i]+1)
        return m
       

def maxlen_stack(org_n):
    stack = [org_n]
    while stack:
        n = stack.pop()
        if len(todc[n]) == 0:
            lendc[n] = 0
        else:
            m = 0
            recursive = []
            for i in todc[n]:
                if lendc[i] == -1:
                    recursive.append(i)
                else:
                    m = max(m, lendc[i]+1)
            if recursive:
                stack += [n] + recursive
            else:
                lendc[n] = m         
    return lendc[org_n]

k = 0
for i in range(1, n+1):
    if lendc[i] == -1:
        #lendc[i] = maxlen(i)
        lendc[i] = maxlen_stack(i)
    k = max(k, lendc[i])
print(k)

Buggy python submission (verdict: Runtime Error):
import collections
n, m = [int(i) for i in input().split()]
todc = collections.defaultdict(lambda: [])

for k in range(m):
    i, j = input().split()
    todc[int(j)] += [int(i)]

def maxlen(n):
    if len(todc[n]) == 0:
        return 0
    else:
        m = 0
        for i in todc[n]:
            m = max(m, (maxlen(i)+1))
        return m

k = 0
for i in range(1, n+1):
    k = max(k, maxlen(i))
print(k)


Fixed submission:
import collections
n, m = [int(i) for i in input().split()]
todc = collections.defaultdict(lambda: [])
lendc = collections.defaultdict(lambda: -1)


for k in range(m):
    i, j = [int(n) for n in input().split()]
    todc[j] += [i]


def maxlen(n):
    if len(todc[n]) == 0:
        lendc[n] = 0
        return 0
    else:
        m = 0
        for i in todc[n]:
            if lendc[i] == -1:
                lendc[i] = maxlen(i)
            m = max(m, lendc[i]+1)
        return m
       

def maxlen_stack(org_n):
    stack = [org_n]
    while stack:
        n = stack.pop()
        if len(todc[n]) == 0:
            lendc[n] = 0
        else:
            m = 0
            recursive = []
            for i in todc[n]:
                if lendc[i] == -1:
                    recursive.append(i)
                else:
                    m = max(m, lendc[i]+1)
            if recursive:
                stack += [n] + recursive
            else:
                lendc[n] = m         
    return lendc[org_n]

k = 0
for i in range(1, n+1):
    if lendc[i] == -1:
        #lendc[i] = maxlen(i)
        lendc[i] = maxlen_stack(i)
    k = max(k, lendc[i])
print(k)

Buggy python submission (verdict: Runtime Error):
import collections
n, m = [int(i) for i in input().split()]
todc = collections.defaultdict(lambda: [])

for k in range(m):
    i, j = input().split()
    todc[int(j)] += [int(i)]

def maxlen(n):
    if len(todc[n]) == 0:
        return 0
    else:
        m = 0
        for i in todc[n]:
            m = max(m, (maxlen(i)+1))
        return m

k = 0
for i in range(1, n+1):
    k = max(k, maxlen(i))
    print("k ", i, maxlen(i))
print(k)

Fixed submission:
import collections
n, m = [int(i) for i in input().split()]
todc = collections.defaultdict(lambda: [])
lendc = collections.defaultdict(lambda: -1)


for k in range(m):
    i, j = [int(n) for n in input().split()]
    todc[j] += [i]


def maxlen(n):
    if len(todc[n]) == 0:
        lendc[n] = 0
        return 0
    else:
        m = 0
        for i in todc[n]:
            if lendc[i] == -1:
                lendc[i] = maxlen(i)
            m = max(m, lendc[i]+1)
        return m
       

def maxlen_stack(org_n):
    stack = [org_n]
    while stack:
        n = stack.pop()
        if len(todc[n]) == 0:
            lendc[n] = 0
        else:
            m = 0
            recursive = []
            for i in todc[n]:
                if lendc[i] == -1:
                    recursive.append(i)
                else:
                    m = max(m, lendc[i]+1)
            if recursive:
                stack += [n] + recursive
            else:
                lendc[n] = m         
    return lendc[org_n]

k = 0
for i in range(1, n+1):
    if lendc[i] == -1:
        #lendc[i] = maxlen(i)
        lendc[i] = maxlen_stack(i)
    k = max(k, lendc[i])
print(k)