Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)

n, m = [int(x) for x in input().split()]
g = [[] for _ in range(n)]
for _ in range(m):
    x, y = [int(x) for x in input().split()]
    g[x - 1].append(y - 1)

memo = [-1]*n

def dp(v):
    if memo[v] >= 0:
        return memo[v]
    else:
        res = 0
        for u in g[v]:
            res = max(res, dp(u) + 1)
        return res

for i in range(n):
    memo[i] = dp(i)
print(max(memo))




Fixed submission:
import sys
input = sys.stdin.readline
n, m = [int(x) for x in input().split()]
g = [[] for _ in range(n)]
for _ in range(m):
    x, y = [int(x) for x in input().split()]
    g[x - 1].append(y - 1)
from functools import lru_cache
sys.setrecursionlimit(10**7)
@lru_cache(maxsize=None)
def dp(v):
    res = 0
    for u in g[v]:
        res = max(res, dp(u) + 1)
    return res
ans = -1
for i in range(n):
    ans = max(ans, dp(i))
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)

n, m = [int(x) for x in input().split()]
g = [[] for _ in range(n)]
for _ in range(m):
    x, y = [int(x) for x in input().split()]
    g[x - 1].append(y - 1)

memo = [-1]*n

def dp(v):
    if memo[v] >= 0:
        return memo[v]
    else:
        res = 0
        for u in g[v]:
            res = max(res, dp(u) + 1)
        return res

for i in range(n):
    memo[i] = dp(i)
print(max(memo))




Fixed submission:
import sys
input = sys.stdin.readline
n, m = [int(x) for x in input().split()]
g = [[] for _ in range(n)]
for _ in range(m):
    x, y = [int(x) for x in input().split()]
    g[x - 1].append(y - 1)
from functools import lru_cache
sys.setrecursionlimit(10**7)
@lru_cache(maxsize=None)
def dp(v):
    res = 0
    for u in g[v]:
        res = max(res, dp(u) + 1)
    return res
ans = -1
for i in range(n):
    ans = max(ans, dp(i))
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)

n, m = [int(x) for x in input().split()]
g = [[] for _ in range(n)]
for _ in range(m):
    x, y = [int(x) for x in input().split()]
    g[x - 1].append(y - 1)

memo = [-1]*n

def dp(v):
    if memo[v] >= 0:
        return memo[v]
    else:
        res = 0
        for u in g[v]:
            res = max(res, dp(u) + 1)
        return res
ans = -1
for i in range(n):
    ans = max(ans, dp(i))
print(ans)




Fixed submission:
import sys
input = sys.stdin.readline
n, m = [int(x) for x in input().split()]
g = [[] for _ in range(n)]
for _ in range(m):
    x, y = [int(x) for x in input().split()]
    g[x - 1].append(y - 1)
from functools import lru_cache
sys.setrecursionlimit(10**7)
@lru_cache(maxsize=None)
def dp(v):
    res = 0
    for u in g[v]:
        res = max(res, dp(u) + 1)
    return res
ans = -1
for i in range(n):
    ans = max(ans, dp(i))
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)

n, m = [int(x) for x in input().split()]
g = [[] for _ in range(n)]
for _ in range(m):
    x, y = [int(x) for x in input().split()]
    g[x - 1].append(y - 1)

memo = [-1]*n

def dp(v):
    if memo[v] >= 0:
        return memo[v]
    else:
        res = 0
        for u in g[v]:
            res = max(res, dp(u) + 1)
        return res
ans = -1
for i in range(n):
    ans = max(ans, dp(i))
print(ans)




Fixed submission:
import sys
input = sys.stdin.readline
n, m = [int(x) for x in input().split()]
g = [[] for _ in range(n)]
for _ in range(m):
    x, y = [int(x) for x in input().split()]
    g[x - 1].append(y - 1)
from functools import lru_cache
sys.setrecursionlimit(10**7)
@lru_cache(maxsize=None)
def dp(v):
    res = 0
    for u in g[v]:
        res = max(res, dp(u) + 1)
    return res
ans = -1
for i in range(n):
    ans = max(ans, dp(i))
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n, m = [int(x) for x in input().split()]
g = [[]*n for _ in range(n)]
visited = set()
for _ in range(m):
    x, y = [int(x) for x in input().split()]
    g[x - 1].append(y - 1)
dp = [0] * n
from collections import deque
def dfs(u):
    queue = deque([u])
    d = [None]*n
    max_d = 0
    d[u] = 0
    while queue:
        v = queue.popleft()
        for i in g[v]:
            if d[i] is None:
                d[i] = d[v] + 1
                max_d = max(max_d, d[i])
                queue.append(i)
                visited.add(i)
            elif d[v] + 1 >= d[i]:
                d[i] = max(d[i], d[v] + 1)
                max_d = max(max_d, d[i])
                queue.append(i)
    return max_d

for i in range(n):
    if i in visited:
        continue
    dp[i] = dfs(i)

print(max(dp))




Fixed submission:
import sys
input = sys.stdin.readline
n, m = [int(x) for x in input().split()]
g = [[] for _ in range(n)]
for _ in range(m):
    x, y = [int(x) for x in input().split()]
    g[x - 1].append(y - 1)
from functools import lru_cache
sys.setrecursionlimit(10**7)
@lru_cache(maxsize=None)
def dp(v):
    res = 0
    for u in g[v]:
        res = max(res, dp(u) + 1)
    return res
ans = -1
for i in range(n):
    ans = max(ans, dp(i))
print(ans)