Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
A=list(map(int,input().split()))
#A=int(input().split())
#print(A)
B=[ 0 for _ in range(N)]
#print(B)
ans=""
for i in range(N):
  B[A[i]-1]=i+1
#  ans+=str(A.index(i+1)+1)
#  if i!=N-1:

for i in range(N):
  ans+=str(B[i])
  if i!=N-1:
    ans+=" "
#print(B)
print(ans)

Fixed submission:
N=int(input())
A=list(map(int,input().split()))
#A=int(input().split())
#print(A)
B=[ 0 for _ in range(N)]
#print(B)
ans=""
for i in range(N):
  B[A[i]-1]=i+1
#  ans+=str(A.index(i+1)+1)
#  if i!=N-1:

for i in range(N):
  ans+=str(B[i])
  if i!=N-1:
    ans+=" "
#print(B)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
A=list(map(int,input().split()))
ans=""
for i in range(N):
  ans+=str(A.index(i+1)+1)
  if i!=N-1:
    ans+=" "
print(ans)


Fixed submission:
N=int(input())
A=list(map(int,input().split()))
#A=int(input().split())
#print(A)
B=[ 0 for _ in range(N)]
#print(B)
ans=""
for i in range(N):
  B[A[i]-1]=i+1
#  ans+=str(A.index(i+1)+1)
#  if i!=N-1:

for i in range(N):
  ans+=str(B[i])
  if i!=N-1:
    ans+=" "
#print(B)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
A=list(map(int,input().split()))
ans=""
for i in range(N):
  ans+=str(A.index(i+1)+1)
  if i!=N-1:
    ans+=" "
print(ans)

Fixed submission:
N=int(input())
A=list(map(int,input().split()))
#A=int(input().split())
#print(A)
B=[ 0 for _ in range(N)]
#print(B)
ans=""
for i in range(N):
  B[A[i]-1]=i+1
#  ans+=str(A.index(i+1)+1)
#  if i!=N-1:

for i in range(N):
  ans+=str(B[i])
  if i!=N-1:
    ans+=" "
#print(B)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
A=list(map(int,input().split()))
#print(N,A)
for i in range(N):
  print(A.index(i+1)+1,end="")
  if i!=N-1:
  	print(" ",end="")
print("")

Fixed submission:
N=int(input())
A=list(map(int,input().split()))
#A=int(input().split())
#print(A)
B=[ 0 for _ in range(N)]
#print(B)
ans=""
for i in range(N):
  B[A[i]-1]=i+1
#  ans+=str(A.index(i+1)+1)
#  if i!=N-1:

for i in range(N):
  ans+=str(B[i])
  if i!=N-1:
    ans+=" "
#print(B)
print(ans)
Buggy python submission (verdict: Runtime Error):
import math
N,M=map(int,input().split())
#print(N,M)
a = list(map(float,input().split()))
#print(a)
a.sort(reverse=True)
#print(a)
k=0
maxa=a[0]/2
while M>0:
#  print(M,k,maxa,a)
  if a[k]>=maxa:
    a[k]=a[k]/2
    M=M-1
  if k==len(a)-1:
    a.sort(reverse=True)
    k=0
    maxa=a[0]
    if maxa<1:
      break
    continue
  if a[k]<a[k+1]:
    if maxa<a[k]:
      maxa=a[k]
    k=k+1
  elif a[k]<maxa:
    if maxa<a[k]:
      maxa=a[k]
    k=k+1
#print(a)
total=0
for t in a:
  total+=int(t)
#  print(t,total)
print(total)

Fixed submission:
s=input()
if s=="Sunny":
  print ("Cloudy")
elif s=="Cloudy":
  print ("Rainy")
else:
  print ("Sunny")
