Problem p03166 (python)

Fixed submission:
import sys
sys.setrecursionlimit(200000)
N, M = map(int, input().split())
G = [[] for _ in range(N+1)]
MEMO = [-1 for _ in range(N+1)]

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


def dp_LP(v):# 頂点nから始まる最長経路を返す.
  if MEMO[v] != -1:
    return MEMO[v]
  else:
    ans = 0
    for i in G[v]:
      ans = max(ans, dp_LP(i)+1)
    MEMO[v] = ans

    return ans



answer = 0
for n in range(1,N+1):
  answer = max(answer, dp_LP(n))
print(answer)

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())
G = [[] for _ in range(N+1)]
MEMO = [-1 for _ in range(N+1)]

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


def dp_LP(v):# 頂点nから始まる最長経路を返す.
  if MEMO[v] != -1:
    return MEMO[v]
  else:
    ans = 0
    for i in G[v]:
      ans = max(ans, dp_LP(i)+1)
    MEMO[v] = ans

    return ans



answer = 0
for n in range(1,N+1):
  answer = max(answer, dp_LP(n))
print(answer)

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())
G = [[] for _ in range(N+1)]
MEMO = [-1 for _ in range(N+1)]

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


def dp_LP(v):# 頂点nから始まる最長経路を返す.
  if MEMO[v] != -1:
    return MEMO[v]
  else:
    ans = 0
    for i in G[v]:
      ans = max(ans, dp_LP(i)+1)
    MEMO[v] = ans

    return ans



answer = 0
for n in range(1,N+1):
  answer = max(answer, dp_LP(n))
print(answer)

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())
Grapeee = {j+1:[] for j in range(N)}
memo = {j+1: 0 for j in range(N)} # 各頂点からの最長経路をメモ. -1で初期化.
doneee = {j+1: False for j in range(N)}
ans = 0

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

#print(memo)
#print(done)
def dp(v):# 頂点nから始まる最長経路を返す.
  if doneee[v]: 
    return memo[v]
  elif doneee[v] == False:
    answ = 0
    for to in Grapeee[v]:
      answ = max(answ, dp(to)+1)
    memo[v] = answ
    doneee[v] = True
    #print(memo)
    #print(done)
    return answ

for n in range(1,N+1):
  #print(dp(n))
  ans = max(ans, dp(n))
print(ans)

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())
Grap = {j+1:[] for j in range(N)}
memo = {j+1: 0 for j in range(N)} # 各頂点からの最長経路をメモ. -1で初期化.
done = {j+1: False for j in range(N)}
ans = 0

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

#print(memo)
#print(done)
def dp(v):# 頂点nから始まる最長経路を返す.
  if done[v]: 
    return memo[v]
  elif done[v] == False:
    answ = 0
    for to in Grap[v]:
      answ = max(answ, dp(to)+1)
    memo[v] = answ
    done[v] = True
    #print(memo)
    #print(done)
    return answ

for n in range(1,N+1):
  #print(dp(n))
  ans = max(ans, dp(n))
print(ans)

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())
Grap = {j+1:[] for j in range(N)}
memo = {j+1: 0 for j in range(N)} # 各頂点からの最長経路をメモ. -1で初期化.
done = {j+1: False for j in range(N)}
ans = 0

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


def dp(v):# 頂点nから始まる最長経路を返す.
  if done[v]: 
    return memo[v]
  elif done[v] == True:
    answ = 0
    for to in Grap[v]:
      answ = max(answ, dp(to)+1)
    memo[v] = answ
    done[v] = True
    return answ

for n in range(1,N+1):
  ans = max(ans, dp(n))
print(ans)

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())
Grap = {j+1:[] for j in range(N)}
memo = {j+1: -1 for j in range(N)} # 各頂点からの最長経路をメモ. -1で初期化.
ans = 0

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


def dp(v):# 頂点nから始まる最長経路を返す.
  if memo[v] != -1: 
    return memo[v]
  else:
    answ = 0
    for to in Grap[v]:
      answ = max(answ, dp(to)+1)
    memo[v] = answ
    return answ

for n in range(1,N+1):
  ans = max(ans, dp(n))
print(ans)

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())
Grap = {j+1:[] for j in range(N)}
memo = {j+1: -1 for j in range(N)} # 各頂点からの最長経路をメモ. -1で初期化.
ans = 0

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


def dp(v):# 頂点nから始まる最長経路を返す.
  if memo[v] != -1: 
    return memo[v]
  else:
    answ = 0
    for to in Grap[v]:
      answ = max(answ, dp(to)+1)
    memo[v] = answ
    return answ

for n in range(1,N+1):
  ans = max(ans, dp(n))
print(ans)

Buggy submission (verdict: Runtime Error):
def dp(v):# 頂点nから始まる最長経路を返す.
  if memo[v] != -1: return memo[v]
  answ = 0

  for to in Grap[v]:
    answ = max(answ, dp(to)+1)
  memo[v] = answ
  return answ


N, M = map(int, input().split())
Grap = {j+1:[] for j in range(N)}
memo = {j+1: -1 for j in range(N+5)} # 各頂点からの最長経路をメモ. -1で初期化.

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

ans = 0
for n in range(1,N+1):
  ans = max(ans, dp(n))
print(ans)

Buggy submission (verdict: Runtime Error):
def dp(v):# 頂点nから始まる最長経路を返す.
  if memo[v] != -1: return memo[v]
  to_list = Grap[v]
  answ = 0

  for to in to_list:
    answ = max(answ, dp(to)+1)
  memo[v] = answ
  return answ


N, M = map(int, input().split())
Grap = {j+1:[] for j in range(N)}
memo = {j+1: -1 for j in range(N)} # 各頂点からの最長経路をメモ. -1で初期化.

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

ans = 0
for n in range(1,N+1):
  ans = max(ans, dp(n))
print(ans)

Buggy submission (verdict: Runtime Error):
def dp(v):# 頂点nから始まる最長経路を返す.
  if memo[v] != -1: return memo[v]
  to_list = Graph[v]
  answ = 0

  for to in to_list:
    answ = max(answ, dp(to)+1)
  memo[v] = answ
  return answ


N, M = map(int, input().split())
Graph = {j+1:[] for j in range(N)}
memo = {j+1: -1 for j in range(N)} # 各頂点からの最長経路をメモ. -1で初期化.

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

ans = 0
for n in range(1,N+1):
  ans = max(ans, dp(n))
print(ans)

Buggy submission (verdict: Runtime Error):
def dp(v):# 頂点nから始まる最長経路を返す.
  if memo[v] != -1: return memo[v]
  to_list = Graph[v]
  answ = 0

  for i in to_list:
    answ = max(answ, dp(i)+1)
  memo[v] = answ
  return answ


N, M = map(int, input().split())
Graph = {j+1:[] for j in range(N)}
memo = [-1] * (N+1) # 各頂点からの最長経路をメモ. -1で初期化.

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

ans = 0
for n in range(1,N+1):
  ans = max(ans, dp(n))
print(ans)

Buggy submission (verdict: Runtime Error):
def dp(n):# 頂点nから始まる最長経路を返す.
  if memo[n] != -1: return memo[n]
  to_list = Graph[n]
  answ = 0

  for i in to_list:
    answ = max(answ, dp(i)+1)
  memo[n] = answ
  return answ


N, M = map(int, input().split())
Graph = {n+1:[] for n in range(N)}
memo = [-1] * (N+1) # 各頂点からの最長経路をメモ. -1で初期化.

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

ans = 0
for n in range(1,N+1):

  ans = max(ans, dp(n))
print(ans)