Problem:


Score : 100 points

Problem Statement

There is a directed graph G with N vertices and M edges.
The vertices are numbered 1, 2, \ldots, N, and for each i (1 \leq i \leq M), the i-th directed edge goes from Vertex x_i to y_i.
G does not contain directed cycles.

Find the length of the longest directed path in G.
Here, the length of a directed path is the number of edges in it.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq x_i, y_i \leq N

All pairs (x_i, y_i) are distinct.

G does not contain directed cycles.

Input

Input is given from Standard Input in the following format:

N M
x_1 y_1
x_2 y_2
:
x_M y_M

Output

Print the length of the longest directed path in G.

Sample Input 1

4 5
1 2
1 3
3 2
2 4
3 4

Sample Output 1

3

The red directed path in the following figure is the longest:

Sample Input 2

6 3
2 3
4 5
5 6

Sample Output 2

2

The red directed path in the following figure is the longest:

Sample Input 3

5 8
5 3
2 3
2 4
5 2
5 1
1 4
4 3
1 3

Sample Output 3

3

The red directed path in the following figure is one of the longest:



Sample input:
4 5
1 2
1 3
3 2
2 4
3 4


Sample output:
3

Submission status: Runtime Error

Solution code:
def dfs(dp,edges,node,visited):
    if node not in visited:
        visited.add(node)
        for kid in edges[node]:
            dfs(dp,edges,kid,visited)
            #print(dp,node,kid)
            dp[node-1] = max(dp[node-1],dp[kid-1])
        if edges[node]:
            dp[node-1] += 1

def solve(edges,n):
    dp = [0]*n
    visited = set()
    for i in range(1,n+1):
        if i not in visited:
            #print(i)
            dfs(dp,edges,i,visited)

    #print(dp)
    print(max(dp))

def main():
    n,m = map(int,input().split())
    edges = {}
    for i in range(1,n+1):
        edges[i] = []

    for i in range(m):
        x,y = map(int,input().split())
        edges[x].append(y)

    solve(edges,n)


main()

Submission status: Runtime Error

Solution code:
def solve():
    n, list_of_xy = read()
    result = think(n, list_of_xy)
    write(result)


def read():
    n, m = read_int(2)
    list_of_xy = []
    for i in range(m):
        list_of_xy.append(read_int(2))
    return n, list_of_xy


def read_int(n):
    return list(map(lambda x: int(x), read_line().split(' ')))[:n]


def read_line(n=0):
    if n == 0:
        return input().rstrip()
    else:
        return input().rstrip()[:n]


def think(n, list_of_xy):
    num_of_input_edges = [0 for _ in range(n + 1)]
    num_of_output_edges = [0 for _ in range(n + 1)]

    linked_list = generate_linked_list(list_of_xy)

    invalid_value = -1
    length_of_longest_path_start_from = [invalid_value for _ in range(n + 1)]

    for x, y in list_of_xy:
        num_of_input_edges[y] += 1
        num_of_output_edges[x] += 1

    for i in range(1, len(num_of_input_edges)):
        if num_of_output_edges[i] == 0:
            length_of_longest_path_start_from[i] = 0

    for i in range(1, len(num_of_input_edges)):
        if num_of_input_edges[i] == 0:
            update_logest_path_start_from(i, linked_list, length_of_longest_path_start_from, invalid_value)

    return max(length_of_longest_path_start_from)


def generate_linked_list(list_of_xy):
    linked_list = {}
    for x, y in list_of_xy:
        if x in linked_list:
            linked_list[x].append(y)
        else:
            linked_list[x] = [y]
    return linked_list


def update_logest_path_start_from(x, linked_list, length_of_longest_path_start_from, invalid_value):
    if length_of_longest_path_start_from[x] != invalid_value:
        return

    dest_from_x = linked_list.get(x, [])
    for y in dest_from_x:
        if length_of_longest_path_start_from[y] == invalid_value:
            update_logest_path_start_from(y, linked_list, length_of_longest_path_start_from, invalid_value)
            length_of_longest_path_start_from[x] = max(length_of_longest_path_start_from[x], length_of_longest_path_start_from[y] + 1)
        else:
            length_of_longest_path_start_from[x] = max(length_of_longest_path_start_from[x], length_of_longest_path_start_from[y] + 1)


def write(result):
    print(result)


if __name__ == '__main__':
    solve()

Submission status: Runtime Error