Buggy python submission (verdict: Time Limit Exceeded):
import math
N,M=map(int,input().split())
#print(N,M)
a = list(map(float,input().split()))
#print(a)
a.sort(reverse=True)
#print(a)
k=0
maxa=a[0]/2
while M>0:
#  print(M,k,maxa,a)
  if a[k]>=maxa:
    a[k]=a[k]/2
    M=M-1
  if k==len(a)-1:
    a.sort(reverse=True)
    k=0
    maxa=a[0]
    if maxa<1:
      break
    continue
  if a[k]<a[k+1]:
    if maxa<a[k]:
      maxa=a[k]
    k=k+1
  elif a[k]<maxa:
    if maxa<a[k]:
      maxa=a[k]
    k=k+1
#print(a)
total=0
for t in a:
  total+=int(t)
#  print(t,total)
print(total)

Fixed submission:
n, m = list(map(int, input().split()))
lisA = [int(i) for i in input().split()]
#print(n, m)
#print(lisA)
 
lisA.sort(reverse=True)
half0 = lisA[0] // 2
count = 0
for i in range(m):
	if (count > n - 1):
		count = 0
	if (lisA[count] < half0):
		lisA.sort(reverse=True)
		half0 = lisA[0] // 2
		count = 0
	lisA[count] //= 2
	count += 1
print(sum(lisA))

Buggy python submission (verdict: Time Limit Exceeded):
import math
N,M=map(int,input().split())
#print(N,M)
a = list(map(float,input().split()))
#print(a)
a.sort(reverse=True)
#print(a)
k=0
maxa=a[0]/2
while M>0:
#  print(M,k,maxa,a)
  if a[k]>=maxa:
    a[k]=a[k]/2
    M=M-1
  if k==len(a)-1:
    a.sort(reverse=True)
    k=0
    maxa=a[0]
    if maxa<1:
      break
    continue
  if a[k]<a[k+1]:
    if maxa<a[k]:
      maxa=a[k]
    k=k+1
  elif a[k]<maxa:
    if maxa<a[k]:
      maxa=a[k]
    k=k+1
#print(a)
total=0
for t in a:
  total+=int(t)
#  print(t,total)
print(total)

Fixed submission:
n, m = list(map(int, input().split()))
lisA = [int(i) for i in input().split()]
#print(n, m)
#print(lisA)
 
lisA.sort(reverse=True)
half0 = lisA[0] // 2
count = 0
for i in range(m):
	if (count > n - 1):
		count = 0
	if (lisA[count] < half0):
		lisA.sort(reverse=True)
		half0 = lisA[0] // 2
		count = 0
	lisA[count] //= 2
	count += 1
print(sum(lisA))

Buggy python submission (verdict: Time Limit Exceeded):
import math
N,M=map(int,input().split())
#print(N,M)
a = list(map(float,input().split()))
#print(a)
a.sort(reverse=True)
#print(a)
k=0
maxa=a[0]/2
while M>0:
#  print(M,k,maxa,a)
  if a[k]>=maxa:
    a[k]=a[k]/2
    M=M-1
  if k==len(a)-1:
    a.sort(reverse=True)
    k=0
    maxa=a[0]
    if maxa<1:
      break
    continue
  if a[k]<a[k+1]:
    if maxa<a[k]:
      maxa=a[k]
    k=k+1
  elif a[k]<maxa:
    if maxa<a[k]:
      maxa=a[k]
    k=k+1
#print(a)
total=0
for t in a:
  total+=int(t)
#  print(t,total)
print(total)

Fixed submission:
n, m = list(map(int, input().split()))
lisA = [int(i) for i in input().split()]
#print(n, m)
#print(lisA)
 
lisA.sort(reverse=True)
half0 = lisA[0] // 2
count = 0
for i in range(m):
	if (count > n - 1):
		count = 0
	if (lisA[count] < half0):
		lisA.sort(reverse=True)
		half0 = lisA[0] // 2
		count = 0
	lisA[count] //= 2
	count += 1
