Skip to content

Instantly share code, notes, and snippets.

@maehrm
Last active April 7, 2024 05:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maehrm/a759b24dcffd509381aae9a2fc879725 to your computer and use it in GitHub Desktop.
Save maehrm/a759b24dcffd509381aae9a2fc879725 to your computer and use it in GitHub Desktop.
import sys
sys.setrecursionlimit(10**7)
def dfs(v):
if dp[v] != -1: # 調査済み
return dp[v]
ret = 0
for nxt in G[v]:
ret = max(ret, dfs(nxt) + 1)
dp[v] = ret
return ret
N, M = map(int, input().split())
G = [[] * N for _ in range(N)]
for _ in range(M):
x, y = map(lambda v: int(v) - 1, input().split())
G[x].append(y)
dp = [-1] * N
ans = 0
for v in range(N):
ans = max(ans, dfs(v))
print(ans)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment