Buggy python submission (verdict: Time Limit Exceeded):
#!/mnt/c/Users/moiki/bash/env/bin/python
N,M = map(int, input().split())
g = [ list(map(lambda x: int(x)-1, input().split())) for _ in range(M) ]

from collections import Counter, defaultdict
childs  = Counter( x[1] for x in g)
childs_list  = defaultdict(list)

for x,y in g:
    childs_list[x].append(y)
roots = []
for i in range(N):
    if childs[i] == 0:
        roots.append(i)

l = [ i for i in roots]
costs = [ 0 for _ in range(N)]

while l: 

    nex_stack = []
    # p = l.pop()
    for p in l:

        cnt = costs[p]

        for c in childs_list[p]:
            if cnt+1 > costs[c]:
                costs[c] = cnt+1
                # l.append( c ) 
                nex_stack.append(c)
    
    l = nex_stack


print(max(costs))
        
        



Fixed submission:
#!/mnt/c/Users/moiki/bash/env/bin/python
N,M = map(int, input().split())
g = [ list(map(lambda x: int(x)-1, input().split())) for _ in range(M) ]

from collections import Counter, defaultdict
childs  = Counter( x[1] for x in g)
childs_list  = defaultdict(list)

for x,y in g:
    childs_list[x].append(y)
roots = []
for i in range(N):
    if childs[i] == 0:
        roots.append(i)

l = [ i for i in roots]
costs = [ 0 for _ in range(N)]

while l: 

    nex_stack = []
    # p = l.pop()
    for p in l:
        # cnt = costs[p]
        for c in childs_list[p]:
            costs[c] = max(costs[p]+1, costs[c])
            childs[c] -= 1
            if childs[c] == 0:
                nex_stack.append(c)
    
    l = nex_stack


print(max(costs))

Buggy python submission (verdict: Time Limit Exceeded):
#!/mnt/c/Users/moiki/bash/env/bin/python
N,M = map(int, input().split())
g = [ list(map(lambda x: int(x)-1, input().split())) for _ in range(M) ]

from collections import Counter, defaultdict
childs  = Counter( x[1] for x in g)
childs_list  = defaultdict(list)

for x,y in g:
    childs_list[x].append(y)
roots = []
for i in range(N):
    if childs[i] == 0:
        roots.append(i)

l = [ i for i in roots]
costs = [ 0 for _ in range(N)]

while l: 
    p = l.pop()

    
    # costs[p] = max(costs[v_idx], cnt)
    # costs[p] = max(costs[p], costs[ 

    cnt = costs[p]

    for c in childs_list[p]:
        if cnt+1 > costs[c]:
            # l.append(  (c, childs_list[c], cnt+1) ) 
            costs[c] = cnt+1
            l.append( c ) 
            # nex_stack.append( c ) 


print(max(costs))
        
        



Fixed submission:
#!/mnt/c/Users/moiki/bash/env/bin/python
N,M = map(int, input().split())
g = [ list(map(lambda x: int(x)-1, input().split())) for _ in range(M) ]

from collections import Counter, defaultdict
childs  = Counter( x[1] for x in g)
childs_list  = defaultdict(list)

for x,y in g:
    childs_list[x].append(y)
roots = []
for i in range(N):
    if childs[i] == 0:
        roots.append(i)

l = [ i for i in roots]
costs = [ 0 for _ in range(N)]

while l: 

    nex_stack = []
    # p = l.pop()
    for p in l:
        # cnt = costs[p]
        for c in childs_list[p]:
            costs[c] = max(costs[p]+1, costs[c])
            childs[c] -= 1
            if childs[c] == 0:
                nex_stack.append(c)
    
    l = nex_stack


print(max(costs))

Buggy python submission (verdict: Time Limit Exceeded):
#!/mnt/c/Users/moiki/bash/env/bin/python
N,M = map(int, input().split())
g = [ list(map(lambda x: int(x)-1, input().split())) for _ in range(M) ]