print(sum(lisA))
Buggy python submission (verdict: Runtime Error):
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=list(map(int,input().split()))
#print(n)
#print(a)
#print(b)
#print(c)
total=0
pre=0
for i in range(n):
  total+=b[i]
  if pre+1==a[i] and pre!=0:
    total+=c[pre-1]
  pre=a[i]
print(total)

Fixed submission:
a=int(input())
print(a*a*a)
Buggy python submission (verdict: Runtime Error):
def main():
    a = int(input())
    b = int(input())
    ans_list = [1, 2, 3]
    ans_list.pop(a-1)
    ans_list.pop(b-1)

    ans = ans_list[0]
    print(ans)


if __name__ == '__main__':
    main()


Fixed submission:
def main():
    a = int(input())
    b = int(input())

    ans_list = [1, 2, 3]
    for i in range(3):
        if ans_list[i] == a or ans_list[i] == b:
            ans_list[i] = -1
    ans = 0
    for i in range(3):
        if ans_list[i] > 0:
            ans = ans_list[i]

    print(ans)


if __name__ == '__main__':
    main()
Buggy python submission (verdict: Runtime Error):
import math


def main():
    a, b, x = map(int, input().split())
    y = x / a
    s = a * b
    if y >= s / 2:
        y = (a * b) - (x / a)
        # a, b = b, a
        b_temp = 2 * y / a
        angle = math.atan(a / b_temp)
        print(90 - math.degrees(angle))
    else:
        a_temp = 2 * y / b
        angle = math.atan(b / a_temp)
        print(math.degrees(angle))



if __name__ == '__main__':
    main()


Fixed submission:
import math


def main():
    a, b, x = map(int, input().split())
    y = x / a
    s = a * b
    if y == s:
        print('0.0')
    elif y >= s / 2:
        y = (a * b) - (x / a)
        b_temp = 2 * y / a
        angle = math.atan(a / b_temp)
        print(90 - math.degrees(angle))
    else:
        a_temp = 2 * y / b
        angle = math.atan(b / a_temp)
        print(math.degrees(angle))



if __name__ == '__main__':
    main()
Buggy python submission (verdict: Time Limit Exceeded):
def sub_c():
    n = int(input())
    a_list = list(map(int, input().split()))

    a_tuple_list = []
    for i, a in enumerate(a_list):
        temp = (a, i)
        a_tuple_list.append(temp)
    a_tuple_list.sort()

    ret = ''
    for t in a_tuple_list:
        ret += str(t[1]+1) + ' '
    print(ret)


if __name__ == '__main__':
    sub_c()


Fixed submission:
def sub_c():
    n = int(input())
    a_list = list(map(int, input().split()))

    a_tuple_list = []
    for i, a in enumerate(a_list):
        temp = (a, i)
        a_tuple_list.append(temp)
    a_tuple_list.sort()

    ret = ''
    for t in a_tuple_list:
        ret += str(t[1]+1) + ' '
    print(ret)


if __name__ == '__main__':
    sub_c()
Buggy python submission (verdict: Time Limit Exceeded):
import collections


def main():
    n, q = map(int, input().split())

    # 頂点のリスト、ポイント、走査済みか
    graph = [[[], 0, False] for _ in range(n)]
    for i in range(n-1):
        a, b = map(int, input().split())
        graph[a-1][0].append(b-1)
        graph[b-1][0].append(a-1)

    for i in range(q):
        p, x = map(int, input().split())
        graph[p-1][1] += x

    queue = collections.deque([0])
    while queue:
        q = queue.pop()
        graph[q][2] = True
        for v in graph[q][0]:
            if graph[v][2]:
                continue
            graph[v][1] += graph[q][1]
            queue.append(v)

    for i in range(n):
        print(graph[i][1], end=' ')


if __name__ == '__main__':
    main()


