Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
import sys

stdin = sys.stdin


def ni(): return int(ns())


def na(): return list(map(int, stdin.readline().split()))


def naa(N): return [na() for _ in range(N)]


def ns(): return stdin.readline().rstrip()  # ignore trailing spaces


N = ni()
ans = 1
a_array = na()
for a in a_array:
    ans = ans * a
    if ans == 0:
        break
    if ans > 10 ** 18:
        ans = -1

print(ans)


Fixed submission:
import sys

stdin = sys.stdin


def ni(): return int(ns())


def na(): return list(map(int, stdin.readline().split()))


def naa(N): return [na() for _ in range(N)]


def ns(): return stdin.readline().rstrip()  # ignore trailing spaces


N = ni()
ans = 1
a_array = na()
overflow = 0
for a in a_array:
    if a == 0:
        ans = 0
        break
    if not overflow:
        ans = ans * a
        if ans > 10 ** 18:
            overflow = 1
            ans = -1

print(ans)
Buggy python submission (verdict: Runtime Error):
import sys
import math


stdin = sys.stdin


def ni(): return int(ns())


def na(): return list(map(int, stdin.readline().split()))


def ns(): return stdin.readline().rstrip()  # ignore trailing spaces


N = ni()

AB_array = [na() for _ in range(N)]

# print(AB_array)

plus_dic = {}
minus_dic = {}
ans = 1
fish_count = 0
mod = 10 ** 9 + 7
for ab in AB_array:
    a, b = ab
    if a == 0 and b == 0:
        continue
    fish_count += 1
    if a == 0:
        vec = (0, 1)
        if vec in plus_dic:
            plus_dic[vec] += 1
        else:
            plus_dic[vec] = 1
    elif b == 0:
        vec = (1, 0)
        if vec in minus_dic:
            minus_dic[vec] += 1
        else:
            minus_dic[vec] = 1
    c = math.gcd(a, b)
    a = a // c
    b = b // c
    if a < 0:
        a *= -1
        b *= -1
    if b > 0:
        vec = (a, b)
        if vec in plus_dic:
            plus_dic[vec] += 1
        else:
            plus_dic[vec] = 1
    else:
        vec = (a, b)
        if vec in minus_dic:
            minus_dic[vec] += 1
        else:
            minus_dic[vec] = 1


# print(plus_dic, minus_dic, fish_count)

for k, vp in plus_dic.items():
    x, y = k
    if (y, -x) in minus_dic:
        vm = minus_dic[(y, -x)]
        ans = ans * (pow(2, vp, mod) + pow(2, vm, mod) - 1) % mod
        fish_count -= (vp + vm)

ans = ans * pow(2, fish_count, mod) % mod

print((ans - 1) % mod)


Fixed submission:
import sys
import math


stdin = sys.stdin


def ni(): return int(ns())


def na(): return list(map(int, stdin.readline().split()))


def ns(): return stdin.readline().rstrip()  # ignore trailing spaces


N = ni()

AB_array = [na() for _ in range(N)]

# print(AB_array)

plus_dic = {}
minus_dic = {}
ans = 1
fish_count = 0
mod = 10 ** 9 + 7
zero_count = 0
for ab in AB_array:
    a, b = ab
    if a == 0 and b == 0:
        zero_count += 1
        continue
    fish_count += 1
    if a == 0:
        vec = (0, 1)
        if vec in plus_dic:
            plus_dic[vec] += 1
        else:
            plus_dic[vec] = 1
        continue
    elif b == 0:
        vec = (1, 0)
        if vec in minus_dic:
            minus_dic[vec] += 1
        else:
            minus_dic[vec] = 1
        continue
    c = math.gcd(a, b)
    a = a // c
    b = b // c
    if a < 0:
        a *= -1
        b *= -1
    if b > 0:
        vec = (a, b)
        if vec in plus_dic:
            plus_dic[vec] += 1
        else:
            plus_dic[vec] = 1
    else:
        vec = (a, b)
        if vec in minus_dic:
            minus_dic[vec] += 1
        else:
            minus_dic[vec] = 1


# print(plus_dic, minus_dic, fish_count)

for k, vp in plus_dic.items():
    x, y = k
    if (y, -x) in minus_dic:
        vm = minus_dic[(y, -x)]
        ans = ans * (pow(2, vp, mod) + pow(2, vm, mod) - 1) % mod
        fish_count -= (vp + vm)

