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

def main():
    N,M = map(int,input().split())
    
    edge = [[] for _ in range(N)]
    for _ in range(M):
        x,y = map(int,input().split())
        edge[x-1].append(y-1)
        
    dist = [-1 for _ in range(N)]
            
    def dfs(s):
        if dist[s] != -1:
            return dist[s]
        else:
            ret = 0
            for fol in edge[s]:
                ret = max(ret,dfs(fol)+1)
            dist[s] = ret
            return ret
    
    ans = 0    
    for i in range(N):
        ans = max(ans,dfs(i))
        
    print(ans)
        
if __name__ == "__main__":
    main()


Fixed submission:
import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 9)

def main():
    N,M = map(int,input().split())
    
    edge = [[] for _ in range(N)]
    for _ in range(M):
        x,y = map(int,input().split())
        edge[x-1].append(y-1)
        
    dist = [-1 for _ in range(N)]
            
    def dfs(s):
        if dist[s] != -1:
            return dist[s]
        else:
            ret = 0
            for fol in edge[s]:
                ret = max(ret,dfs(fol)+1)
            dist[s] = ret
            return ret

    for i in range(N):
        dfs(i)
        
    print(max(dist))
    
if __name__ == "__main__":
    main()

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

def main():
    N,M = map(int,input().split())
    
    edge = [[] for _ in range(N)]
    begin = [True for _ in range(N)]
    for _ in range(M):
        x,y = map(int,input().split())
        edge[x-1].append(y-1)
        begin[y-1] = False
            
    def longest(s):
        INF = 10**10
        l = [-INF for _ in range(N)]
        l[s] = 0
        q = deque([s])
        while q:
            now = q.popleft()
            for fol in edge[now]:
                if l[fol] < l[now]+1:
                    l[fol] = l[now]+1
                    q.append(fol)
        return max(l)
    
    ans = 0
    for i,b in enumerate(begin):
        if b:
            ans = max(ans,longest(i))
            
    print(ans)
        
if __name__ == "__main__":
    main()


Fixed submission:
import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 9)

def main():
    N,M = map(int,input().split())
    
    edge = [[] for _ in range(N)]
    for _ in range(M):
        x,y = map(int,input().split())
        edge[x-1].append(y-1)
        
    dist = [-1 for _ in range(N)]
            
    def dfs(s):
        if dist[s] != -1:
            return dist[s]
        else:
            ret = 0
            for fol in edge[s]:
                ret = max(ret,dfs(fol)+1)
            dist[s] = ret
            return ret

    for i in range(N):
        dfs(i)
        
    print(max(dist))
    
if __name__ == "__main__":
    main()

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

def main():
    N,M = map(int,input().split())
    
    edge = [[] for _ in range(N)]
    begin = [True for _ in range(N)]
    for _ in range(M):
        x,y = map(int,input().split())
        edge[x-1].append(y-1)
        begin[y-1] = False
            
    def longest(s):
        INF = 10**10
        l = [-INF for _ in range(N)]
        l[s] = 0
        q = deque([s])
        while q:
            now = q.popleft()
            for fol in edge[now]:
                if l[fol] < l[now]+1:
                    l[fol] = l[now]+1
                    q.append(fol)
        return max(l)
    
    ans = 0
    for i,b in enumerate(begin):
        if b:
            ans = max(ans,longest(i))
            
    print(ans)
        
if __name__ == "__main__":
    main()


Fixed submission:
import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 9)

def main():
    N,M = map(int,input().split())
    
    edge = [[] for _ in range(N)]
    for _ in range(M):
        x,y = map(int,input().split())
        edge[x-1].append(y-1)
        
    dist = [-1 for _ in range(N)]
            
    def dfs(s):
        if dist[s] != -1:
            return dist[s]
        else:
            ret = 0
            for fol in edge[s]:
                ret = max(ret,dfs(fol)+1)
            dist[s] = ret
            return ret

    for i in range(N):
        dfs(i)
        
    print(max(dist))
    
if __name__ == "__main__":
    main()