Fixed submission:
def main():
    n, q = map(int, input().split())

    # 頂点のリスト、ポイント、走査済みか
    graph = [[[], 0, False] for _ in range(n)]
    for i in range(n-1):
        a, b = map(int, input().split())
        graph[a-1][0].append(b-1)
        graph[b-1][0].append(a-1)

    for i in range(q):
        p, x = map(int, input().split())
        graph[p-1][1] += x

    queue = [0]
    while queue:
        q = queue.pop()
        graph[q][2] = True
        for v in graph[q][0]:
            if graph[v][2]:
                continue
            graph[v][1] += graph[q][1]
            queue.append(v)

    for i in range(n):
        print(graph[i][1], end=' ')


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n, q = map(int, input().split())

    # 頂点のリスト、ポイント、走査済みか
    graph = [[[], 0, False] for _ in range(n)]
    for i in range(n-1):
        a, b = map(int, input().split())
        graph[a-1][0].append(b-1)
        graph[b-1][0].append(a-1)

    for i in range(q):
        p, x = map(int, input().split())
        graph[p-1][1] += x

    queue = [0]
    while queue:
        q = queue.pop()
        graph[q][2] = True
        for v in graph[q][0]:
            if graph[v][2]:
                continue
            graph[v][1] += graph[q][1]
            queue.append(v)

    for i in range(n):
        print(graph[i][1], end=' ')


if __name__ == '__main__':
    main()


Fixed submission:
def main():
    n, q = map(int, input().split())

    # 頂点のリスト、ポイント、走査済みか
    graph = [[[], 0, False] for _ in range(n)]
    for i in range(n-1):
        a, b = map(int, input().split())
        graph[a-1][0].append(b-1)
        graph[b-1][0].append(a-1)

    for i in range(q):
        p, x = map(int, input().split())
        graph[p-1][1] += x

    queue = [0]
    while queue:
        q = queue.pop()
        graph[q][2] = True
        for v in graph[q][0]:
            if graph[v][2]:
                continue
            graph[v][1] += graph[q][1]
            queue.append(v)

    for i in range(n):
        print(graph[i][1], end=' ')


if __name__ == '__main__':
    main()
Buggy python submission (verdict: Runtime Error):
import collections
# 再帰制限に引っかかった場合に外す
import sys
sys.setrecursionlimit(100000)

nCr = {}


def cmb(n, r):
    if r == 0 or r == n:
        return 1
    elif r == 1:
        return n
    elif n < r:
        return 0
    if (n, r) in nCr:
        return nCr[(n, r)]
    nCr[(n, r)] = cmb(n-1, r) + cmb(n-1, r-1)
    return nCr[(n, r)]


def main():
    n = int(input())
    s_list = []
    for i in range(n):
        temp_s = sorted(list(input()))
        s_list.append(''.join(temp_s))

    counter = collections.Counter(s_list)
    ans = 0
    for key, value in counter.items():
        temp = cmb(value, 2)
        ans += temp


    print(ans)


if __name__ == '__main__':
    main()


Fixed submission:
import collections
from operator import mul
from functools import reduce


def cmb(n, r):
    if n < r:
        return 0
    r = min(n-r, r)
    if r == 0:
        return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1, r + 1))
    return over // under


def main():
    n = int(input())
    s_list = []
    for i in range(n):
        temp_s = sorted(list(input()))
        s_list.append(''.join(temp_s))

    counter = collections.Counter(s_list)
    ans = 0
    for key, value in counter.items():
        temp = cmb(value, 2)
        ans += temp


    print(ans)


if __name__ == '__main__':
    main()
Buggy python submission (verdict: Time Limit Exceeded):
import math

def main():
    n = int(input())
    dams = list(map(int, input().split()))
    mountains = [0] * n
    left = 0
    # right = 10**9
    right = max(dams) * 2
    while True:
        center = (left + right) // 2
        if center % 2 == 1:
            center -= 1
        mountains[0] = center
        for i in range(1, n):
            mountains[i] = 2 * dams[i-1] - mountains[i-1]
        temp_dam = (mountains[0] + mountains[-1]) / 2
        if dams[-1] == temp_dam:
            break
        elif dams[-1] < temp_dam:
            right = center
        else:
            left = center
    ans = ' '.join(map(str, mountains))
    print(ans)