from collections import Counter, defaultdict
childs  = Counter( x[1] for x in g)
childs_list  = defaultdict(list)

for x,y in g:
    childs_list[x].append(y)
roots = []
for i in range(N):
    if childs[i] == 0:
        roots.append(i)

# l = [(i,childs_list[i],0)]
l = [(i,childs_list[i],0) for i in roots]
costs = [ 0 for _ in range(N)]

while l: 
    nex_stack = []

    for p in l:
        v_idx, chlist, cnt = p
        costs[v_idx] = max(costs[v_idx], cnt)

        for c in chlist:
            if cnt+1 > costs[c]:
                # l.append(  (c, childs_list[c], cnt+1) ) 
                nex_stack.append( (c, childs_list[c], cnt+1) ) 

    l = nex_stack
print(max(costs))
        
        



Fixed submission:
#!/mnt/c/Users/moiki/bash/env/bin/python
N,M = map(int, input().split())
g = [ list(map(lambda x: int(x)-1, input().split())) for _ in range(M) ]

from collections import Counter, defaultdict
childs  = Counter( x[1] for x in g)
childs_list  = defaultdict(list)

for x,y in g:
    childs_list[x].append(y)
roots = []
for i in range(N):
    if childs[i] == 0:
        roots.append(i)

l = [ i for i in roots]
costs = [ 0 for _ in range(N)]

while l: 

    nex_stack = []
    # p = l.pop()
    for p in l:
        # cnt = costs[p]
        for c in childs_list[p]:
            costs[c] = max(costs[p]+1, costs[c])
            childs[c] -= 1
            if childs[c] == 0:
                nex_stack.append(c)
    
    l = nex_stack


print(max(costs))

Buggy python submission (verdict: Time Limit Exceeded):
#!/mnt/c/Users/moiki/bash/env/bin/python
N,M = map(int, input().split())
g = [ list(map(lambda x: int(x)-1, input().split())) for _ in range(M) ]

from collections import Counter, defaultdict
childs  = Counter( x[1] for x in g)
childs_list  = defaultdict(list)

for x,y in g:
    childs_list[x].append(y)
roots = []
for i in range(N):
    if childs[i] == 0:
        roots.append(i)

# l = [(i,childs_list[i],0)]
l = [(i,childs_list[i],0) for i in roots]
costs = [ 0 for _ in range(N)]

while l: 
    v_idx, chlist, cnt = l.pop()
    costs[v_idx] = max(costs[v_idx], cnt)

    for c in chlist:
        if cnt+1 > costs[c]:
            l.append(  (c, childs_list[c], cnt+1) ) 
print(max(costs))
        
        



Fixed submission:
#!/mnt/c/Users/moiki/bash/env/bin/python
N,M = map(int, input().split())
g = [ list(map(lambda x: int(x)-1, input().split())) for _ in range(M) ]

from collections import Counter, defaultdict
childs  = Counter( x[1] for x in g)
childs_list  = defaultdict(list)

for x,y in g:
    childs_list[x].append(y)
roots = []
for i in range(N):
    if childs[i] == 0:
        roots.append(i)

l = [ i for i in roots]
costs = [ 0 for _ in range(N)]

while l: 

    nex_stack = []
    # p = l.pop()
    for p in l:
        # cnt = costs[p]
        for c in childs_list[p]:
            costs[c] = max(costs[p]+1, costs[c])
            childs[c] -= 1
            if childs[c] == 0:
                nex_stack.append(c)
    
    l = nex_stack


print(max(costs))

Buggy python submission (verdict: Time Limit Exceeded):
#!/mnt/c/Users/moiki/bash/env/bin/python
N,M = map(int, input().split())
g = [ list(map(lambda x: int(x)-1, input().split())) for _ in range(M) ]

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

# dp[i][j], i:step, j:path
from collections import Counter, defaultdict
parents = Counter( x[0] for x in g)
childs  = Counter( x[1] for x in g)


