Harryxun/GPT-100-sft-dataset huggingface dataset:
Problem p02573 (python)

Fixed submission:
#unionfind木_集合の管理_同じ集合の数を数える場合：
#parには集合の数と親の名前を同時にメモ化する（根なら-size, 子なら親）
def find(k):#要素k
    if par[k] < 0:#初期はparがすべて負の値
        return k
    else:
        par[k] = find(par[k])
        return par[k]

def unite(i, j):
    i = find(i)
    j = find(j)
    if i==j:#すでに同じ集合
        return False
    else:
        #sizeが大きい方がi
        if i > j:#sizeはマイナス管理なので、i>jの時jの方がサイズが大きい
            i, j = j, i
        par[i] += par[j] #親は負の値で大きくなる
        par[j] = i
        return True

def size(x):
    return -par[find(x)]

n, m= map(int, input().split())
par = [-1]*n#初めは自分自身が親

for i in range(m):
    a, b = map(lambda x:int(x)-1, input().split())
    unite(a, b)

ans = 0
for i in range(n):
    ans = max(ans, -par[i])
print(ans)

Buggy submission (verdict: Runtime Error):
#unionfind木_

def find(k):#要素k
    if par[k] == k:
        return par[k]
    else:
        par[k] = find(par[k])
        return par[k]

def union(i, j):
    i = find(i)
    j = find(j)
    if i==j: return False
    else:
        if rank[i] == rank[j]:#ランクが同じ場合
            par[j] = par[i]
            rank[i] += 1
        if rank[j] > rank[i]:
            i, j = j, i
            par[i] = par[j]
        else:
            par[i] = par[j]

n, m= map(int, input().split())
par = list(range(n))#初めは自分自身が親
rank = [1]*n

for i in range(m):
    a, b = map(lambda x:int(x)-1, input().split())
    union(a, b)

ans = [0]*n
for i in range(n):
    ans[find(ini_list[i])]+=1
print(max(ans))

Buggy submission (verdict: Runtime Error):
#unionfind木_
class unionfind():
    def __init__(self, init_list, ide_ele=1):
        self.par = init_list #初めは自分自身が親
        self.rank = [ide_ele]*len(init_list)

    def find(self, k):#要素k
        if self.par[k] == k:
            return self.par[k]
        else:
            self.par[k] = self.find(self.par[k])
            return self.par[k]

    def union(self, i, j):
        i = self.find(i)
        j = self.find(j)
        if i==j: return False
        else:
            if self.rank[i] == self.rank[j]:#ランクが同じ場合
                self.par[j] = self.par[i]
                self.rank[i] += 1
            if self.rank[j] > self.rank[i]:
                i, j = j, i
                self.par[i] = self.par[j]
            else:
                self.par[i] = self.par[j]

n, m= map(int, input().split())
ini_list = list(range(n))
uni_tree = unionfind(ini_list)
for i in range(m):
    a, b = map(lambda x:int(x)-1, input().split())
    uni_tree.union(a, b)

ans = [0]*n
for i in range(n):
    ans[uni_tree.find(ini_list[i])]+=1
print(max(ans))
Problem p02614 (python)

Fixed submission:
h, w, k = map(int, input().split())
C = [list(input()) for i in range(h)]
ans = 0
for i in range(1<<h):
    for j in range(1<<w):
        cnt = 0
        for x in range(h):
            if (i>>x)&1 == 1:
                continue
            for y in range(w):
                if (j>>y)&1 == 1:
                    continue
                if C[x][y] == '#':
                    cnt += 1
        if cnt == k:
            ans += 1
print(ans)

Buggy submission (verdict: Runtime Error):
import itertools
import numpy as np

h, w, k = map(int, input().split())
f = [list(input()) for i in range(h)]
f = np.array(f)
ans = 0
for i in range(w+1):
    for j in range(h+1):
        for p in itertools.combinations(range(w), i):
            for q in itertools.combinations(range(h), j):
                g = f.copy()
                cnt = 0
                for r in q:
                    g[r] = 'r'
                for r in p:
                    g.T[r] = 'r'
                
                for s in range(w):
                    for t in range(h):
                        if g[t][s] == '#':
                            cnt +=1
                if cnt == k:
                    ans += 1