Solution code:
def solve():
    n, list_of_xy = read()
    result = think(n, list_of_xy)
    write(result)


def read():
    n, m = read_int(2)
    list_of_xy = []
    for i in range(m):
        list_of_xy.append(read_int(2))
    return n, list_of_xy


def read_int(n):
    return list(map(lambda x: int(x), read_line().split(' ')))[:n]


def read_line(n=0):
    if n == 0:
        return input().rstrip()
    else:
        return input().rstrip()[:n]


def think(n, list_of_xy):
    num_of_input_edges = [0 for _ in range(n + 1)]
    num_of_output_edges = [0 for _ in range(n + 1)]

    linked_list = generate_linked_list(list_of_xy)

    invalid_value = -1
    length_of_longest_path_start_from = [invalid_value for _ in range(n + 1)]

    for x, y in list_of_xy:
        num_of_input_edges[y] += 1
        num_of_output_edges[x] += 1

    for i in range(1, len(num_of_input_edges)):
        if num_of_output_edges[i] == 0:
            length_of_longest_path_start_from[i] = 0

    for i in range(1, len(num_of_input_edges)):
        if num_of_input_edges[i] == 0:
            update_logest_path_start_from(i, linked_list, length_of_longest_path_start_from, invalid_value)

    return max(length_of_longest_path_start_from)


def generate_linked_list(list_of_xy):
    linked_list = {}
    for x, y in list_of_xy:
        if x in linked_list:
            linked_list[x].append(y)
        else:
            linked_list[x] = [y]
    return linked_list


def update_logest_path_start_from(x, linked_list, length_of_longest_path_start_from, invalid_value):
    if length_of_longest_path_start_from[x] != invalid_value:
        return

    dest_from_x = linked_list[x]
    for y in dest_from_x:
        if length_of_longest_path_start_from[y] == invalid_value:
            update_logest_path_start_from(y, linked_list, length_of_longest_path_start_from, invalid_value)
            length_of_longest_path_start_from[x] = max(length_of_longest_path_start_from[x], length_of_longest_path_start_from[y] + 1)
        else:
            length_of_longest_path_start_from[x] = max(length_of_longest_path_start_from[x], length_of_longest_path_start_from[y] + 1)


def write(result):
    print(result)


if __name__ == '__main__':
    solve()

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
l=[[] for i in range(n)]
for i in range(m):
    p=[int(j) for j in input().split()]
    l[p[1]-1].append(p[0]-1)

dp=[-1 for i in range(n+1)]

def f(v):
    if dp[v]!=-1:
        return dp[v]
    p=0
    for i in l[v]:
        p=max(p,f(i)+1)
    dp[v]=p
    return dp[v]

for i in range(n):
    f(i)

print(max(dp))

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
l=[[] for i in range(n)]
for i in range(m):
    p=[int(j) for j in input().split()]
    l[p[1]-1].append(p[0]-1)

dp=[-1 for i in range(n+1)]
from functools import lru_cache
@lru_cache(maxsize=1000000)
def f(v):
    if dp[v]!=-1:
        return dp[v]
    p=0
    for i in l[v]:
        p=max(p,f(i)+1)
    dp[v]=p
    return dp[v]

for i in range(n):
    f(i)

print(max(dp))

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
l=[[] for i in range(n)]
for i in range(m):
    p=[int(j) for j in input().split()]
    l[p[1]-1].append(p[0]-1)

dp=[-1 for i in range(n+1)]
from functools import lru_cache
@lru_cache(maxsize=1000000)
def f(n):
    if dp[n]!=-1:
        return dp[n]
    p=0
    for i in l[n]:
        p=max(p,f(i)+1)
    dp[n]=p
    return dp[n]

for i in range(n):
    f(i)

print(max(dp))

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
l=[[] for i in range(n)]
for i in range(m):
    p=[int(j) for j in input().split()]
    l[p[1]-1].append(p[0]-1)

dp=[0 for i in range(n)]
from functools import lru_cache
@lru_cache(maxsize=1000000)
def f(n):
    if dp[n]!=0:
        return dp[n]
    for i in l[n]:
        dp[n]=max(dp[n],f(i)+1)
    return dp[n]

for i in range(n):
    f(i)

print(max(dp))

Submission status: Runtime Error

Solution code:
from collections import defaultdict, deque
import sys

input = sys.stdin.readline

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

es = [[int(x) for x in input().split()] for _ in range(M)]

outs = defaultdict(list)
ins = defaultdict(list)
cnt = defaultdict(int)