if __name__ == '__main__':
    main()


Fixed submission:
import math

def main():
    n = int(input())
    dams = list(map(int, input().split()))
    mountains = [0] * n
    left = 0
    # right = 10**9
    right = (max(dams)+1) * 2
    while True:
        center = (left + right) // 2
        if center % 2 == 1:
            center -= 1
        mountains[0] = center
        for i in range(1, n):
            mountains[i] = 2 * dams[i-1] - mountains[i-1]
        temp_dam = (mountains[0] + mountains[-1]) // 2
        if dams[-1] == temp_dam:
            break
        elif dams[-1] < temp_dam:
            right = center
        else:
            left = center
    ans = ' '.join(map(str, mountains))
    print(ans)


if __name__ == '__main__':
    main()
Buggy python submission (verdict: Runtime Error):
import math
import fractions

def main():
    a, b, c, d = map(int, input().split())

    quotient = []
    temp_lcm = fractions.gcd(c, d) // c * d
    for i, x in enumerate([c, d, temp_lcm]):
        temp = (b // x) - ((a-1) // x)
        quotient.append(temp)
    temp1 = b - a + 1
    ans = temp1 - (quotient[0] + quotient[1]) + quotient[2]
    print(ans)


if __name__ == '__main__':
    main()


Fixed submission:
import math
import fractions

def main():
    a, b, c, d = map(int, input().split())

    quotient = []
    temp_lcm = c * d // fractions.gcd(c, d)
    for i, x in enumerate([c, d, temp_lcm]):
        temp = (b // x) - ((a-1) // x)
        quotient.append(temp)
    temp1 = b - a + 1
    ans = temp1 - (quotient[0] + quotient[1]) + quotient[2]
    print(ans)


if __name__ == '__main__':
    main()
Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n = int(input())
    a_list = list(map(int, input().split()))

    temp_list = []
    count = 0
    is_ongoing = True
    while is_ongoing:
        for a in a_list:
            if a % 2 == 0:
                temp_list.append(a/2)
            else:
                is_ongoing = False
                print(count)
                break
        else:
            count += 1
        a_list = temp_list[:]


if __name__ == '__main__':
    main()


Fixed submission:
def main():
    n = int(input())
    a_list = list(map(int, input().split()))

    # temp_list = []
    count = 0
    is_ongoing = True
    while is_ongoing:
        for i, a in enumerate(a_list):
            if a % 2 == 0:
                # temp_list.append(a/2)
                a_list[i] /= 2
            else:
                is_ongoing = False
                print(count)
                break
        else:
            count += 1
        # a_list = temp_list[:]


if __name__ == '__main__':
    main()
Buggy python submission (verdict: Time Limit Exceeded):
k, s = map(int, input().split())
# print(k, s)
count = 0
for x in range(k+1):
    for y in range(k+1):
        for z in range(k+1):
            # print(x, y, z)
            if x + y + z == s:
                count += 1
print(count)

Fixed submission:
k, s = map(int, input().split())
# print(k, s)
count = 0
for x in range(k+1):
    for y in range(k+1):
        # print(x, y)
        if 0 <= s - x - y <= k:
            count += 1
print(count)

Buggy python submission (verdict: Time Limit Exceeded):
k, s = map(int, input().split())
# print(k, s)
count = 0
for i in range(k+1):
    for j in range(k+1):
        for k in range(k+1):
            # print(i, j, k)
            if i + j + k == s:
                count += 1
print(count)

Fixed submission:
k, s = map(int, input().split())
# print(k, s)
count = 0
for x in range(k+1):
    for y in range(k+1):
        # print(x, y)
        if 0 <= s - x - y <= k:
            count += 1
print(count)

Buggy python submission (verdict: Time Limit Exceeded):
k, s = map(int, input().split())
# print(k, s)
count = 0
for i in range(k+1):
    for j in range(k+1):
        for k in range(k+1):
            # print(i, j, k)
            if i + j + k == s:
                count += 1
print(count)

Fixed submission:
k, s = map(int, input().split())
# print(k, s)
count = 0
for x in range(k+1):
    for y in range(k+1):
        # print(x, y)
        if 0 <= s - x - y <= k:
            count += 1
print(count)
Buggy python submission (verdict: Runtime Error):
A = []
index = {}
for i in range(3):
    lst = list(map(int,input().split()))
    for idx,l in enumerate(lst):
        index[l] = (i,idx)
    A.append(lst)
#print(index)
N = int(input())

for _ in range(N):
    s = int(input())
    if s in index:
        i,j = index[s]
        A[i][j] = 0

for i in range(3):
    if sum(A[i]) == 0:
        print("Yes")
        exit()
for i in range(3):
    su = 0
    for j in range(3):
        su+=A[j][i]
    if su == 0:
        print("Yes")
        eixt()
if A[0][0] + A[1][1] + A[2][2] == 0 or A[0][2] + A[1][1] + A[2][0] == 0:
    print("Yes")
    exit()
print("No")
    
    
        

Fixed submission:
A = []
index = {}
for i in range(3):
    lst = list(map(int,input().split()))
    for idx,l in enumerate(lst):
        index[l] = (i,idx)
    A.append(lst)
N = int(input())

for _ in range(N):
    s = int(input())
    if s in index:
        i,j = index[s]
        A[i][j] = 0

for i in range(3):
    if sum(A[i]) == 0:
        print("Yes")
        exit()
for i in range(3):
    su = 0
    for j in range(3):
        su+=A[j][i]
    if su == 0:
        print("Yes")
        exit()
if A[0][0] + A[1][1] + A[2][2] == 0 or A[0][2] + A[1][1] + A[2][0] == 0:
    print("Yes")
    exit()
print("No")
Buggy python submission (verdict: Runtime Error):
from functools import lru_cache
import sys
sys.setrecursionlimit(2147483647)

N = int(input())
A = []
for i in range(0,N):
    A.append(list(map(int,input().split())))

@lru_cache(None)
def dfs(d,pre):
    if d>=N-1: return 0
    tmp = [0] 
    for idx,a in enumerate(A[d]):
        if idx != pre:
            res = dfs(d + 1,idx) + a 
            tmp.append(res)
    return max(tmp)

print(dfs(-1,-1))

Fixed submission:
N,K = map(int,input().split())
A = list(map(int, input().split()))
dp = [0 for _ in range(len(A))]
dp[0] = 0 
for i in range(1,N):
    tmp = [float("inf")]
    for j in range(1,K+1):
        if i - j>=0:
            tmp.append(dp[i-j] + abs(A[i] - A[i-j]))
    dp[i] = min(tmp)

print(dp[len(A)-1])

Buggy python submission (verdict: Runtime Error):
from functools import lru_cache
import sys
sys.setrecursionlimit(2147483647)

N = int(input())
A = []
for i in range(0,N):
    A.append(list(map(int,input().split())))



@lru_cache(None)
def dfs(d,pre):
    if d>=N-1: return 0
    tmp = [0] 
    for idx,a in enumerate(A[d]):
        if idx != pre:
            res = dfs(d + 1,idx) + a 
            tmp.append(res)
    return max(tmp)

print(dfs(-1,-1))

Fixed submission:
N,K = map(int,input().split())
A = list(map(int, input().split()))
dp = [0 for _ in range(len(A))]
dp[0] = 0 
for i in range(1,N):
    tmp = [float("inf")]
    for j in range(1,K+1):
        if i - j>=0:
            tmp.append(dp[i-j] + abs(A[i] - A[i-j]))
    dp[i] = min(tmp)

print(dp[len(A)-1])

Buggy python submission (verdict: Runtime Error):
N = int(input())
A = []
for i in range(0,N):
    A.append(list(map(int,input().split())))

def dfs(d,pre):
    if d>=N-1: return 0
    tmp = [0] 
    for idx,a in enumerate(A[d]):
        if idx != pre:
            res = dfs(d + 1,idx) + a 
            tmp.append(res)
    return max(tmp)

print(dfs(-1,-1))

Fixed submission:
N,K = map(int,input().split())
A = list(map(int, input().split()))
dp = [0 for _ in range(len(A))]
dp[0] = 0 
for i in range(1,N):
    tmp = [float("inf")]
    for j in range(1,K+1):
        if i - j>=0:
            tmp.append(dp[i-j] + abs(A[i] - A[i-j]))
    dp[i] = min(tmp)

print(dp[len(A)-1])
Buggy python submission (verdict: Runtime Error):
import collections
N,M = map(int,input().split())
A = [list(map(int, input().split())) for i in range(M)]
visit = set()
G = collections.defaultdict(set)
for idx,(f,to) in enumerate(A):
    G[f].add(to)

cache = {}
    
def dfs(start):
    if start in cache:
        return cache[start]
    ma = 0
    for i in G[start]:
        if i not in visit:
            visit.add(i)
            ma = max(ma,dfs(i))
            visit.remove(i)
    cache[start] = ma + 1 
    return ma + 1

ma = 0 
for f,to in A:
    visit.add(f)
    ma = max(ma,dfs(f))
    visit.remove(f)
print(ma - 1)
        
        

Fixed submission:
import collections
import sys
sys.setrecursionlimit(10000000)

N,M = map(int,input().split())
A = [list(map(int, input().split())) for i in range(M)]
visit = set()
G = collections.defaultdict(set)
for idx,(f,to) in enumerate(A):
    G[f].add(to)

cache = {}
    
def dfs(start):
    if start in cache:
        return cache[start]
    ma = 0
    for i in G[start]:
        if i not in visit:
            visit.add(i)
            ma = max(ma,dfs(i))
            visit.remove(i)
    cache[start] = ma + 1 
    return ma + 1

ma = 0 
for f,to in A:
    visit.add(f)
    ma = max(ma,dfs(f))
    visit.remove(f)
print(ma - 1)

Buggy python submission (verdict: Runtime Error):
N,M = map(int,input().split())
A = [list(map(int, input().split())) for i in range(M)]
visit = set()
G = collections.defaultdict(set)
for idx,(f,to) in enumerate(A):
    G[f].add(to)

cache = {}
    
def dfs(start):
    if start in cache:
        return cache[start]
    ma = 0
    for i in G[start]:
        if i not in visit:
            visit.add(i)
            ma = max(ma,dfs(i))
            visit.remove(i)
    cache[start] = ma + 1 
    return ma + 1

ma = 0 
for f,to in A:
    visit.add(f)
    ma = max(ma,dfs(f))
    visit.remove(f)
print(ma - 1)
        
        

Fixed submission:
import collections
import sys
sys.setrecursionlimit(10000000)

N,M = map(int,input().split())
A = [list(map(int, input().split())) for i in range(M)]
visit = set()
G = collections.defaultdict(set)
for idx,(f,to) in enumerate(A):
    G[f].add(to)

cache = {}
    
def dfs(start):
    if start in cache:
        return cache[start]
    ma = 0
    for i in G[start]:
        if i not in visit:
            visit.add(i)
            ma = max(ma,dfs(i))
            visit.remove(i)
    cache[start] = ma + 1 
    return ma + 1

ma = 0 
for f,to in A:
    visit.add(f)
    ma = max(ma,dfs(f))
    visit.remove(f)
print(ma - 1)