Buggy python submission (verdict: Runtime Error):
def g():
    from collections import defaultdict
    N, M = map(int, input().split())
    xy = defaultdict(list)
    memo = defaultdict(list)
    for _ in range(M):
        x, y = map(int, input().split())
        xy[x].append(y)

    def g2(x):
        if x in memo:
            return memo[x]
        else:
            r = 0
            for ny in xy[x]:
                r = max(r, g2(ny) + 1)
            memo[x] = r
        return r

    ans = 0
    for i in range(N):
        ans = max(ans, g2(i))
    return ans


if __name__ == '__main__':
    ans = g()
    print(ans)

Fixed submission:
def g():
    from collections import defaultdict
    import sys

    sys.setrecursionlimit(10 ** 6)
    N, M = map(int, input().split())
    xy = defaultdict(list)
    memo = {}
    for _ in range(M):
        x, y = map(int, input().split())
        xy[x].append(y)

    def g2(x):
        if x in memo:
            return memo[x]
        else:
            r = 0
            for ny in xy[x]:
                r = max(r, g2(ny) + 1)
            memo[x] = r
        return r

    ans = 0
    for i in range(1, N + 1):
        ans = max(ans, g2(i))
    return ans


if __name__ == '__main__':
    ans = g()
    print(ans)

Buggy python submission (verdict: Runtime Error):
def g():
    from collections import defaultdict
    N, M = map(int, input().split())
    xy = defaultdict(list)
    memo = defaultdict(list)
    for _ in range(M):
        x, y = map(int, input().split())
        xy[x].append(y)

    def g2(x):
        if x in memo:
            return memo[x]
        else:
            r = 0
            for ny in xy[x]:
                r = max(r, g2(ny) + 1)
            memo[x] = r
        return r

    ans = [g2(i) for i in range(1, N + 1)]
    return max(ans)


if __name__ == '__main__':
    ans = g()
    print(ans)

Fixed submission:
def g():
    from collections import defaultdict
    import sys

    sys.setrecursionlimit(10 ** 6)
    N, M = map(int, input().split())
    xy = defaultdict(list)
    memo = {}
    for _ in range(M):
        x, y = map(int, input().split())
        xy[x].append(y)

    def g2(x):
        if x in memo:
            return memo[x]
        else:
            r = 0
            for ny in xy[x]:
                r = max(r, g2(ny) + 1)
            memo[x] = r
        return r

    ans = 0
    for i in range(1, N + 1):
        ans = max(ans, g2(i))
    return ans


if __name__ == '__main__':
    ans = g()
    print(ans)

Buggy python submission (verdict: Runtime Error):
def g():
    from collections import defaultdict
    N, M = map(int, input().split())
    xy = defaultdict(list)
    for _ in range(M):
        x, y = map(int, input().split())
        xy[x].append(y)
    memo = {}

    def g2(x):
        if x in memo:
            return memo[x]
        else:
            r = 0
            for ny in xy[x]:
                r = max(r, g2(ny) + 1)
            memo[x] = r
        return r

    ans = [g2(i) for i in range(1, N + 1)]
    return max(ans)


if __name__ == '__main__':
    ans = g()
    print(ans)

Fixed submission:
def g():
    from collections import defaultdict
    import sys

    sys.setrecursionlimit(10 ** 6)
    N, M = map(int, input().split())
    xy = defaultdict(list)
    memo = {}
    for _ in range(M):
        x, y = map(int, input().split())
        xy[x].append(y)

    def g2(x):
        if x in memo:
            return memo[x]
        else:
            r = 0
            for ny in xy[x]:
                r = max(r, g2(ny) + 1)
            memo[x] = r
        return r

    ans = 0
    for i in range(1, N + 1):
        ans = max(ans, g2(i))
    return ans


if __name__ == '__main__':
    ans = g()
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
def g():
    N, M = map(int, input().split())
    xy = [list(map(int, input().split())) for _ in range(M)]
    memo = {}

    def g2(x):
        if x in memo:
            return memo[x]
        else:
            r = 0
            for xx, yy in xy:
                if x == xx:
                    r = max(r, g2(yy) + 1)
            memo[x] = r
        return r

    ans = [g2(i) for i in range(1, N + 1)]
    return max(ans)


if __name__ == '__main__':
    ans = g()
    print(ans)

Fixed submission:
def g():
    from collections import defaultdict
    import sys

    sys.setrecursionlimit(10 ** 6)
    N, M = map(int, input().split())
    xy = defaultdict(list)
    memo = {}
    for _ in range(M):
        x, y = map(int, input().split())
        xy[x].append(y)

    def g2(x):
        if x in memo:
            return memo[x]
        else:
            r = 0
            for ny in xy[x]:
                r = max(r, g2(ny) + 1)
            memo[x] = r
        return r

    ans = 0
    for i in range(1, N + 1):
        ans = max(ans, g2(i))
    return ans


if __name__ == '__main__':
    ans = g()
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
def g():
    N, M = map(int, input().split())
    xy = [list(map(int, input().split())) for _ in range(M)]
    memo = {}

    def g2(x):
        if x in memo:
            return memo[x]
        else:
            r = 0
            for xx, yy in xy:
                if x == xx:
                    r = max(r, g2(yy) + 1)
            memo[x] = r
        return r

    ans = [g2(i) for i in range(1, N + 1)]
    return max(ans)


if __name__ == '__main__':
    ans = g()
    print(ans)

Fixed submission:
def g():
    from collections import defaultdict
    import sys

    sys.setrecursionlimit(10 ** 6)
    N, M = map(int, input().split())
    xy = defaultdict(list)
    memo = {}
    for _ in range(M):
        x, y = map(int, input().split())
        xy[x].append(y)

    def g2(x):
        if x in memo:
            return memo[x]
        else:
            r = 0
            for ny in xy[x]:
                r = max(r, g2(ny) + 1)
            memo[x] = r
        return r

    ans = 0
    for i in range(1, N + 1):
        ans = max(ans, g2(i))
    return ans


if __name__ == '__main__':
    ans = g()
    print(ans)