for v1, v2 in es:
    outs[v1].append(v2)
    cnt[v2] += 1
    ins[v2].append(v1)

dp = [0 for _ in range(N)]
dp[0] = 0
ts = []

for v1 in range(1, N+1):
    if cnt[v1] == 0:   
        dp[ts.index(v1)] = 0 
        q = deque(v1)

while q:
    v1 = q.popleft()
    ts.append(v1)
    
    for v2 in outs[v1]:
        cnt[v2] -= 1
        dp[ts.index(v2)] = dp[ts.index(v1)] + 1
        if cnt[v2] == 0:
            q.append(v2)

print(dp[-1])

Submission status: Runtime Error

Solution code:
import sys

sys.setrecursionlimit(10 ** 5 * 2)
input = sys.stdin.readline()
N, M = map(int, input().split())
Ps = [[] for _ in range(N)]
for i in range(M):
    x, y = map(int, input().split())
    Ps[x - 1].append(y - 1)

mem = [-1] * N


def rec(n):
    if mem[n] > -1:
        return mem[n]
    elif len(Ps[n]) == 0:
        mem[n] = 0
        return mem[n]
    else:
        mem[n] = max([rec(Ps[n][i]) for i in range(len(Ps[n]))]) + 1
        return mem[n]


for i in range(N):
    rec(i)
print(max(mem))

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
Ps = [[] for _ in range(N)]
for i in range(M):
    x, y = map(int, input().split())
    Ps[x - 1].append(y - 1)

mem = [-1] * N


def rec(n):
    if mem[n] > -1:
        return mem[n]
    elif len(Ps[n]) == 0:
        mem[n] = 0
        return mem[n]
    else:
        mem[n] = max([rec(Ps[n][i]) for i in range(len(Ps[n]))]) + 1
        return mem[n]


for i in range(N):
    rec(i)
print(max(mem))

Submission status: Runtime Error

Solution code:
def getMax(d,n,c):
    global bs
    if len(d[n])==0:
        bs=max(bs,c)

    for k in d[n]:
        getMax(d,k,c+1)

n,m=map(int,input().split())
d={}
for j in range(1,n+1):
    d[j]=[]
for i in range(m):
    a,b=map(int,input().split())
    d[a].append(b)
bs=-1
for i in range(1,n+1):
    c=0
    getMax(d,i,c)
print(bs)

Submission status: Runtime Error

Solution code:
dp=[]
G=[]
def rec(v):
  if dp[v]!=-1:
    return dp[v]
  res=0
  for i in G[v]:
    s=rec(i)
    if res<s+1:
      res=s+1
  dp[v]=res
  return res

n,m=map(int,input().split())

for i in range(n):
  G.append([])
  dp.append(-1)

for i in range(m):
  x,y=map(int,input().split())
  G[x-1].append(y-1)

res=0
for i in range(n):
  r=rec(i)
  if res<r:
    res=r
print(res)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
#dp6 #Longest-Path

import sys
def LI(): return list(map(int,sys.stdin.readline().split()))
def LIR(n): return [LI() for _ in range(n)]
def g():
    def dfs(x):
        #既に最長パスがわかっている頂点はそれを返す
        if dp[x] != None:
            return dp[x]
        #移動できる頂点がないとき0
        if len(Graph[x]) == 0:
            dp[x] = 0
            return 0
        else:
            res = 0
            #移動できる頂点の中で最大パスをもつものを探す
            for y in Graph[x]:
                res = max(res,dfs(y)+1)
            dp[x] = res
            return dp[x]

    n,m = LI()
    #dpで各頂点を始点とした最大パスを管理
    dp = [None for _ in range(n)]
    Graph = [[] for _ in range(n)]
    for _ in range(m):
        x,y = LI()
        Graph[x-1].append(y-1)
    #各頂点を始点として最大パスを探す
    for i in range(n):
        dfs(i)
    print(max(dp))
if __name__ == '__main__':
    g()

Submission status: Runtime Error

Solution code:
# @author 

import sys

class GLongestPath:
    def dfs(self, s):
        if self.done[s]:
            return self.dp[s]

        self.done[s] = 1

        ans = 0
        for x in self.adj[s]:
            ans = max(ans, 1 + self.dfs(x))

        self.dp[s] = ans
        return ans

    def solve(self):
        from collections import defaultdict
        n, m = [int(_) for _ in input().split()]
        self.adj = defaultdict(list)
        self.dp = [0] * (n + 1)
        self.done = [0] * (n + 1)
        for i in range(m):
            u, v = [int(_) for _ in input().split()]
            self.adj[u].append(v)

        ans = 0
        for i in range(1, n + 1):
            ans = max(ans, self.dfs(i))

        print(ans)

