Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
oya=[set() for i in range(n)]
ko=[set() for i in range(n)]
rank=[0]*n
for i in range(n+m-1):
    a,b=map(int,input().split())
    ko[a-1].add(b-1)
    oya[b-1].add(a-1)
for s in range(n):
    if not oya[s]:break
q={s}
ans=[0]*n
while q:
    now=q.pop()
    for i in ko[now]:
        if oya[now]:continue
        oya[i].remove(now)
        if not oya[i]:
            ans[i]=now+1
            q|=ko[now]
for i in ans:print(i)

Fixed submission:
n,m=map(int,input().split())
oya=[set() for i in range(n)]
ko=[set() for i in range(n)]
rank=[0]*n
for i in range(n+m-1):
    a,b=map(int,input().split())
    ko[a-1].add(b-1)
    oya[b-1].add(a-1)
for s in range(n):
    if not oya[s]:break
q=[[s,ko[s]]]
ans=[0]*n
while q:
    now,nex=q.pop()
    for i in nex:
        oya[i].remove(now)
        if not oya[i]:
            ans[i]=now+1
            q.append([i,ko[i]])
for i in ans:print(i)

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int, input().split())
ko=[[] for i in range(n)]
oya=[[] for i in range(n)]
rank=[0]*n 
for i in range(n-1+m):
    a,b=map(int, input().split())
    ko[b-1].append(a-1)
    oya[a-1].append(b-1)
from collections import deque
s=0
for i in range(n):
    if not ko[i]:s=i
p=deque([s])
p2={s}
nr=1
ans=[0]*n
while p:
    now=p.popleft()
    p2.remove(now)
    nr=rank[now]+1
    for i in oya[now]:
        rank[i]=nr
        ans[i]=now+1
        if i not in p2:
            p.append(i)
            p2.add(i)
for i in ans:print(i)

Fixed submission:
n,m=map(int,input().split())
oya=[set() for i in range(n)]
ko=[set() for i in range(n)]
rank=[0]*n
for i in range(n+m-1):
    a,b=map(int,input().split())
    ko[a-1].add(b-1)
    oya[b-1].add(a-1)
for s in range(n):
    if not oya[s]:break
q=[[s,ko[s]]]
ans=[0]*n
while q:
    now,nex=q.pop()
    for i in nex:
        oya[i].remove(now)
        if not oya[i]:
            ans[i]=now+1
            q.append([i,ko[i]])
for i in ans:print(i)

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int, input().split())
ko=[[] for i in range(n)]
oya=[[] for i in range(n)]
rank=[0]*n 
for i in range(n-1+m):
    a,b=map(int, input().split())
    ko[b-1].append(a-1)
    oya[a-1].append(b-1)
from collections import deque
s=0
for i in range(n):
    if not ko[i]:s=i
p=deque([s])
nr=1
ans=[0]*n
while p:
    now=p.popleft()
    nr=rank[now]+1
    p.extend(oya[now])
    for i in oya[now]:
        rank[i]=nr
        ans[i]=now+1
for i in ans:print(i)

Fixed submission:
n,m=map(int,input().split())
oya=[set() for i in range(n)]
ko=[set() for i in range(n)]
rank=[0]*n
for i in range(n+m-1):
    a,b=map(int,input().split())
    ko[a-1].add(b-1)
    oya[b-1].add(a-1)
for s in range(n):
    if not oya[s]:break
q=[[s,ko[s]]]
ans=[0]*n
while q:
    now,nex=q.pop()
    for i in nex:
        oya[i].remove(now)
        if not oya[i]:
            ans[i]=now+1
            q.append([i,ko[i]])
for i in ans:print(i)

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int, input().split())
ko=[[] for i in range(n)]
oya=[[] for i in range(n)]
rank=[0]*n 
for i in range(n-1+m):
    a,b=map(int, input().split())
    ko[b-1].append(a-1)
    oya[a-1].append(b-1)
from collections import deque
s=0
for i in range(n):
    if len(ko[i])==0:s=i
p=deque([s])
nr=1
ans=[0]*n
while p:
    now=p.popleft()
    nr=rank[now]+1
    p.extend(oya[now])
    for i in oya[now]:
        rank[i]=nr
        ans[i]=now+1
for i in ans:print(i)

Fixed submission:
n,m=map(int,input().split())
oya=[set() for i in range(n)]
ko=[set() for i in range(n)]
rank=[0]*n
for i in range(n+m-1):
    a,b=map(int,input().split())
    ko[a-1].add(b-1)
    oya[b-1].add(a-1)
for s in range(n):
    if not oya[s]:break
q=[[s,ko[s]]]
ans=[0]*n
while q:
    now,nex=q.pop()
    for i in nex:
        oya[i].remove(now)
        if not oya[i]:
            ans[i]=now+1
            q.append([i,ko[i]])
for i in ans:print(i)

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int, input().split())
ko=[[] for i in range(n)]
oya=[[] for i in range(n)]
rank=[0]*n 

for i in range(n-1+m):
    a,b=map(int, input().split())
    ko[b-1].append(a-1)
    oya[a-1].append(b-1)
s=0
for i in range(n):
    if len(ko[i])==0:s=i
p=[s]
nr=1
ans=[0]*n
while p:
    now=p.pop(0)
    nr=rank[now]+1
    p+=oya[now]
    for i in oya[now]:
        rank[i]=nr
        ans[i]=now+1
for i in ans:print(i)

Fixed submission:
n,m=map(int,input().split())
oya=[set() for i in range(n)]
ko=[set() for i in range(n)]
rank=[0]*n
for i in range(n+m-1):
    a,b=map(int,input().split())
    ko[a-1].add(b-1)
    oya[b-1].add(a-1)
for s in range(n):
    if not oya[s]:break
q=[[s,ko[s]]]
ans=[0]*n
while q:
    now,nex=q.pop()
    for i in nex:
        oya[i].remove(now)
        if not oya[i]:
            ans[i]=now+1
            q.append([i,ko[i]])
for i in ans:print(i)