parents_dict = defaultdict(lambda :-1)
childs_list  = defaultdict(list)
parents_list  = defaultdict(list)

for x,y in g:
    childs_list[x].append(y)
    parents_dict[y]=x
    parents_list[y].append(x)

# print(parents)
roots = []
for i in range(N):
    # if childs[i] == 0:
    # if len( childs_list[i]) == 0:
    # if parents[i] == 0:
    if childs[i] == 0:
        roots.append(i)

l =  [ (i,childs_list[i], 0) for i in roots]
# l =  [ (i,parents_dict[i], 0) for i in roots]
# print(l)
# exit(0)

costs = [ 0 for _ in range(N)]
ans = 0
while l: 
    # v_idx, pa, cnt = l.pop()
    v_idx, chlist, cnt = l.pop()
    costs[v_idx] = max(costs[v_idx], cnt)
    ans = max(ans, cnt)
    # if parents_dict[pa] != -1:
    for c in chlist:
        if cnt+1 > costs[c]:
            l.append(  (c, childs_list[c], cnt+1) ) 
        # l.append(  (pa, parents_dict[pa], cnt+1) ) 
print(ans)
        
        



Fixed submission:
#!/mnt/c/Users/moiki/bash/env/bin/python
N,M = map(int, input().split())
g = [ list(map(lambda x: int(x)-1, input().split())) for _ in range(M) ]

from collections import Counter, defaultdict
childs  = Counter( x[1] for x in g)
childs_list  = defaultdict(list)

for x,y in g:
    childs_list[x].append(y)
roots = []
for i in range(N):
    if childs[i] == 0:
        roots.append(i)

l = [ i for i in roots]
costs = [ 0 for _ in range(N)]

while l: 

    nex_stack = []
    # p = l.pop()
    for p in l:
        # cnt = costs[p]
        for c in childs_list[p]:
            costs[c] = max(costs[p]+1, costs[c])
            childs[c] -= 1
            if childs[c] == 0:
                nex_stack.append(c)
    
    l = nex_stack


print(max(costs))

Buggy python submission (verdict: Time Limit Exceeded):
#!/mnt/c/Users/moiki/bash/env/bin/python
N,M = map(int, input().split())
g = [ list(map(lambda x: int(x)-1, input().split())) for _ in range(M) ]

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

# dp[i][j], i:step, j:path
from collections import Counter, defaultdict
parents = Counter( x[0] for x in g)
childs  = Counter( x[1] for x in g)


childs_list  = defaultdict(list)
for x,y in g:
    childs_list[x].append(y)


roots = []
for i in range(N):
    if childs[i] == 0:
        roots.append(i)

# root = i
l =  [ (i,childs_list[i], 0) for i in roots]
# l = [ (i, childs_list[i], 0) ] 
ans = 0
while l: 
    v_idx, chlist, cnt = l.pop()
    ans = max(ans, cnt)
    for c in sorted(chlist, key=lambda x:childs[x]):
        l.append(  (c, childs_list[c], cnt+1) ) 
print(ans)
        
        



Fixed submission:
#!/mnt/c/Users/moiki/bash/env/bin/python
N,M = map(int, input().split())
g = [ list(map(lambda x: int(x)-1, input().split())) for _ in range(M) ]

from collections import Counter, defaultdict
childs  = Counter( x[1] for x in g)
childs_list  = defaultdict(list)

for x,y in g:
    childs_list[x].append(y)
roots = []
for i in range(N):
    if childs[i] == 0:
        roots.append(i)

l = [ i for i in roots]
costs = [ 0 for _ in range(N)]

while l: 

    nex_stack = []
    # p = l.pop()
    for p in l:
        # cnt = costs[p]
        for c in childs_list[p]:
            costs[c] = max(costs[p]+1, costs[c])
            childs[c] -= 1
            if childs[c] == 0:
                nex_stack.append(c)
    
    l = nex_stack


print(max(costs))