solver = GLongestPath()
input = sys.stdin.readline

solver.solve()

Submission status: Runtime Error

Solution code:
import itertools as ite
import math
import random

INF = 10 ** 18
MOD = 10 ** 9 + 7

N, M = map(int, raw_input().split())
E = [[] for i in range(N + 1)]
for i in range(M):
    x, y = map(int, raw_input().split())
    E[x].append(y)

m = {}
def func(pos):
    if pos in m:
        return m[pos]
    ret = 0
    for to in E[pos]:
        ret = max(ret, func(to) + 1)
    m[pos] = ret
    return ret

print max(func(i) for i in range(1, N + 1))

Submission status: Runtime Error

Solution code:
n, m = map(int, input().split())

memo = [-1] * (10 ** 6 + 100)

g = [[] for i in range(n + 1)]

for i in range(m):
    x, y = map(int, input().split())
    g[y].append(x)


def dfs(i):
    if memo[i] != -1:
        return memo[i]
    if not g[i]:
        return 0
    else:
        memo[i] = max(dfs(v) for v in g[i]) + 1
        return memo[i]


for i in range(1, n+1):
    dfs(i)

print(max(memo))

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10000)
input = sys.stdin.readline
'''
allinputs = iter(input().splitlines())
input = lambda : next(allinputs)
#'''
N, M = map(int, input().split())
x, y = [0] * M, [0] * M
from_x = [[] for _ in range(N)]
memo = [-1] * N
ans = 0

def length(i):
	if memo[i] >= 0:
		return memo[i]
	else:
		if len(from_x[i]) == 0:
			memo[i] = 0
			return memo[i]
		else:
			ret = 0
			
			for j in from_x[i]:
				tmp = length(j) + 1
				if tmp > ret:
					ret = tmp
					
			memo[i] = ret
			return ret

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

for i in range(N):
	tmp = length(i)
	if tmp > ans:
		ans = tmp

print(ans)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
'''
allinputs = iter(input().splitlines())
input = lambda : next(allinputs)
#'''
N, M = map(int, input().split())
x, y = [0] * M, [0] * M
from_x = [[] for _ in range(N)]
memo = [-1] * N
ans = 0

def length(i):
	if memo[i] >= 0:
		return memo[i]
	else:
		if len(from_x[i]) == 0:
			memo[i] = 0
			return memo[i]
		else:
			ret = 0
			
			for j in from_x[i]:
				tmp = length(j) + 1
				if tmp > ret:
					ret = tmp
					
			memo[i] = ret
			return ret

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

for i in range(N):
	tmp = length(i)
	if tmp > ans:
		ans = tmp

print(ans)

Submission status: Runtime Error

Solution code:
from collections import defaultdict
import sys
sys.setrecursionlimit(10**10)
N,M = map(int,input().split())

d = defaultdict(lambda:[])

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

dp = [-1]*N

def dist(s):
    if dp[s] >= 0:
        return dp[s]
    if d[s] == []:
        dp[s] = 0
        return 0
    res = 0
    for t in d[s]:
        res = max(res,1+dist(t))
    dp[s] = res
    return res

ans = 0
for i in range(N):
    ans = max(ans,dist(i))

print(ans)

Submission status: Runtime Error

Solution code:
from collections import defaultdict
N,M = map(int,input().split())

d = defaultdict(lambda:[])

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

dp = [-1]*N

def dist(s):
    if dp[s] >= 0:
        return dp[s]
    if d[s] == []:
        dp[s] = 0
        return 0
    res = 0
    for t in d[s]:
        res = max(res,1+dist(t))
    dp[s] = res
    return res

ans = 0
for i in range(N):
    ans = max(ans,dist(i))

print(ans)

Submission status: Runtime Error

Solution code:
import sys

input = sys.stdin.readline

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

G = [[] for _ in range(N)]

for _ in range(M):
    x, y = map(int, input().split())
    x -= 1
    y -= 1  # 0 startにする
    G[x].append(y)


# dp[v] := ノードvを始点としたときのパス長の最大値
# BFSでdp[v]を求める
# res = dp[v]として、chmax(res, rec(nv) + 1)



        


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

