Buggy python submission (verdict: Time Limit Exceeded):
#DAGの最長有向パスをトポロジカルソートで解く
from collections import defaultdict

N,M=map(int,input().split(' '))
se=set()
d=defaultdict(int)
for i in range(M):
    x,y=map(int,input().split(' '))
    se.add(x*(10**5)+y)
n=set(i for i in range(1,N+1))
while N>0:
    m=set([x%10**5 for x in se])
    for i in n-m:
        n.remove(i)
        N-=1
        de=set([x for x in se if x//10**5==i])
        for j in de:
            k=j%10**5
            se.remove(j)
            if d[k]<=d[i]:
                d[k]=d[i]+1
print(max(d.values()))

Fixed submission:
from collections import defaultdict, deque

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

outs = defaultdict(list)
ins = defaultdict(int)
for v1, v2 in es:
    outs[v1].append(v2)
    ins[v2] += 1
length=defaultdict(int)

q = deque(set(i for i in range(1,N+1))-set(ins.keys()))
length=defaultdict(int)
while q:
    v1 = q.popleft()
    for v2 in outs[v1]:
        ins[v2] -= 1
        if length[v2]<=length[v1]:
            length[v2]=length[v1]+1
        if ins[v2] == 0:
            q.append(v2)

print(max(length.values()))

Buggy python submission (verdict: Time Limit Exceeded):
#DAGの最長有向パスをトポロジカルソートで解く
from collections import defaultdict

N,M=map(int,input().split(' '))
x=[0]*M
y=[0]*M
d=defaultdict(int)
for i in range(M):
    x[i],y[i]=map(int,input().split(' '))
n=set(i for i in range(1,N+1))
while len(n)>0:
    for i in n-set(y):
        n.remove(i)
        for j in range(M):
            if i==x[j]:
                if d[y[j]]<=d[i]:
                    d[y[j]]=d[i]+1
                y[j]=0
print(max(d.values()))

Fixed submission:
from collections import defaultdict, deque

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

outs = defaultdict(list)
ins = defaultdict(int)
for v1, v2 in es:
    outs[v1].append(v2)
    ins[v2] += 1
length=defaultdict(int)

q = deque(set(i for i in range(1,N+1))-set(ins.keys()))
length=defaultdict(int)
while q:
    v1 = q.popleft()
    for v2 in outs[v1]:
        ins[v2] -= 1
        if length[v2]<=length[v1]:
            length[v2]=length[v1]+1
        if ins[v2] == 0:
            q.append(v2)

print(max(length.values()))

Buggy python submission (verdict: Time Limit Exceeded):
#DAGの最長有向パスをトポロジカルソートで解く
from collections import defaultdict

N,M=map(int,input().split(' '))
se=set()
d=defaultdict(int)
for i in range(M):
    x,y=map(int,input().split(' '))
    se.add(x*(10**5)+y)
n=set(i for i in range(1,N+1))
while N>0:
    m=set([x%10**5 for x in se])
    for i in n-m:
        n.remove(i)
        N-=1
        de=[x for x in se if x//10**5==i]
        for j in de:
            k=j%10**5
            se.remove(j)
            if d[k]<=d[i]:
                d[k]=d[i]+1
print(max(d.values()))

Fixed submission:
from collections import defaultdict, deque

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

outs = defaultdict(list)
ins = defaultdict(int)
for v1, v2 in es:
    outs[v1].append(v2)
    ins[v2] += 1
length=defaultdict(int)

q = deque(set(i for i in range(1,N+1))-set(ins.keys()))
length=defaultdict(int)
while q:
    v1 = q.popleft()
    for v2 in outs[v1]:
        ins[v2] -= 1
        if length[v2]<=length[v1]:
            length[v2]=length[v1]+1
        if ins[v2] == 0:
            q.append(v2)

print(max(length.values()))

Buggy python submission (verdict: Time Limit Exceeded):
#DAGの最長有向パスをトポロジカルソートで解く
from collections import defaultdict

N,M=map(int,input().split(' '))
x=[0]*M
y=[0]*M
d=defaultdict(int)
for i in range(M):
    x[i],y[i]=map(int,input().split(' '))
n=set(i for i in range(1,N+1))
while len(n)>0:
    m=n-set(y)
    for i in m:
        n.remove(i)
        for j in range(M):
            if i==x[j]:
                if d[y[j]]<=d[i]:
                    d[y[j]]=d[i]+1
                y[j]=0
print(max(d.values()))

Fixed submission:
from collections import defaultdict, deque

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

outs = defaultdict(list)
ins = defaultdict(int)
for v1, v2 in es:
    outs[v1].append(v2)
    ins[v2] += 1
length=defaultdict(int)

q = deque(set(i for i in range(1,N+1))-set(ins.keys()))
length=defaultdict(int)
while q:
    v1 = q.popleft()
    for v2 in outs[v1]:
        ins[v2] -= 1
        if length[v2]<=length[v1]:
            length[v2]=length[v1]+1
        if ins[v2] == 0:
            q.append(v2)

print(max(length.values()))

Buggy python submission (verdict: Time Limit Exceeded):
#DAGの最長有向パスをトポロジカルソートで解く
N,M=map(int,input().split(' '))
x=[0]*M
y=[0]*M
z=[0]*(N+1)
key=[0]*(N+1)
for i in range(M):
    x[i],y[i]=map(int,input().split(' '))
n=set(i for i in range(1,N+1))
while len(n)>0:
    for i in n-set(y):
        n.remove(i)
        for j in range(M):
            if i==x[j]:
                if z[y[j]]<=z[x[j]]:
                    z[y[j]]=z[x[j]]+1
                x[j],y[j]=0,0
print(max(z))

Fixed submission:
from collections import defaultdict, deque

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

outs = defaultdict(list)
ins = defaultdict(int)
for v1, v2 in es:
    outs[v1].append(v2)
    ins[v2] += 1
length=defaultdict(int)

q = deque(set(i for i in range(1,N+1))-set(ins.keys()))
length=defaultdict(int)
while q:
    v1 = q.popleft()
    for v2 in outs[v1]:
        ins[v2] -= 1
        if length[v2]<=length[v1]:
            length[v2]=length[v1]+1
        if ins[v2] == 0:
            q.append(v2)

print(max(length.values()))

Buggy python submission (verdict: Time Limit Exceeded):
#DAGの最長有向パスをトポロジカルソートで解く
N,M=map(int,input().split(' '))
x=[0]*M
y=[0]*M
z=[0]*(N+1)
key=[0]*(N+1)
for i in range(M):
    x[i],y[i]=map(int,input().split(' '))
n=set(i for i in range(1,N+1))
while len(n)>0:
    for i in n:
        if i not in y:
            n.remove(i)
            for j in range(M):
                if i==x[j]:
                    if z[y[j]]<=z[x[j]]:
                        z[y[j]]=z[x[j]]+1
                    x[j],y[j]=0,0
            break
print(max(z))

Fixed submission:
from collections import defaultdict, deque

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

outs = defaultdict(list)
ins = defaultdict(int)
for v1, v2 in es:
    outs[v1].append(v2)
    ins[v2] += 1
length=defaultdict(int)

q = deque(set(i for i in range(1,N+1))-set(ins.keys()))
length=defaultdict(int)
while q:
    v1 = q.popleft()
    for v2 in outs[v1]:
        ins[v2] -= 1
        if length[v2]<=length[v1]:
            length[v2]=length[v1]+1
        if ins[v2] == 0:
            q.append(v2)

print(max(length.values()))

Buggy python submission (verdict: Time Limit Exceeded):
#DAGの最長有向パスをトポロジカルソートで解く
N,M=map(int,input().split(' '))
x=[0]*M
y=[0]*M
z=[0]*(N+1)
for i in range(M):
    x[i],y[i]=map(int,input().split(' '))
n=[i for i in range(1,N+1)]
while len(n)>0:
    for i in n:
        if i not in y:
            n.remove(i)
            for j in range(M):
                if i==x[j]:
                    if z[y[j]]<=z[x[j]]:
                        z[y[j]]=z[x[j]]+1
                    x[j],y[j]=0,0
            break
print(max(z))

Fixed submission:
from collections import defaultdict, deque

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

outs = defaultdict(list)
ins = defaultdict(int)
for v1, v2 in es:
    outs[v1].append(v2)
    ins[v2] += 1
length=defaultdict(int)

q = deque(set(i for i in range(1,N+1))-set(ins.keys()))
length=defaultdict(int)
while q:
    v1 = q.popleft()
    for v2 in outs[v1]:
        ins[v2] -= 1
        if length[v2]<=length[v1]:
            length[v2]=length[v1]+1
        if ins[v2] == 0:
            q.append(v2)

print(max(length.values()))