print(ans)

Buggy submission (verdict: Runtime Error):
import itertools
import numpy as np
import copy
h, w, k = map(int, input().split())
f = [list(input()) for i in range(h)]
f = np.array(f)
ans = 0
for i in range(w+1):
    for j in range(h+1):
        for p in itertools.combinations(range(w), i):
            for q in itertools.combinations(range(h), j):
                g = f.copy()
                cnt = 0
                for r in q:
                    g[r] = 'r'
                for r in p:
                    g.T[r] = 'r'
                
                for s in range(w):
                    for t in range(h):
                        if g[t][s] == '#':
                            cnt +=1
                if cnt == k:
                    ans += 1
print(ans)
Problem p03436 (python)

Fixed submission:
from collections import deque
inf = 1000000000
h, w = map(int, input().split())
root = [[-1]*w for i in range(h)]
root[0][0] = 1
s = [[] for i in range(h)]

cnt = 0
for i in range(h):
    s[i] = input()
    for j in range(w):
        if s[i][j] == '#': cnt+=1
dw = [-1, 0, 1, 0]
dh = [0, 1, 0, -1]
que = deque()
que.append([0, 0])
while len(que)>0:
    x, y = que.popleft()
    for dx, dy in zip(dw, dh):
        nx, ny = x+dx, y+dy
        if 0<=nx<w and 0<=ny<h and root[ny][nx]==-1 and s[ny][nx]=='.':
                que.append([nx, ny])
                root[ny][nx] = root[y][x] + 1
if root[h-1][w-1]<0:
    print(-1)
else:
    print(h*w - cnt - root[h-1][w-1])

Buggy submission (verdict: Runtime Error):
from collections import deque
inf = 1000000000
h, w = map(int, input().split())
root = [[-np.inf]*w for i in range(h)]
root[0][0] = 1
s = [[] for i in range(h)]

cnt = 0
for i in range(h):
    s[i] = input()
    for j in range(w):
        if s[i][j] == '#': cnt+=1
dw = [-1, 0, 1, 0]
dh = [0, 1, 0, -1]
que = deque()
que.append([0, 0])
while que:
    x, y = que.pop()
    for dx, dy in zip(dw, dh):
        nx, ny = x+dx, y+dy
        if 0<=nx<w and 0<=ny<h and root[ny][nx]<=0 and s[ny][nx]=='.':
                que.append([nx, ny])
                root[ny][nx] = root[y][x] + 1
if root[h-1][w-1]<0:
    print(-1)
else:
    print(h*w - cnt - root[h-1][w-1])

Buggy submission (verdict: Runtime Error):
import numpy as np
from collections import deque

h, w = map(int, input().split())
root = [[-np.inf]*w for i in range(h)]
root[0][0] = 1
s = [[] for i in range(h)]

cnt = 0
for i in range(h):
    s[i] = input()
    for j in range(w):
        if s[i][j] == '#': cnt+=1
dw = [-1, 0, 1, 0]
dh = [0, 1, 0, -1]
que = deque()
que.append([0, 0])
while que:
    x, y = que.pop()
    for dx, dy in zip(dw, dh):
        nx, ny = x+dx, y+dy
        if 0<=nx<w and 0<=ny<h and root[ny][nx]<=0 and s[ny][nx]=='.':
                que.append([nx, ny])
                root[ny][nx] = root[y][x] + 1
if root[h-1][w-1]<0:
    print(-1)
else:
    print(h*w - cnt - root[h-1][w-1])
Problem p02582 (python)

Fixed submission:
S = input()
if S == 'SSS':
  a = 0
if S == 'SSR':
  a = 1
if S == 'SRS':
  a = 1
if S == 'SRR':
  a = 2
if S == 'RSS':
  a = 1
if S == 'RSR':
  a = 1
if S == 'RRS':
  a = 2
if S == 'RRR':
  a = 3
print(a)

Buggy submission (verdict: Runtime Error):
S = input()
if S = 'SSS':
  a = 0
if S = 'SSR':
  a = 1
if S = 'SRS':
  a = 1
if S = 'SRR':
  a = 2
if S = 'RSS':
  a = 1
if S = 'RSR':
  a = 1