def rec(v):
    if dp[v] != -1:
        return dp[v]
    res = 0
    for next_v in G[v]:
        res = max(res, rec(next_v) + 1)

    dp[v] = res
    return dp[v]

res = 0
for v in range(N):
    res = max(res, rec(v) + 1)
print(res - 1)

Submission status: Runtime Error

Solution code:
def LI(): return [int(x) for x in input().split()]
def TI(): return tuple(LI())
def II(): return int(input())

N,M=LI()

MP={}
for _ in range(M):
    fr, to = TI()
    if not fr in MP:
        MP[fr] = []
    MP[fr].append(to)

RESL=[-1]*(N+1)
def dfs(n):
    if RESL[n] != -1:
        return RESL[n]
    else:
        res = 0
        if n in MP:
            for to in MP[n]:
                res = max(res, dfs(to))
            res += 1
        RESL[n] = res
        return res
for i in range(1, N+1): dfs(i)
# print(RESL)
print(max(RESL))

Submission status: Runtime Error

Solution code:
N,M = map(int,input().split())
edge = { i:[] for i in range(1,N+1)}
for _ in range(M):
    x,y = map(int,input().split())
    edge[x] += [y]

dp = [-1]*(N+1)
def distance(start):
    if dp[start] > 0:
        return dp[start]
    elif len(edge[start]) == 0:
        dp[start] = 0
        return 0
    else:
        temp = [distance(e) + 1 for e in edge[start]]
        dp[start] =  max(temp)
        return dp[start]

for i in range(1,N+1):
    go = distance(i)
print(max(dp))

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
g=[[] for i in range(1+n)]
dist=[-1]*(1+n)
for i in range(m):
    a,b=map(int,input().split())
    g[a].append(b)
    
def dfs(cur):
    if dist[cur]!=-1:
        return dist[cur]
        
    if not g[cur]:
        dist[cur]=0
    else:
        dist[cur]=1+max([dfs(nb) for nb in g[cur]])
    return dist[cur]

ans=0
for i in range(1,n+1):
    ans=max(ans,dfs(i))

print(ans)

Submission status: Runtime Error

Solution code:
### 動的計画法のテーブルを用意
# dp(n) は頂点nを始点として時の最大有効パスの長さ

def get_inputs():
    N, M = map(int, input().split())

    # グラフgを初期化
    g = [[] for _ in range(N)]

    # 辺情報を読み込みgに追加
    for _ in range(M):
        x, y = map(lambda c: int(c) - 1, input().split())
        g[x].append(y)

    return N, M, g


def getlp(x, depth):
    global dp, g

    if depth >= MAX_DEPTH:
        return -1
    elif dp[x] != -1:
        return dp[x]
    elif len(g[x]) == 0:
        dp[x] = 0
        return dp[x]
    else:
        mxp = 0
        for y in g[x]:
            yp = getlp(y, depth + 1)
            if yp == -1:
                return -1
            elif yp > mxp:
                mxp = yp
        dp[x] = mxp + 1
        return dp[x]


N, M, g = get_inputs()
MAX_DEPTH = 999

dp = [-1] * N
nodes = list(range(N))
ans = 0

while len(nodes) > 0:
    tmp_nodes = []
    for x in range(N):
        if dp[x] == -1:
            t = getlp(x, 0)
            if t == -1:
                tmp_nodes.append(x)
            else:
                ans = max(ans, t)
        else:
            ans = max(ans, dp[x])

    nodes = list(tmp_nodes)

print(ans)

Submission status: Runtime Error

Solution code:
### 動的計画法のテーブルを用意
# dp(n) は頂点nを始点として時の最大有効パスの長さ

def get_inputs():
    N, M = map(int, input().split())

    # グラフgを初期化
    g = [[] for _ in range(N)]

    # 辺情報を読み込みgに追加
    for _ in range(M):
        x, y = map(lambda c: int(c) - 1, input().split())
        g[x].append(y)

    return N, M, g


def getlp(x, depth):
    global dp, g

    if depth >= MAX_DEPTH:
        return -1
    elif dp[x] != -1:
        return dp[x]
    elif len(g[x]) == 0:
        dp[x] = 0
        return dp[x]
    else:
        mxp = 0
        for y in g[x]:
            yp = getlp(y, depth + 1)
            if yp == -1:
                return -1
            elif yp > mxp:
                mxp = yp
        dp[x] = mxp + 1
        return dp[x]