ans = ans * pow(2, fish_count, mod) % mod

print((ans - 1 + zero_count) % mod)
Buggy python submission (verdict: Time Limit Exceeded):
import bisect
from collections import deque
import copy

N = int(input())
a_array = list(map(int, input().split()))

node_dic = {}
for _ in range(N - 1):
    u, v = map(int, input().split())
    if u in node_dic:
        node_dic[u].append(v)
    else:
        node_dic[u] = [v]

    if v in node_dic:
        node_dic[v].append(u)
    else:
        node_dic[v] = [u]

# print(node_dic)

use_array = [-1] * N
ans_array = [0] * N

edge_array = deque([(1, [0])])
use_array[0] = 1

while(len(edge_array)):
    edge, arr = edge_array.popleft()
    # print(edge, arr, ans_array)

    insert = bisect.bisect_left(arr, a_array[edge - 1])
    if insert == len(arr):
        arr.append(a_array[edge - 1])
    else:
        arr[insert] = a_array[edge - 1]
    ans_array[edge - 1] = len(arr) - 1
    for next_edge in node_dic[edge]:
        if use_array[next_edge - 1] == -1:
            use_array[next_edge - 1] = 1
            arr_next = copy.deepcopy(arr)
            edge_array.append((next_edge, arr_next))

print("\n".join(map(str, ans_array)))


Fixed submission:
import sys
import bisect


def dfs(edge):
    insert = bisect.bisect_left(lis_array, a_array[edge - 1])
    old = lis_array[insert]
    lis_array[insert] = a_array[edge - 1]
    ans = bisect.bisect_left(lis_array, INF)
    ans_array[edge - 1] = ans
    for next_edge in node_dic[edge]:
        if use_array[next_edge - 1] == -1:
            use_array[next_edge - 1] = 1
            dfs(next_edge)
    lis_array[insert] = old


sys.setrecursionlimit(10 ** 9)
N = int(input())
a_array = list(map(int, input().split()))

node_dic = {}
for _ in range(N - 1):
    u, v = map(int, input().split())
    if u in node_dic:
        node_dic[u].append(v)
    else:
        node_dic[u] = [v]

    if v in node_dic:
        node_dic[v].append(u)
    else:
        node_dic[v] = [u]

# print(node_dic)

use_array = [-1] * N
ans_array = [0] * N

INF = 2 * 10 ** 9
lis_array = [INF] * N