if S = 'RRS':
  a = 2
if S = 'RRR':
  a = 3
print(a)
Problem p02609 (python)

Fixed submission:
def p(x):
  return bin(x).count('1')

def f(x):
  if x == 0:
    return 0
  return f(x%p(x)) + 1

N = int(input())
X = input()
cnt = X.count('1')
x = int(X,2)
a = x % (cnt + 1)
if cnt != 1:
  b = x % (cnt - 1)

for i in range(N):
  if X[i] == '0':
    k = a
    k += pow(2,N-i-1,cnt+1)
    k %= cnt+1
  else:
    if cnt == 1:
      print(0)
      continue
    k = b
    k -= pow(2,N-i-1,cnt-1) 
    k %= cnt-1  
  print(f(k)+1)

Buggy submission (verdict: Runtime Error):
def p(x):
  a = list(bin(x))
  return a.count('1')

def f(x):
  if x == 0:
    return 0
  return f(x%p(x)) + 1

N = int(input())
X = input()
##最初の操作だけこっちでやっちゃう
cnt = X.count('1')
x = int(X,2)
a = x % (cnt + 1)
b = x % (cnt - 1)

t = [0] * N ##tは一回X[i]をp(X[i])で割った余りで置き換えた配列
for i in range(N):
  if X[i] == '0':
    t[i] = (a + 2**(N-i-1) % (cnt + 1)) % (cnt + 1)
  if X[i] == '1':
    if cnt == 1:
      t[i] = 0
      continue
    t[i] = (b - 2**(N-i-1) % (cnt - 1)) % (cnt - 1)  
  

result = []
for j in range(N):
  result.append(1 + f(t[j]))
print(*result, sep='\n')

Buggy submission (verdict: Runtime Error):
def f(x):
  if p(x) == 0:
    return 0
  if x % p(x) == 0:
    return 1
  return f(x%p(x)) + 1

def p(x):
  a = list(bin(x))
  return a.count('1')
  
N = int(input())
X = input()
##最初の操作だけこっちでやっちゃう
cnt = X.count('1')
x = int(X,2)
a = x % (cnt + 1)
b = x % (cnt - 1)

t = [0] * N ##tは一回X[i]をp(X[i])で割った余りで置き換えた配列
for i in range(N):
  if X[i] == '0':
    t[i] = (a + 2**i % (cnt + 1)) % (cnt + 1)
  if X[i] == '1':
    t[i] = (b - 2**i % (cnt - 1)) % (cnt - 1)  
  

result = []
for j in range(N):
  result.append(1 + f(t[j]))
print(*result, sep='\n')

Buggy submission (verdict: Runtime Error):
def f(x):
  if x % p(x) == 0:
    return 1
  return f(x%p(x)) + 1

def p(x):
  a = list(bin(x))
  return a.count('1')
  
N = int(input())
X = input()
A = [0] * N
for i in range(N):
  if X[i] == '1':
    A[i] = int(X,2) - 2**(N-i-1)
  else:
    A[i] = int(X,2) + 2**(N-i-1)
for j in A:
  print(f(j))
Problem p02615 (python)

Fixed submission:
N = int(input())
a = [int(i) for i in input().split()]
a.sort(reverse = True)
if N == 3:
  print(a[0] + a[1])