N, M, g = get_inputs()
MAX_DEPTH = 1000

dp = [-1] * N
nodes = list(range(N))
ans = 0

while len(nodes) > 0:
    tmp_nodes = []
    for x in range(N):
        if dp[x] == -1:
            t = getlp(x, 0)
            if t == -1:
                tmp_nodes.append(x)
            else:
                ans = max(ans, t)
        else:
            ans = max(ans, dp[x])

    nodes = list(tmp_nodes)

print(ans)

Submission status: Runtime Error

Solution code:
### 動的計画法のテーブルを用意
# dp(n) は頂点nを始点として時の最大有効パスの長さ

def get_inputs():
    N, M = map(int, input().split())
    
    # グラフgを初期化
    g = [[] for _ in range(N)]
    
    # 辺情報を読み込みgに追加
    for _ in range(M):
        x, y = map(lambda c: int(c) - 1, input().split())
        g[x].append(y)
    
    return N, M, g


def getlp(x):
    global dp, g
    
    if dp[x] != -1:
        return dp[x]
    elif len(g[x]) == 0:
        dp[x] = 0
        return dp[x]
    else:
        mxp = 0
        for y in g[x]:
            yp = getlp(y)
            if yp > mxp:
                mxp = yp
        dp[x] = mxp + 1
        return dp[x]


N, M, g = get_inputs()

dp = [-1] * N
ans = 0
for x in range(N-1, -1, -1):
    ans = max(ans, getlp(x))

print(ans)

Submission status: Runtime Error

Solution code:
# 動的計画法のテーブルを用意
# dp(n) は頂点nを始点として時の最大有効パスの長さ

def get_inputs():
    N, M = map(int, input().split())
    
    # グラフgを初期化
    g = [[] for _ in range(N)]
    
    # 辺情報を読み込みgに追加
    for _ in range(M):
        x, y = map(lambda c: int(c) - 1, input().split())
        g[x].append(y)
    
    return N, M, g


def getlp(x):
    global dp, g
    
    if dp[x] != -1:
        return dp[x]
    elif len(g[x]) == 0:
        dp[x] = 0
        return dp[x]
    else:
        mxp = 0
        for y in g[x]:
            yp = getlp(y)
            if yp > mxp:
                mxp = yp
        dp[x] = mxp + 1
        return dp[x]


N, M, g = get_inputs()

dp = [-1] * N
ans = 0
for x in range(N):
    ans = max(ans, getlp(x))

print(ans)

Submission status: Runtime Error

Solution code:
# 動的計画法のテーブルを用意
# dp(n) は頂点nを始点として時の最大有効パスの長さ

def get_inputs():
    N, M = map(int, input().split())

    # グラフgを初期化
    g = [[] for _ in range(N)]

    # 辺情報を読み込みgに追加
    for _ in range(M):
        x, y = map(lambda c: int(c) - 1, (input().split()))
        g[x].append(y)

    return N, M, g


def getlp(x):
    global dp, g

    if dp[x] != -1:
        return dp[x]
    elif len(g[x]) == 0:
        dp[x] = 0
    else:
        mxp = 0
        for y in g[x]:
            yp = getlp(y)
            if yp > mxp:
                mxp = yp
        dp[x] = mxp + 1

    return dp[x]


N, M, g = get_inputs()

dp = [-1] * N
ans = 0
for x in range(N):
    ans = max(ans, getlp(x))

print(ans)

Submission status: Runtime Error

Solution code:
# 動的計画法のテーブルを用意
# dp(n) は頂点nを始点として時の最大有効パスの長さ

def get_inputs():
    N, M = map(int, input().split())

    # グラフgを初期化
    g = [[] for _ in range(N)]

    # 辺情報を読み込みgに追加
    for _ in range(M):
        x, y = map(lambda c: int(c) - 1, (input().split()))
        g[x].append(y)

    return N, M, g


def getlp(x):
    global dp, g

    if dp[x] != -1:
        pass
    elif len(g[x]) == 0:
        dp[x] = 0
    else:
        mxp = 0
        for y in g[x]:
            yp = getlp(y)
            if yp > mxp:
                mxp = yp
        dp[x] = mxp + 1

    return dp[x]


N, M, g = get_inputs()

dp = [-1] * N
ans = 0
for x in range(N):
    dp[x] = getlp(x)
    ans = max(ans, dp[x])

print(ans)

Submission status: Runtime Error