use_array[0] = 1
dfs(1)
print("\n".join(map(str, ans_array)))
Buggy python submission (verdict: Time Limit Exceeded):
# 大きい数用
def modPow(a, n, p):
    if n == 0:
        return 1
    if n == 1:
        return a
    if n % 2 == 1:
        return (a * (modPow(a, n//2, p) ** 2)) % p
    else:
        return (modPow(a, n//2, p) ** 2) % p


N, P = map(int, input().split())
S = input()

if P != 2 and P != 5:
    p_array = [0] * P
    mod = 0
    for i, s in enumerate(S[::-1]):
        s = int(s)
        mod = (mod + s * modPow(10, i, P)) % P
        p_array[mod] += 1

    ans = 0
    for i, p in enumerate(p_array):
        if i == 0:
            ans += p
        ans += (p * (p-1) // 2)

    print(int(ans))

else:
    ans = 0
    for i, s in enumerate(S):
        if int(s) % P == 0:
            ans += (i+1)
    print(ans)


Fixed submission:
N, P = map(int, input().split())
S = input()

if P != 2 and P != 5:
    p_array = [0] * P
    mod = 0
    amari = 1
    for i, s in enumerate(S[::-1]):
        s = int(s)
        mod = (mod + s * amari) % P
        amari = amari * 10 % P
        p_array[mod] += 1

    ans = 0
    for i, p in enumerate(p_array):
        if i == 0:
            ans += p
        ans += (p * (p-1) // 2)

    print(int(ans))

else:
    ans = 0
    for i, s in enumerate(S):
        if int(s) % P == 0:
            ans += (i+1)
    print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import fractions

A, B = map(int, input().split())

gcd = fractions.gcd(A, B)


ans = 1

for p in range(2, gcd+1):
    if gcd % p == 0:
        ans += 1
        gcd = gcd / p
        while gcd % p == 0:
            gcd = gcd / p
    if p > gcd:
        break
print(ans)

Fixed submission:
import fractions
import math

A, B = map(int, input().split())

gcd = fractions.gcd(A, B)


ans = 1

max_N = int(math.sqrt(gcd))

for p in range(2, max_N+1):
    if gcd % p == 0:
        ans += 1
        gcd = gcd / p
        while gcd % p == 0:
            gcd = gcd / p
    if p > gcd:
        break
if gcd != 1:
    ans += 1
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = input()
ans = 0

letters_dict = {}

for i in range(N):
    for j in range(i + 1, N):
        key = S[i:j + 1]
        if key not in letters_dict:
            letters_dict[key] = i
        else:
            ans = max(ans, min(len(key), i - letters_dict[key]))

print(ans)

Fixed submission:
N = int(input())
S = input()

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

for j in range(N):
    if S[j] == S[-1]:
        dp[-1][j] = 1

for i in range(N - 2, -1, -1):
    for j in range(N - 1):
        if S[i] == S[j]:
            dp[i][j] = dp[i + 1][j + 1] + 1
    if S[i] == S[-1]:
        dp[i][-1] = 1

ans = 0
for i in range(N):
    for j in range(N):
        ans = max(ans, min(dp[i][j], abs(i - j)))

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = input()
ans = 0

letters_dict = {}

for i in range(N):
    for j in range(i + 1, N):
        key = S[i:j + 1]
        if key not in letters_dict:
            letters_dict[key] = i
        else:
            ans = max(ans, min(len(key), i - letters_dict[key]))

print(ans)

Fixed submission:
N = int(input())
S = input()

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

for j in range(N):
    if S[j] == S[-1]:
        dp[-1][j] = 1

for i in range(N - 2, -1, -1):
    for j in range(N - 1):
        if S[i] == S[j]:
            dp[i][j] = dp[i + 1][j + 1] + 1
    if S[i] == S[-1]:
        dp[i][-1] = 1

ans = 0
for i in range(N):
    for j in range(N):
        ans = max(ans, min(dp[i][j], abs(i - j)))

print(ans)
Buggy python submission (verdict: Runtime Error):
import collections
N = int(input())
xy_array = [list(map(int, input().split())) for i in range(N)]
# print(xy_array)
distance_array = []
for i in range(N):
    x_i, y_i = xy_array[i]
    for j in range(N):
        if i == j:
            continue
        x_j, y_j = xy_array[j]
        distance_array.append((x_i - x_j, y_i - y_j))

ans_array = collections.Counter(distance_array)
max_count = ans_array.most_common()[0][1]
print(N - max_count)


Fixed submission:
import collections
N = int(input())
xy_array = [list(map(int, input().split())) for i in range(N)]
if N == 1:
    print(1)
    exit()
# print(xy_array)
distance_array = []
for i in range(N):
    x_i, y_i = xy_array[i]
    for j in range(N):
        if i == j:
            continue
        x_j, y_j = xy_array[j]
        distance_array.append((x_i - x_j, y_i - y_j))

ans_array = collections.Counter(distance_array)
max_count = ans_array.most_common()[0][1]
print(N - max_count)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A_array = map(int, input().split())
B_array = map(int, input().split())

AB_use = []
BA_use = []
for a, b in zip(A_array, B_array):

    if a > b:
        BA_use.append((b, a))

    elif b > a:
        AB_use.append((a, b))

ans = N

if len(AB_use):
    ans_array = [0] * (ans + 1)
    for i in range(1, ans + 1):
        ans_max = ans_array[i - 1] + 1
        for j in AB_use:
            w, v = j
            if w <= i:
                ans_max = max(ans_max, ans_array[i - w] + v)
        ans_array[i] = ans_max
    # print(ans_array)
    ans = ans_array[ans]


if len(BA_use):
    ans_array = [0] * (ans + 1)
    for i in range(1, ans + 1):
        ans_max = ans_array[i - 1] + 1
        for j in BA_use:
            w, v = j
            if w <= i:
                ans_max = max(ans_max, ans_array[i - w] + v)
        ans_array[i] = ans_max
    ans = ans_array[ans]
    # print(ans_array)
print(ans)

Fixed submission:
N = int(input())
A_array = map(int, input().split())
B_array = map(int, input().split())

AB_use = []
BA_use = []
for a, b in zip(A_array, B_array):

    if a > b:
        BA_use.append((b, a))

    elif b > a:
        AB_use.append((a, b))

ans = N

if len(AB_use):
    if len(AB_use) == 1:
        w, v = AB_use[0]
        ans = v * (ans//w) + (ans % w)
    else:
        ans_array = [0] * (ans + 1)
        for i in range(1, ans + 1):
            ans_max = ans_array[i - 1] + 1
            for j in AB_use:
                w, v = j
                if w <= i:
                    ans_max = max(ans_max, ans_array[i - w] + v)
            ans_array[i] = ans_max
        # print(ans_array)
        ans = ans_array[ans]


if len(BA_use):
    if len(BA_use) == 1:
        w, v = BA_use[0]
        ans = v * (ans//w) + (ans % w)
    else:
        ans_array = [0] * (ans + 1)
        for i in range(1, ans + 1):
            ans_max = ans_array[i - 1] + 1
            for j in BA_use:
                w, v = j
                if w <= i:
                    ans_max = max(ans_max, ans_array[i - w] + v)
            ans_array[i] = ans_max
        ans = ans_array[ans]
        # print(ans_array)
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
import math
readline = sys.stdin.readline

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

q_array = []

score_needed = 0

for i in range(N):
    b, l, u = map(int, readline().split())
    q_array.append([b, l, u, b * l + (X - b) * u])
    score_needed += b * l

q_array = sorted(q_array, key=lambda x: -x[3])

ans = 0
# print(q_array)
for i in range(N):
    if q_array[i][3] <= score_needed:
        ans += X
        score_needed -= q_array[i][3]
        if score_needed == 0:
            break
    else:
        study_min = X
        # print(score_needed)
        for j in range(i, N):
            b, l, u, s = q_array[j]
            if score_needed > s:
                break
            yobun = s - score_needed
            if score_needed <= b * l:
                study_min = min(study_min, math.ceil(score_needed / l))
            else:
                study_min = min(study_min, b + math.ceil(
                    (score_needed - b * l) / u))
            for k in range(i):
                b, l, u, s = q_array[k]
                needed = s - yobun
                if score_needed <= b * l:
                    study_min = min(study_min, math.ceil(needed / l))
                else:
                    study_min = min(study_min, b + math.ceil(
                        (needed - b * l) / u))
        ans += study_min
        break
print(ans)

Fixed submission:
import sys
readline = sys.stdin.readline

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

q_array = []
s_array = []

score_needed = 0

for i in range(N):
    b, l, u = map(int, readline().split())
    q_array.append([b, l, u, b * l + (X - b) * u])
    s_array.append(b * l + (X - b) * u)
    score_needed += b * l

q_array = sorted(q_array, key=lambda x: -x[3])
s_array = sorted(s_array, key=lambda x: -x)
sum_array = []
ss = 0
for s in s_array:
    ss += s
    sum_array.append(ss)
ans_min = 0
ans_max = N * X
if score_needed == 0:
    print(0)
    exit()
while ans_max - ans_min > 1:
    ans = (ans_min + ans_max) // 2
    use_all = ans // X
    use_part = ans % X
    # print(use_all, use_part)
    score_get_max = 0
    score_tmp = 0
    if use_part == 0:
        score_get_max = sum_array[use_all - 1]
    else:
        score_get_max = 0
        score_tmp = 0
        for q in q_array:
            b, l, u, s = q
            score_tmp = 0
            if use_part <= b:
                score_tmp = use_part * l
            else:
                score_tmp = (b * l + (use_part - b) * u)
            if use_all > 0:
                if s_array[use_all - 1] <= s:
                    score_tmp += (sum_array[use_all] - s)
                else:
                    score_tmp += sum_array[use_all - 1]
            score_get_max = max(score_get_max, score_tmp)
    # print(ans, score_get_max)
    if score_get_max >= score_needed:
        ans_max = ans
    else:
        ans_min = ans

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

H, W = map(int, input().split())
ans_array = [[10000] * W for i in range(H)]
ans = 10000
ans_check_flag = 0
for i in range(H):
    A_array = input()[:-1]
    for j, s in enumerate(A_array):
        # print(s)
        if s == ".":
            continue
        ans_check_flag = 0
        for h in range(max(0, i - ans + 1), min(H, i + ans - 1)):
            for w in range(max(0, j - ans + 1), min(W, j + ans - 1)):
                # print(i, j, h, w)
                distance = abs(h - i) + abs(w - j)
                if ans_array[h][w] == ans and distance < ans_array[h][w]:
                    ans_check_flag = 1
                    ans_array[h][w] = distance
                    continue
                ans_array[h][w] = min(ans_array[h][w], distance)
        if ans_check_flag:
            ans = max([max(ans_array[i]) for i in range(H)])
print(ans)

Fixed submission:
from scipy import ndimage
import numpy as np
import sys
input = sys.stdin.readline

H, W = map(int, input().split())

A_array = np.array(
    [[0 if s == "#" else 1 for s in input()[:-1]] for i in range(H)])
# print(A_array)


# print(A_array)
b = ndimage.morphology.distance_transform_cdt(
    A_array, metric="taxicab")
print(np.max(b))

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

H, W = map(int, input().split())
A_array = [input()[:-1] for s in range(H)]

ans_array = [[10000] * W for i in range(H)]
# print(ans_array)

for i, A in enumerate(A_array):
    black_index = [i for i, x in enumerate(A) if x == "#"]
    # print(black_index)
    for black in black_index:
        ans_array = [[
            min(ans_array[h][w],
                abs(h - i) + abs(w - black)) for w in range(W)
        ] for h in range(H)]
print(max([max(ans_array[i]) for i in range(H)]))

Fixed submission:
from scipy import ndimage
import numpy as np
import sys
input = sys.stdin.readline

H, W = map(int, input().split())

A_array = np.array(
    [[0 if s == "#" else 1 for s in input()[:-1]] for i in range(H)])
# print(A_array)


# print(A_array)
b = ndimage.morphology.distance_transform_cdt(
    A_array, metric="taxicab")
print(np.max(b))

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

H, W = map(int, input().split())
A_array = [input()[:-1] for s in range(H)]

ans_array = [[10000] * W for i in range(H)]
# print(ans_array)

for i, A in enumerate(A_array):
    black_index = [i for i, x in enumerate(A) if x == "#"]
    # print(black_index)
    for black in black_index:
        ans_array = [[
            min(ans_array[h][w],
                abs(h - i) + abs(w - black)) for w in range(W)
        ] for h in range(H)]
print(max([max(ans_array[i]) for i in range(H)]))

Fixed submission:
from scipy import ndimage
import numpy as np
import sys
input = sys.stdin.readline

H, W = map(int, input().split())

A_array = np.array(
    [[0 if s == "#" else 1 for s in input()[:-1]] for i in range(H)])
# print(A_array)


# print(A_array)
b = ndimage.morphology.distance_transform_cdt(
    A_array, metric="taxicab")
print(np.max(b))
Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
N, K = map(int, input().split())

# print(N, K)
S = input()
index_array = np.array([])
now_letter = "1"
letter_count = 0
for s in S:
    if s != now_letter:
        index_array = np.append(index_array, letter_count)
        now_letter = s
        letter_count = 1
    else:
        letter_count += 1
else:
    index_array = np.append(index_array, letter_count)
    if now_letter == "0":
        index_array = np.append(index_array, 0)
# print(index_array)
ans = 0

array_len = len(index_array)
if (K * 2 + 1) >= array_len:
    print(N)
    exit()
candidate = (array_len + 1) // 2 - K
for i in range(candidate):
    # print(index_array[i * 2:(i + K) * 2 + 1])
    ans = max(np.sum(index_array[i * 2:(i + K) * 2 + 1]), ans)
print(int(ans))


Fixed submission:
N, K = map(int, input().split())

# print(N, K)
S = input()
index_array = [0]
now_letter = "1"
for i, s in enumerate(S):
    if s != now_letter:
        now_letter = s
        index_array.append(i)
else:
    if now_letter == "1":
        index_array.append(N)
    else:
        index_array.append(N)
        index_array.append(N)        

# print(index_array)
ans = 0
array_len = len(index_array)
if (K + 1) * 2 > array_len:
    print(N)
    exit()
candidate = array_len // 2 - K
for i in range(candidate):
    # print(index_array[(i + K) * 2 + 1] - index_array[i * 2])
    ans = max(index_array[(i + K) * 2 + 1] - index_array[i * 2], ans)
print(int(ans))
Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
N, Q = map(int, input().split())
# print(N, Q)
S = input()
AC_array = np.array([
    1 if (S[i] == "A" and S[i + 1] == "C") else 0 for i in range(N - 1)
])
lr_array = [list(map(int, input().split())) for i in range(Q)]
# print(lr_array)
# print(AC_array)
ans = ""
for lr in lr_array:
    ans += str(np.sum(AC_array[lr[0]-1:lr[1]-1])) + "\n"
print(ans[:-1])

Fixed submission:
N, Q = map(int, input().split())
# print(N, Q)
S = input()
lr_array = [list(map(int, input().split())) for i in range(Q)]
AC_array = []
AC_count = 0

# print(S)
for i, s in enumerate(S):
    if i == 0:
        s_old = s
        continue
    if s_old == "A" and s == "C":
        AC_count += 1
    AC_array.append(AC_count)
    s_old = s

# print(lr_array)
# print(AC_array)
ans = ""
for lr in lr_array:
    ans += str(AC_array[lr[1] - 2] -
               (AC_array[lr[0] - 2] if lr[0] > 1 else 0)) + "\n"
print(ans[:-1])

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
N, Q = map(int, input().split())
# print(N, Q)
S = input()
AC_array = np.array([
    1 if (S[i] == "A" and S[i + 1] == "C") else 0 for i in range(N - 1)
])
lr_array = [list(map(int, input().split())) for i in range(Q)]
# print(lr_array)
# print(AC_array)
for lr in lr_array:
    print(np.sum(AC_array[lr[0]-1:lr[1]-1]))

Fixed submission:
N, Q = map(int, input().split())
# print(N, Q)
S = input()
lr_array = [list(map(int, input().split())) for i in range(Q)]
AC_array = []
AC_count = 0

# print(S)
for i, s in enumerate(S):
    if i == 0:
        s_old = s
        continue
    if s_old == "A" and s == "C":
        AC_count += 1
    AC_array.append(AC_count)
    s_old = s

# print(lr_array)
# print(AC_array)
ans = ""
for lr in lr_array:
    ans += str(AC_array[lr[1] - 2] -
               (AC_array[lr[0] - 2] if lr[0] > 1 else 0)) + "\n"
print(ans[:-1])
Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import deque

stdin = sys.stdin


def ni(): return int(ns())


def na(): return list(map(int, stdin.readline().split()))


def naa(N): return [na() for _ in range(N)]


def ns(): return stdin.readline().rstrip()  # ignore trailing spaces


N, M = na()
node_dic = {}
no_in = [1] * N

for _ in range(M):
    x, y = na()
    if x-1 in node_dic:
        node_dic[x-1].append(y-1)
    else:
        node_dic[x-1] = [y-1]
    no_in[y-1] = 0


# print(node_dic, no_in)


ans_array = [0] * N

edge = deque([])

for i, x in enumerate(no_in):
    if x == 1:
        deque.append(edge, i)

while(len(edge)):
    e = deque.popleft(edge)
    if e in node_dic:
        for x in node_dic[e]:
            deque.append(edge, x)
            ans_array[x] = max(ans_array[x], ans_array[e] + 1)
    # print(ans_array)
print(max(ans_array))

Fixed submission:
import sys
from collections import deque
sys.setrecursionlimit(10**8)
stdin = sys.stdin


def ni(): return int(ns())


def na(): return list(map(int, stdin.readline().split()))


def naa(N): return [na() for _ in range(N)]


def ns(): return stdin.readline().rstrip()  # ignore trailing spaces


def cal(e):
    if ans_array[e] != -1:
        return ans_array[e]
    ans = 0
    if e in node_dic:
        for x in node_dic[e]:
            ans = max(ans, cal(x) + 1)
    ans_array[e] = ans
    return ans


N, M = na()
node_dic = {}
no_in = [1] * N

for _ in range(M):
    x, y = na()
    if x-1 in node_dic:
        node_dic[x-1].append(y-1)
    else:
        node_dic[x-1] = [y-1]
    no_in[y-1] = 0


# print(node_dic, no_in)


ans_array = [-1] * N

edge = deque([])

for i, x in enumerate(no_in):
    if x == 1:
        cal(i)


print(max(ans_array))