Buggy python submission (verdict: Time Limit Exceeded):
int1 = lambda x: int(x) - 1
import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(1000000000)

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

start_point = set(range(N))
to_point = [[] for _ in range(N)]

for _ in range(M):
    x, y = map(int1, input().split())
    start_point = start_point - {y}
    to_point[x].append(y)

dp_memo = [-1]*N
for x in range(N):
    if len(to_point[x]) == 0:
        dp_memo[x] = 0

# 点i始点のLongest Pathの長さ
def dp(i):
    if dp_memo[i] >= 0:
        return dp_memo[i]
    lp = 0
    for j in to_point[i]:
        lp = max(lp, dp(j)+1)
    dp_memo[i] = lp
    return lp

for i in range(N):
    dp(i)

print(max(dp_memo))

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

int1 = lambda x: int(x) - 1

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

to_point = [[] for _ in range(N)]

for _ in range(M):
    x, y = map(int1, input().split())
    to_point[x].append(y)

dp_memo = [-1]*N
for x in range(N):
    if len(to_point[x]) == 0:
        dp_memo[x] = 0

# 点i始点のLongest Pathの長さ
def dp(i):
    if dp_memo[i] >= 0:
        return dp_memo[i]
    lp = 0
    for j in to_point[i]:
        lp = max(lp, dp(j)+1)
    dp_memo[i] = lp
    return lp

for i in range(N):
    dp(i)

print(max(dp_memo))

Buggy python submission (verdict: Time Limit Exceeded):
int1 = lambda x: int(x) - 1
import sys
sys.setrecursionlimit(1000000000)

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

start_point = set(range(N))
to_point = [[] for _ in range(N)]

for _ in range(M):
    x, y = map(int1, input().split())
    start_point = start_point - {y}
    to_point[x].append(y)

dp_memo = [-1]*N
for x in range(N):
    if len(to_point[x]) == 0:
        dp_memo[x] = 0

# 点i始点のLongest Pathの長さ
def dp(i):
    if dp_memo[i] >= 0:
        return dp_memo[i]
    lp = 0
    for j in to_point[i]:
        lp = max(lp, dp(j)+1)
    dp_memo[i] = lp
    return lp

for i in range(N):
    dp(i)

print(max(dp_memo))

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

int1 = lambda x: int(x) - 1

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

to_point = [[] for _ in range(N)]

for _ in range(M):
    x, y = map(int1, input().split())
    to_point[x].append(y)

dp_memo = [-1]*N
for x in range(N):
    if len(to_point[x]) == 0:
        dp_memo[x] = 0

# 点i始点のLongest Pathの長さ
def dp(i):
    if dp_memo[i] >= 0:
        return dp_memo[i]
    lp = 0
    for j in to_point[i]:
        lp = max(lp, dp(j)+1)
    dp_memo[i] = lp
    return lp

for i in range(N):
    dp(i)

print(max(dp_memo))

Buggy python submission (verdict: Time Limit Exceeded):
int1 = lambda x: int(x) - 1
import sys
sys.setrecursionlimit(1000000000)

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

start_point = set(range(N))
to_point = [[] for _ in range(N)]

for _ in range(M):
    x, y = map(int1, input().split())
    start_point = start_point - {y}
    to_point[x].append(y)

dp_memo = [-1]*N
for x in range(N):
    if len(to_point[x]) == 0:
        dp_memo[x] = 0

# 点i始点のLongest Pathの長さ
def dp(i):
    if dp_memo[i] >= 0:
        return dp_memo[i]
    lp = 0
    for j in to_point[i]:
        lp = max(lp, dp(j)+1)
    dp_memo[i] = lp
    return lp

ans = 0
for x in start_point:
    ans = max(ans, dp(x))

print(ans)

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

int1 = lambda x: int(x) - 1

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

to_point = [[] for _ in range(N)]

for _ in range(M):
    x, y = map(int1, input().split())
    to_point[x].append(y)

dp_memo = [-1]*N
for x in range(N):
    if len(to_point[x]) == 0:
        dp_memo[x] = 0

# 点i始点のLongest Pathの長さ
def dp(i):
    if dp_memo[i] >= 0:
        return dp_memo[i]
    lp = 0
    for j in to_point[i]:
        lp = max(lp, dp(j)+1)
    dp_memo[i] = lp
    return lp

for i in range(N):
    dp(i)

print(max(dp_memo))

Buggy python submission (verdict: Time Limit Exceeded):
int1 = lambda x: int(x) - 1
import sys
sys.setrecursionlimit(1000000000)

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

edge = [[0]*N for _ in range(N)]
to_point = [[] for _ in range(N)]