Solution code:
# 動的計画法のテーブルを用意
# dp(n) は頂点nを始点として時の最大有効パスの長さ
dp = [-1] * N

def get_inputs():
    N, M = map(int, input().split())
    
    # グラフgを初期化
    g = [[] for _ in range(N)]
    
    # 辺情報を読み込みgに追加
    for _ in range(M):
        x, y = map(lambda c: int(c) - 1, (input().split()))
        g[x].append(y)
    
    return N, M, g


def getlp(x):
    global dp, g
    
    if dp[x] != -1:
        pass
    elif len(g[x]) == 0:
        dp[x] = 0
    else:
        mxp = 0
        for y in g[x]:
            yp = getlp(y)
            if yp > mxp:
                mxp = yp
        dp[x] = mxp + 1
    
    return dp[x]


N, M, g = get_inputs()

ans = 0
for x in range(N):
    dp[x] = getlp(x)
    ans = max(ans, dp[x])

print(ans)

Submission status: Runtime Error

Solution code:
# 動的計画法のテーブルを用意
# dp(n) は頂点nを始点として時の最大有効パスの長さ
dp = [-1] * N

def get_inputs():
    N, M = map(int, input().split())
    
    # グラフgを初期化
    g = [[] for _ in range(N)]
    
    # 辺情報を読み込みgに追加
    for _ in range(M):
        x, y = map(lambda c: int(c) - 1, (input().split()))
        g[x].append(y)
    
    return N, M, g


def getlp(x):
    global dp, g
    
    if dp[x] != -1:
        pass
    elif len(g[x]) == 0:
        dp[x] = 0
    else:
        mxp = 0
        for y in g[x]:
            yp = getlp(y)
            if yp > mxp:
                mxp = yp
        dp[x] = mxp + 1
    
    return dp[x]


if __name__ == "__main__":
    N, M, g = get_inputs()
    
    ans = 0
    for x in range(N):
        dp[x] = getlp(x)
        ans = max(ans, dp[x])
    
    print(ans)

Submission status: Runtime Error

Solution code:
n, m = map(int, input().split())
info = [list(map(int, input().split())) for i in range(m)]
tree = [[] for i in range(n)]
start_point = [True] * n
for i in range(m):
    tree[info[i][0] - 1].append(info[i][1] - 1)
    start_point[info[i][1] - 1] = False
     
dp = [0]*n

def dfs(pos):
    for i in tree[pos]:
        if dp[i] < dp[pos] + 1:
            dp[i] = dp[pos] + 1
            dfs(i)

for i in range(len(start_point)):
    if start_point[i]:
        dfs(i)
print(max(dp))

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!
from functools import lru_cache


n,m = [int(i) for i in input().split()]

#ko = [[] for _ in range(n)]
oya = [[] for _ in range(n)]
for _ in range(m):
    x,y = [int(i)-1 for i in input().split()]
#    ko[x].append(y)
    oya[y].append(x)

#memo = [-1]*n

@lru_cache(maxsize=None)
def longest(v): #nを終点とするlongest
#    if memo[v] >= 0:
#        return memo[v]
#    else:
    if oya[v]:
        return max([longest(j)+1 for j in oya[v]])
    else:
        return 0
        
    

memo = [longest(i) for i in range(n)]

print(max(memo))    
#print(memo)

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!
from functools import lru_cache


n,m = [int(i) for i in input().split()]

#ko = [[] for _ in range(n)]
oya = [[] for _ in range(n)]
for _ in range(m):
    x,y = [int(i)-1 for i in input().split()]
#    ko[x].append(y)
    oya[y].append(x)

#memo = [-1]*n

@lru_cache()
def longest(v): #nを終点とするlongest
#    if memo[v] >= 0:
#        return memo[v]
#    else:
    c=0
    for j in oya[v]:
        c = max(c,longest(j)+1)
#    memo[v] = c
    return c

memo = [longest(i) for i in range(n)]

print(max(memo))    
#print(memo)

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!

n,m = [int(i) for i in input().split()]

#ko = [[] for _ in range(n)]
oya = [[] for _ in range(n)]
for _ in range(m):
    x,y = [int(i)-1 for i in input().split()]
#    ko[x].append(y)
    oya[y].append(x)

memo = [-1]*n

def longest(v): #nを終点とするlongest
    if memo[v] >= 0:
        return memo[v]
    else:
        c=0
        for j in oya[v]:
            c = max(c,longest(j)+1)
        memo[v] = c
        return c

