Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**9)
input=sys.stdin.readline
    
def g(to_li, from_li, dp, n):
    # nにつくパスの最大長
    if dp[n] != -1:
        return dp[n]
    if n == 0:
        return 0
    val = max([g(to_li, from_li, dp, t)+1 for t in to_li[n]])
    dp[n] = val
    return val
    

def main():
    N, M = [int(n) for n in input().split()]
    from_li = [[] for i in range(N+2)] # from[i]:=点iからつながった点
    to_li = [[] for i in range(N+2)] # to[i]:=点iにつながる点
    for i in range(M):
        x, y = [int(n) for n in input().split()]
        from_li[x].append(y)
        to_li[y].append(x)
    parents = []
    children = []
    dp = [-1]*(N+2)
    for index, to in enumerate(to_li):
        if len(to) == 0 and index != N+1 and index != 0:
            from_li[0].append(index)
            to_li[index].append(0)
    for index, fr in enumerate(from_li):
        if len(fr) == 0 and index != 0 and index != N+1:
            to_li[N+1].append(index)
            from_li[index].append(N+1)
    
    print(g(to_li, from_li, dp, N+1) - 2)




if __name__ == "__main__":
    main()

Fixed submission:
import sys
sys.setrecursionlimit(10**9)
def main():
    n, m = [int(n) for n in input().split()]
    paths = [[] for i in range(n+1)]
    for i in range(m):
        x, y = [int(r) for r in input().split()]
        paths[x].append(y)
    dp = [-1]*(n+1)

    ans = 0
    for i in range(n+1):
        ans = max(ans, dfs(paths, dp, i))
    print(ans-1)
def dfs(paths, dp, n):
    # nから下の最大長
    length = 0
    for p in paths[n]:
        val=0
        if dp[p] != -1:
            val = dp[p]
        else:
            val = dfs(paths, dp, p)
        length = max(length, val)
    length += 1 # pからnまでの一本もしくはnから終点までの一本
    dp[n] = length
    return length

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**9)

    
def g(to_li, from_li, dp, n):
    # nにつくパスの最大長
    if dp[n] != -1:
        return dp[n]
    if n == 0:
        return 0
    val = max([g(to_li, from_li, dp, t)+1 for t in to_li[n]])
    dp[n] = val
    return val
    

def main():
    N, M = [int(n) for n in input().split()]
    from_li = [[] for i in range(N+2)] # from[i]:=点iからつながった点
    to_li = [[] for i in range(N+2)] # to[i]:=点iにつながる点
    for i in range(M):
        x, y = [int(n) for n in input().split()]
        from_li[x].append(y)
        to_li[y].append(x)
    parents = []
    children = []
    dp = [-1]*(N+2)
    for index, to in enumerate(to_li):
        if len(to) == 0 and index != N+1 and index != 0:
            from_li[0].append(index)
            to_li[index].append(0)
    for index, fr in enumerate(from_li):
        if len(fr) == 0 and index != 0 and index != N+1:
            to_li[N+1].append(index)
            from_li[index].append(N+1)
    
    print(g(to_li, from_li, dp, N+1) - 2)




if __name__ == "__main__":
    main()

Fixed submission:
import sys
sys.setrecursionlimit(10**9)
def main():
    n, m = [int(n) for n in input().split()]
    paths = [[] for i in range(n+1)]
    for i in range(m):
        x, y = [int(r) for r in input().split()]
        paths[x].append(y)
    dp = [-1]*(n+1)

    ans = 0
    for i in range(n+1):
        ans = max(ans, dfs(paths, dp, i))
    print(ans-1)
def dfs(paths, dp, n):
    # nから下の最大長
    length = 0
    for p in paths[n]:
        val=0
        if dp[p] != -1:
            val = dp[p]
        else:
            val = dfs(paths, dp, p)
        length = max(length, val)
    length += 1 # pからnまでの一本もしくはnから終点までの一本
    dp[n] = length
    return length

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**9)

    
def g(to_li, from_li, dp, n):
    # nにつくパスの最大長
    if dp[n] != -1:
        return dp[n]
    if n == 0:
        return 0
    val = max([g(to_li, from_li, dp, t)+1 for t in to_li[n]])
    dp[n] = val
    return val
    

def main():
    N, M = [int(n) for n in input().split()]
    from_li = [[] for i in range(N+2)] # from[i]:=点iからつながった点
    to_li = [[] for i in range(N+2)] # to[i]:=点iにつながる点
    for i in range(M):
        x, y = [int(n) for n in input().split()]
        from_li[x].append(y)
        to_li[y].append(x)
    parents = []
    children = []
    dp = [-1]*(N+2)
    for index, to in enumerate(to_li):
        if index != N+1 and index != 0 and len(to) == 0:
            from_li[0].append(index)
            to_li[index].append(0)
    for index, fr in enumerate(from_li):
        if index != 0 and index != N+1 and len(fr) == 0:
            to_li[N+1].append(index)
            from_li[index].append(N+1)
    
    print(g(to_li, from_li, dp, N+1) - 2)




