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

N,M=map(int,input().split())
xy = [list(map(int, input().split())) for _ in range(M)]
to = [[]*(N+1) for _ in range(N+1)]
for i in range(M):
    x,y=xy[i]
    to[x].append(y)

dp = [0]*(N+1)

def saiki(v):
    for u in to[v]:
        if dp[u] < dp[v]+1:
            dp[u]=dp[v]+1
            saiki(u)

count = [True]*(N+1)
count[0] = False
for i in range(M):
    count[xy[i][1]] = False

for i in range(1,N+1):
    if count[i]:
        saiki(i)
print(max(dp))


Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10000000)

N,M=map(int,input().split())

to = [[]*(N+1) for _ in range(N+1)]
for i in range(M):
    x,y=map(int,input().split())
    to[x].append(y)

dp = [-1]*(N+1)

def saiki(v):
    if dp[v]!=-1: return dp[v]
    res = 0
    for u in to[v]:
        res=max(res,saiki(u)+1)
    dp[v]=res
    return res

ans=0
for i in range(1,N+1):
    ans = max(ans,saiki(i))
print(max(dp))

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

N,M=map(int,input().split())
xy = [list(map(int, input().split())) for _ in range(M)]
to = [[]*(N+1) for _ in range(N+1)]
for i in range(M):
    x,y=xy[i]
    to[x].append(y)

dp = [0]*(N+1)

def saiki(v):
    for u in to[v]:
        if dp[u] < dp[v]+1:
            dp[u]=dp[v]+1
            saiki(u)

count = [True]*(N+1)
count[0] = False
for i in range(M):
    count[xy[i][1]] = False

for i in range(1,N+1):
    if count[i]:
        saiki(i)
print(max(dp))


Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10000000)

N,M=map(int,input().split())

to = [[]*(N+1) for _ in range(N+1)]
for i in range(M):
    x,y=map(int,input().split())
    to[x].append(y)

dp = [-1]*(N+1)

def saiki(v):
    if dp[v]!=-1: return dp[v]
    res = 0
    for u in to[v]:
        res=max(res,saiki(u)+1)
    dp[v]=res
    return res

ans=0
for i in range(1,N+1):
    ans = max(ans,saiki(i))
print(max(dp))

Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline

N,M=map(int,input().split())
xy = [list(map(int, input().split())) for _ in range(M)]
to = [[]*(N+1) for _ in range(N+1)]
for i in range(M):
    x,y=xy[i]
    to[x].append(y)

dp = [0]*(N+1)

def saiki(v):
    for u in to[v]:
        if dp[u] < dp[v]+1:
            dp[u]=dp[v]+1
            saiki(u)

count = [True]*(N+1)
count[0] = False
for i in range(M):
    count[xy[i][1]] = False

for i in range(1,N+1):
    if count[i]:
        saiki(i)
print(max(dp))


Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10000000)

N,M=map(int,input().split())

to = [[]*(N+1) for _ in range(N+1)]
for i in range(M):
    x,y=map(int,input().split())
    to[x].append(y)

dp = [-1]*(N+1)

def saiki(v):
    if dp[v]!=-1: return dp[v]
    res = 0
    for u in to[v]:
        res=max(res,saiki(u)+1)
    dp[v]=res
    return res

ans=0
for i in range(1,N+1):
    ans = max(ans,saiki(i))
print(max(dp))

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

N,M=map(int,input().split())
xy = [list(map(int, input().split())) for _ in range(M)]

dp = [0]*(N+1)
Flag=True
while Flag:
    Flag=False
    for i in range(M):
        if dp[xy[i][1]] < dp[xy[i][0]]+1:
            dp[xy[i][1]] = dp[xy[i][0]]+1
            Flag=True
print(max(dp))

Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10000000)

N,M=map(int,input().split())

to = [[]*(N+1) for _ in range(N+1)]
for i in range(M):
    x,y=map(int,input().split())
    to[x].append(y)

dp = [-1]*(N+1)

def saiki(v):
    if dp[v]!=-1: return dp[v]
    res = 0
    for u in to[v]:
        res=max(res,saiki(u)+1)
    dp[v]=res
    return res

ans=0
for i in range(1,N+1):
    ans = max(ans,saiki(i))
print(max(dp))