Last active
April 7, 2024 05:17
-
-
Save maehrm/a759b24dcffd509381aae9a2fc879725 to your computer and use it in GitHub Desktop.
G - Longest Path https://atcoder.jp/contests/dp/tasks/dp_g
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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