for _ in range(M):
    x, y = map(int1, input().split())
    edge[x][y] += 1
    to_point[x].append(y)

end_point = set()
for x in range(N):
    if len(to_point[x]) == 0:
        end_point.add(x)

start_point = set()
for y in range(N):
    for x in range(N):
        if edge[x][y] > 0:
            break
    else:
        start_point.add(y)

dp_memo = [-1]*N

# 点i始点のLongest Pathの長さ
def dp(i):
    if dp_memo[i] >= 0:
        return dp_memo[i]
    if i in end_point:
        dp_memo[i] = 0
        return 0
    else:
        lp = 0
        for j in to_point[i]:
            lp = max(lp, dp(j)+1)
        dp_memo[i] = lp
        return lp

ans = 0
for x in start_point:
    ans = max(ans, dp(x))

print(ans)

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

int1 = lambda x: int(x) - 1

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

to_point = [[] for _ in range(N)]

for _ in range(M):
    x, y = map(int1, input().split())
    to_point[x].append(y)

dp_memo = [-1]*N
for x in range(N):
    if len(to_point[x]) == 0:
        dp_memo[x] = 0

# 点i始点のLongest Pathの長さ
def dp(i):
    if dp_memo[i] >= 0:
        return dp_memo[i]
    lp = 0
    for j in to_point[i]:
        lp = max(lp, dp(j)+1)
    dp_memo[i] = lp
    return lp

for i in range(N):
    dp(i)

print(max(dp_memo))

Buggy python submission (verdict: Time Limit Exceeded):
int1 = lambda x: int(x) - 1
import sys
sys.setrecursionlimit(1000000000)

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

edge = [[0]*N for _ in range(N)]
to_point = [[] for _ in range(N)]

for _ in range(M):
    x, y = map(int1, input().split())
    edge[x][y] += 1
    to_point[x].append(y)

end_point = set()
for x in range(N):
    if len(to_point[x]) == 0:
        end_point.add(x)

start_point = set()
for y in range(N):
    for x in range(N):
        if edge[x][y] > 0:
            break
    else:
        start_point.add(y)

# 点i始点のLongest Pathの長さ
def dp(i):
    if i in end_point:
        return 0
    else:
        lp = 0
        for j in to_point[i]:
            lp = max(lp, dp(j)+1)
        return lp

ans = 0
for x in start_point:
    ans = max(ans, dp(x))

print(ans)

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

int1 = lambda x: int(x) - 1

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

to_point = [[] for _ in range(N)]

for _ in range(M):
    x, y = map(int1, input().split())
    to_point[x].append(y)

dp_memo = [-1]*N
for x in range(N):
    if len(to_point[x]) == 0:
        dp_memo[x] = 0

# 点i始点のLongest Pathの長さ
def dp(i):
    if dp_memo[i] >= 0:
        return dp_memo[i]
    lp = 0
    for j in to_point[i]:
        lp = max(lp, dp(j)+1)
    dp_memo[i] = lp
    return lp

for i in range(N):
    dp(i)

print(max(dp_memo))

Buggy python submission (verdict: Time Limit Exceeded):
int1 = lambda x: int(x) - 1
import sys
sys.setrecursionlimit(1000000000)

N, M = map(int, input().split())
edge = [[0]*N for _ in range(N)]
for _ in range(M):
    x, y = map(int1, input().split())
    edge[x][y] += 1

end_point = set()
for x in range(N):
    for y in range(N):
        if edge[x][y] > 0:
            break
    else:
        end_point.add(x)

start_point = set()
for y in range(N):
    for x in range(N):
        if edge[x][y] > 0:
            break
    else:
        start_point.add(y)

# 点i始点のLongest Pathの長さ
def dp(i):
    if i in end_point:
        return 0
    else:
        lp = 0
        for j, flg in enumerate(edge[i]):
            if flg > 0:
                lp = max(lp, dp(j)+1)
        return lp

ans = 0
for x in start_point:
    ans = max(ans, dp(x))

print(ans)

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

int1 = lambda x: int(x) - 1

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

to_point = [[] for _ in range(N)]

for _ in range(M):
    x, y = map(int1, input().split())
    to_point[x].append(y)

dp_memo = [-1]*N
for x in range(N):
    if len(to_point[x]) == 0:
        dp_memo[x] = 0

# 点i始点のLongest Pathの長さ
def dp(i):
    if dp_memo[i] >= 0:
        return dp_memo[i]
    lp = 0
    for j in to_point[i]:
        lp = max(lp, dp(j)+1)
    dp_memo[i] = lp
    return lp

for i in range(N):
    dp(i)

print(max(dp_memo))