ans1 = 2*sum(a[0:N//2]) - a[0]
if N%2 == 0:
  print(ans1)
if N%2 == 1 and N != 3:
  print(ans1 + a[N//2])

Buggy submission (verdict: Runtime Error):
def sum(a,N):
  if N == 1:
    return a[0]
  return a[N-1] +  sum(a,N-1)

N = int(input())
a = [int(i) for i in input().split()]
a.sort(reverse = True)
Sum_1 = 2 * sum(a,N//2) - a[0]
if N%2 == 0:
  print(Sum_1)
if N%2 == 1:
  print(Sum_1 + a[(N+1)//2])
Problem p02536 (python)

Fixed submission:
# https://atcoder.jp/contests/atc001/tasks/unionfind_a
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]
    
    def groups(self):
        groups = dict()
        for i in range(self.n):
            p = self.find(i)
            if not groups.get(p):
                groups[p] = set()
            groups[p].add(i)
        return list(groups.values())

def main():
    n,m = map(int, input().split())
    uf = UnionFind(n)
    for _ in range(m):
        a,b = map(lambda x: int(x) -1, input().split())
        uf.union(a,b)
    print(len(uf.groups())-1)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
# https://atcoder.jp/contests/atc001/tasks/unionfind_a
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

def main():
    n,m = map(int, input().split())
    uf = UnionFind(n)
    for _ in range(m):
        a,b = map(lambda x: int(x) -1, input().split())
        uf.union(a,b)
    print(len(uf.groups())-1)

if __name__ == '__main__':
    main()
Problem p02945 (python)

Fixed submission:
d = raw_input().split()
a, b = d[0], d[1]
operators = ["+", "-", "*"]
current = None
soFar = None
for i in range(len(operators)):
    current = eval(a + operators[i] + b)
    soFar = max(current, soFar)
print(soFar)

Buggy submission (verdict: Runtime Error):
a = int(input())
b = int(input())
add = a+b
minus = a-b
multi = a*b
print(max(add,minus, multi))
Problem p02999 (python)

Fixed submission:
n = list(raw_input())
x, a = int(n[0]), int(n[2])
if x >= a:
    print(10)
else:
    print(0)

Buggy submission (verdict: Runtime Error):
n = list(raw_input())
x, a = int(n[0]), int(n[1])
if x >= a:
    print(10)
else:
    print(0)
Problem p03000 (python)

Fixed submission:
n = raw_input()
input = n.split(" ")
n, x = input[0], input[1]
l = raw_input().split(" ")
current = 0
bounce = 1
for i in range(len(l)):
    current += int(l[i])
    if current <= int(x):
        bounce += 1
    else:
        break
print(bounce)

Buggy submission (verdict: Runtime Error):
n = raw_input()
input = n.split(" ")
n, x = input[0], input[1]
l = raw_input().split(" ")
current = 0
bounce = 1
for i in range(len(l)):
    current += int(l[i])
    if current <= int(x):
        bounce += 1
    else:
        break
print(bounce)
Problem p03261 (python)

Fixed submission:
n = int(input())
w = [input() for i in range(n)]
count = 0
for i in range(1, n):
    if w[i - 1][len(w[i - 1]) - 1] != w[i][0]:
        count += 1
if count == 0 and len(list(set(w))) == n:
    print('Yes')
else:
    print('No')

Buggy submission (verdict: Runtime Error):
n = int(input())
soFar = []
last = None
flag = True
for i in range(n + 1):
    x = input()
    if i == 0:
        soFar.append(x)
        last = x
    else:
        if x not in soFar and last[len(last) - 1] == x[0]:
            soFar.append(x)
            last = x
        else:
            flag = False
            break
if flag:
    print('Yes')
else:
    print('No')
Problem p03545 (python)

Fixed submission:
n = list(raw_input())
operators = ["+", "-"]
total = None
for op1 in operators:
    for op2 in operators:
        for op3 in operators:
            total = n[0] + op1 + n[1] + op2 + n[2] + op3 + n[3]
            if eval(total) == 7:
                total = total + "=7"
                print(total)
                exit(0)

Buggy submission (verdict: Runtime Error):
n = input()
op_cnt = len(n) - 1  # すき間の個数
for i in range(2 ** op_cnt):
    op = ["-"] * op_cnt  # あらかじめ ["-", "-", "-"] というリストを作っておく
    for j in range(op_cnt):
        if ((i >> j) & 1):
            op[op_cnt - 1 - j] = "+"  # フラグが立っていた箇所を "+" で上書き

    formula = ""
    for p_n, p_o in zip(n, op + [""]):  # 少々モヤッとする
        formula += (p_n + p_o)
    if eval(formula) == 7:
        print(formula + "=7")
        break
Problem p04043 (python)

Fixed submission:
from collections import defaultdict
strings = raw_input().split()
# a, b, c = int(strings[0]), int(strings[1]), int(strings[2])
cand = {
    '5': 2,
    '7': 1
}
source = defaultdict(int)
for i in range(len(strings)):
    if strings[i] == '5':
        if source[strings[i]]:
            source[strings[i]] += 1
        else:
            source[strings[i]] = 1
    elif strings[i] == '7':
        if source[strings[i]]:
            source[strings[i]] += 1
        else:
            source[strings[i]] = 1
if source == cand:
    print "YES"
else:
    print "NO"

Buggy submission (verdict: Runtime Error):
strings = raw_input().split()
cand = [5, 7, 5]

for i in range(len(cand)):
    for j in range(len(strings)):
        if cand[i] == int(strings[j]):
            cand.pop(i)
print(len(cand)==0)
Problem p03494 (python)

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

ans = 0

while True:
	for i in range(N):
		if A[i] % 2 != 0:
			print(ans)
			exit()
		A[i] //= 2
	ans += 1

Buggy submission (verdict: Runtime Error):
N = int(input())
A = list(map(int, input().split()))
ans = 0
while 1:
  if A.all(0): break
  for i in A: i //= 2
print(ans)
Problem p04016 (python)

Fixed submission:
import math

N = int(input())
S = int(input())

def f(B, N):
    X, res = N, 0
    while 0 < X:
        res += X % B
        X //= B
    return res


if N == S:
    print(N + 1)
    exit()

i = 2
while i * i <= N:
    if f(i, N) == S:
        print(i)
        exit()
    i += 1

sqrtN = math.floor(math.sqrt(N))
i = 1
ans = []
while i * i < N:
    if (N - S) % i != 0:
        i += 1
        continue
    b = (N - S) // i + 1
    if sqrtN < b and f(b, N) == S:
        ans.append(b)
    i += 1

print(-1 if len(ans) == 0 else min(ans))

Buggy submission (verdict: Runtime Error):
N = int(input())
S = int(input())

def f(B, N):
    X, res = N, 0
    while 0 < X:
        res += X % B
        X //= B
    return res


if N == S:
    print(N + 1)
    exit()

for i in range(2, N):
    if N < i * i: break
    if f(i, N) == S:
        print(i)
        exit()

for i in range(1, N):
    if N < i * i: break
    b = (N - S) // i + 1
    if f(b, N) == S:
        print(b)
        exit()

print(-1)
Problem p02855 (python)

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

cake = [list(input()) for _ in range(H)]
ans = [[0] * W for _ in range(H)]

id = 1
sign = 0
nothing = 0

for i in range(H):
    if cake[i].count('#') == 0:
        nothing += 1
        if i == H-1:
            for j in range(W):
                for d in range(nothing):
                    ans[i-d][j] = ans[i-nothing][j]
        continue
    for j in range(W):
        if cake[i][j] == '#':
            ans[i][sign:j+1] = [id] * (j+1-sign)
            sign = j+1
            id += 1
        elif j == W-1:
            ans[i][sign:j+1] = [id-1] * (j+1-sign)
    if nothing != 0:
        for j in range(W):
            for d in range(1,nothing+1):
                ans[i-d][j] = ans[i][j]
    sign = 0
    nothing = 0

for i in range(H):
    for j in range(W):
        print('{} '.format(ans[i][j]), end='')
    print()

Buggy submission (verdict: Runtime Error):
H, W, K = map(int, input().split())

cake = [list(input().split()) for _ in range(H)]
ans = [[0] * W for _ in range(H)]

id = 1
sign = 0

for i in range(H):
    for j in range(W):
        if cake[i][j] == '#':
            ans[i][sign:j+1] = [id] * (j+1-sign)
            sign = j+1
            id += 1
            #print(ans[i])
        elif j == W-1:
            ans[i][sign:j+1] = [id-1] * (j+1-sign)
    sign = 0

for i in range(H):
    for j in range(W):
        print('{} '.format(ans[i][j]), end='')
    print()

Buggy submission (verdict: Runtime Error):
H, W, K = map(int, input().split())

cake = [list(input()) for _ in range(H)]
ans = [[0] * W for _ in range(H)]

id = 1
sign = 0

for i in range(H):
    for j in range(W):
        if cake[i][j] == '#':
            ans[i][sign:j+1] = [id] * (j+1-sign)
            sign = j+1
            id += 1
        elif j == W-1:
            ans[i][sign:j+1] = [id-1] * (j+1-sign)
    sign = 0

for i in range(H):
    for j in range(W):
        print(f'{ans[i][j]} ', end='')
    print()

Buggy submission (verdict: Runtime Error):
H, W, K = map(int, input().split())

cake = [list(input()) for _ range(H)]

print("1 3 4")
print("1 3 4")
print("2 3 5")

Buggy submission (verdict: Runtime Error):

_N = int(input())
S = input()

open_parenthesis = 0
left = 0
right = 0

for s in S:
    if s == '(':
        open_parenthesis += 1
    elif s == ')':
        if open_parenthesis != 0:
            open_parenthesis -= 1
        else:
            left += 1

right = open_parenthesis

ans = '(' * left + S + ')' * right

print(ans)
Problem p03006 (python)

Fixed submission:
N = int(input())

pos = []
for _ in range(N):
    tmp = tuple(map(int, input().split()))
    pos.append(tmp)

pos.sort()

dic = dict()
for i in range(N):
    for j in range(i+1,N):
        distance = (pos[j][0]-pos[i][0], pos[j][1]-pos[i][1])
        #print(f'{distance=}')
        if distance in dic:
            dic[distance] += 1
        else:
            dic[distance] = 1
if dic :
    max_frenquency = max(dic.values())

    print(N-max_frenquency)
else:
    print(N)

Buggy submission (verdict: Runtime Error):
N = int(input())

pos = []
for _ in range(N):
    tmp = tuple(map(int, input().split()))
    pos.append(tmp)

pos.sort()

dic = dict()
for i in range(N):
    for j in range(i+1,N):
        distance = (pos[j][0]-pos[i][0], pos[j][1]-pos[i][1])
        #print(f'{distance=}')
        if distance in dic:
            dic[distance] += 1
        else:
            dic[distance] = 1

max_frenquency = max(dic.values())

print(N-max_frenquency)
Problem p03160 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10 ** 7)

def solve(index: int, dp: list, footing: list) -> int:
    if dp[index] != -1:
        return dp[index]

    if index == 0:
        return 0
    elif index == 1:
        return abs(footing[1]-footing[0])

    tmp = min(solve(index-1, dp, footing)+abs(footing[index]-footing[index-1]), solve(index-2, dp, footing)+abs(footing[index]-footing[index-2]))
    #print(f'{tmp=}')
    dp[index] = tmp
    return tmp

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

dp = [-1] * N

ans = solve(N-1, dp, footing)

print(ans)

Buggy submission (verdict: Runtime Error):

def solve(index: int, dp: list, footing: list) -> int:
    if dp[index] != -1:
        return dp[index]

    if index == 0:
        return 0
    elif index == 1:
        return abs(footing[1]-footing[0])

    tmp = min(solve(index-1, dp, footing)+abs(footing[index]-footing[index-1]), solve(index-2, dp, footing)+abs(footing[index]-footing[index-2]))
    #print(f'{tmp=}')
    dp[index] = tmp
    return tmp

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

dp = [-1] * N

ans = solve(N-1, dp, footing)

print(ans)
Problem p03166 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10 ** 7)

def search(index: int, dp: list, graph: list) -> int:
    if dp[index] != -1:
        return dp[index]+1

    tmp = 0
    for d in graph[index]:
        tmp = max(tmp, search(d, dp, graph))

    dp[index] = tmp
    return tmp+1


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

graph = [ [] for _ in range(N)]
dp = [-1 for _ in range(N)]

for _ in range(M):
    x, y = map(int, input().split())
    graph[x-1].append(y-1)

ans = 0
for i in range(N):
    ans = max(ans, search(i, dp, graph))

print(ans-1)

Buggy submission (verdict: Runtime Error):

def search(index: int, dp: list, graph: list) -> int:
    if dp[index] != -1:
        return dp[index]+1

    tmp = 0
    for d in graph[index]:
        tmp = max(tmp, search(d, dp, graph))

    dp[index] = tmp
    return tmp+1


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

graph = [ [] for _ in range(N)]
dp = [-1 for _ in range(N)]

for _ in range(M):
    x, y = map(int, input().split())
    graph[x-1].append(y-1)

ans = 0
for i in range(N):
    ans = max(ans, search(i, dp, graph))

print(ans-1)