if __name__ == "__main__":
    main()

Fixed submission:
import sys
sys.setrecursionlimit(10**9)
def main():
    n, m = [int(n) for n in input().split()]
    paths = [[] for i in range(n+1)]
    for i in range(m):
        x, y = [int(r) for r in input().split()]
        paths[x].append(y)
    dp = [-1]*(n+1)

    ans = 0
    for i in range(n+1):
        ans = max(ans, dfs(paths, dp, i))
    print(ans-1)
def dfs(paths, dp, n):
    # nから下の最大長
    length = 0
    for p in paths[n]:
        val=0
        if dp[p] != -1:
            val = dp[p]
        else:
            val = dfs(paths, dp, p)
        length = max(length, val)
    length += 1 # pからnまでの一本もしくはnから終点までの一本
    dp[n] = length
    return length

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**6)
def main():
    N, M = [int(n) for n in input().split()]
    from_li = [[] for i in range(N)] # from[i]:=点iからつながった点
    to_li = [[] for i in range(N)] # to[i]:=点iにつながる点
    for i in range(M):
        x, y = [int(n) for n in input().split()]
        from_li[x-1].append(y-1)
        to_li[y-1].append(x-1)
    parents = []
    children = []
    dp = [-1]*N
    for index, to in enumerate(to_li):
        if len(to) == 0:
            parents.append(index)
    for index, fr in enumerate(from_li):
        if(len(fr)) == 0:
            children.append(index)
    print(max([g(to_li, from_li, parents, dp, c) for c in children]))

    
def g(to_li, from_li, parents, dp, n):
    # nにつくパスの最大長
    if dp[n] != -1:
        return dp[n]
    if n in parents:
        return 0
    val = max([g(to_li, from_li, parents, dp, t)+1 for t in to_li[n]])
    dp[n] = val
    return val
    


if __name__ == "__main__":
    main()

Fixed submission:
import sys
sys.setrecursionlimit(10**9)
def main():
    n, m = [int(n) for n in input().split()]
    paths = [[] for i in range(n+1)]
    for i in range(m):
        x, y = [int(r) for r in input().split()]
        paths[x].append(y)
    dp = [-1]*(n+1)

    ans = 0
    for i in range(n+1):
        ans = max(ans, dfs(paths, dp, i))
    print(ans-1)
def dfs(paths, dp, n):
    # nから下の最大長
    length = 0
    for p in paths[n]:
        val=0
        if dp[p] != -1:
            val = dp[p]
        else:
            val = dfs(paths, dp, p)
        length = max(length, val)
    length += 1 # pからnまでの一本もしくはnから終点までの一本
    dp[n] = length
    return length

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Runtime Error):
def main():
    N, M = [int(n) for n in input().split()]
    from_li = [[] for i in range(N)] # from[i]:=点iからつながった点
    to_li = [[] for i in range(N)] # to[i]:=点iにつながる点
    for i in range(M):
        x, y = [int(n) for n in input().split()]
        from_li[x-1].append(y-1)
        to_li[y-1].append(x-1)
    parents = []
    children = []
    dp = [-1]*N
    for index, to in enumerate(to_li):
        if len(to) == 0:
            parents.append(index)
    for index, fr in enumerate(from_li):
        if(len(fr)) == 0:
            children.append(index)
    print(max([g(to_li, from_li, parents, dp, c) for c in children]))

    
def g(to_li, from_li, parents, dp, n):
    # nにつくパスの最大長
    if dp[n] != -1:
        return dp[n]
    if n in parents:
        return 0
    val = max([g(to_li, from_li, parents, dp, t)+1 for t in to_li[n]])
    dp[n] = val
    return val
    


if __name__ == "__main__":
    main()

Fixed submission:
import sys
sys.setrecursionlimit(10**9)
def main():
    n, m = [int(n) for n in input().split()]
    paths = [[] for i in range(n+1)]
    for i in range(m):
        x, y = [int(r) for r in input().split()]
        paths[x].append(y)
    dp = [-1]*(n+1)

    ans = 0
    for i in range(n+1):
        ans = max(ans, dfs(paths, dp, i))
    print(ans-1)
def dfs(paths, dp, n):
    # nから下の最大長
    length = 0
    for p in paths[n]:
        val=0
        if dp[p] != -1:
            val = dp[p]
        else:
            val = dfs(paths, dp, p)
        length = max(length, val)
    length += 1 # pからnまでの一本もしくはnから終点までの一本
    dp[n] = length
    return length

if __name__ == "__main__":
    main()