for i in range(n):
    longest(i)

print(max(memo))    
#print(memo)

Submission status: Runtime Error

Solution code:
def g():
    from collections import defaultdict
    N, M = map(int, input().split())
    xy = defaultdict(list)
    memo = defaultdict(list)
    for _ in range(M):
        x, y = map(int, input().split())
        xy[x].append(y)

    def g2(x):
        if x in memo:
            return memo[x]
        else:
            r = 0
            for ny in xy[x]:
                r = max(r, g2(ny) + 1)
            memo[x] = r
        return r

    ans = 0
    for i in range(N):
        ans = max(ans, g2(i))
    return ans


if __name__ == '__main__':
    ans = g()
    print(ans)

Submission status: Runtime Error

Solution code:
def g():
    from collections import defaultdict
    N, M = map(int, input().split())
    xy = defaultdict(list)
    memo = defaultdict(list)
    for _ in range(M):
        x, y = map(int, input().split())
        xy[x].append(y)

    def g2(x):
        if x in memo:
            return memo[x]
        else:
            r = 0
            for ny in xy[x]:
                r = max(r, g2(ny) + 1)
            memo[x] = r
        return r

    ans = [g2(i) for i in range(1, N + 1)]
    return max(ans)


if __name__ == '__main__':
    ans = g()
    print(ans)

Submission status: Runtime Error

Solution code:
def g():
    from collections import defaultdict
    N, M = map(int, input().split())
    xy = defaultdict(list)
    for _ in range(M):
        x, y = map(int, input().split())
        xy[x].append(y)
    memo = {}

    def g2(x):
        if x in memo:
            return memo[x]
        else:
            r = 0
            for ny in xy[x]:
                r = max(r, g2(ny) + 1)
            memo[x] = r
        return r

    ans = [g2(i) for i in range(1, N + 1)]
    return max(ans)


if __name__ == '__main__':
    ans = g()
    print(ans)

Submission status: Runtime Error

Solution code:
from collections import defaultdict


def rec(node):
    if dp[node]:
        return dp[node]

    res = 0
    for next_node in edge[node]:
        res = max(res, rec(next_node) + 1)

    dp[node] = res
    return res


n, m = map(int, input().split())
dp = [0] * (n + 1)
edge = defaultdict(list)

for i in range(m):
    x, y = map(int, input().split())
    edge[x].append(y)

ans = 0
for i in range(1, n + 1):
    ans = max(ans, rec(i))

print(ans)

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
V = [[] for _ in range(N)]
for _ in range(M):
    x, y = map(lambda x: int(x) - 1, input().split())
    V[x].append(y)

dp = [None for _ in range(N)]

def dfs(x):
    if dp[x] is not None:
        return dp[x]
    if not V[x]:
        dp[x] = 0
        return 0
    l = max(dfs(v) for v in V[x]) + 1
    dp[x] = l
    return l

print(max(dfs(v) for v in range(N)))

Submission status: Runtime Error

Solution code:
n , m = map(int, input().split())
edge=[[] for _ in range(n)]
deg=[0] * n
for i in range(m):
    x, y =map(int, input().split())
    edge[x - 1 ].append(y - 1)
    deg[y - 1] +=1 

from collections import deque

que =deque()
for i in range(n):
    if deg[i] == 0:
        que.append(i)
dp = [0] *n

while que:
    v = que.popleft()
    nv = edge[v]
    for nv in nv:
        deg[nv] -=1
        if deg[nv] ==0:
            que.append(nv)
            dp[nv] = max(dp[v] + 1 ,dp[nv])
print(max(dp)

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())

graph = [ [] for _ in range(N) ]

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

dp = [-1] * N

def cnt(j):
    if dp[j] != -1:
        return(dp[j])
    else:
        dp[j] = 0
        for k in graph[j]:
            dp[j] = max(cnt(k)+1, dp[j])
        return dp[j]

print(max([ cnt(i) for i in range(N) ]))

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
 
graph = [ [] for _ in range(N) ]
 
for i in range(M):
    x, y = map(int, input().split())
    graph[x-1].append(y-1)
 
dp = [-1] * N
length = [0] * N
 
def count(j):
    if dp[j] != -1:
        return(dp[j])
    else:
        dp[j] = 0
        for k in graph[j]:
            dp[j] = max(count(k)+1, dp[j])
        return dp[j]
 
for i in range(N):
    length[i] = count(i)
 
print(max(length))