Buggy python submission (verdict: Time Limit Exceeded):
import sys
import heapq
sys.setrecursionlimit(2000000)
n,m = map(int,input().split())
g2 = [[] for i in range(n)]
g = [[] for i in range(n)]
for i in range(n-1+m):
        a,b = map(int,input().split())
        g2[b-1].append(a-1)
        g[a-1].append(b-1)
for j in range(n):
        if len(g2[j]) == 0:
                ne = j
                break
ans = [[-1,-1] for i in range(n)]
ans[ne] = [0,0]
def dfs(x,y,z):
        for i in g[x]:
                if i == y:
                        continue
                if ans[i][1] < z+1:
                        ans[i] = [x+1,z+1]
                        heapq.heappush(que,[z+1,i,x])
que = [[0,ne,-1]]
heapq.heapify(que)
while que:
        c,a,b = heapq.heappop(que)
        dfs(a,b,c)
for i,j in ans:
        print(i)

Fixed submission:
import sys
import heapq
sys.setrecursionlimit(2000000)
n,m = map(int,input().split())
g2 = [[] for i in range(n)]
g = [[] for i in range(n)]
for i in range(n-1+m):
        a,b = map(int,input().split())
        g2[b-1].append(a-1)
        g[a-1].append(b-1)
for j in range(n):
        if len(g2[j]) == 0:
                ne = j
                break
kazu = [0]*n
for i in range(n):
        kazu[i] = len(g2[i])
ans = [0] * n
def dfs(x):
        z = []
        for i in g[x]:
                kazu[i]-=1
                if kazu[i] == 0:
                        z.append(i)
                        ans[i] = x+1
        for i in z:
                dfs(i)
dfs(ne)
for i in ans:
        print(i)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(2000000)
n,m = map(int,input().split())
g2 = [[] for i in range(n)]
g = [[] for i in range(n)]
for i in range(n-1+m):
        a,b = map(int,input().split())
        g2[b-1].append(a-1)
        g[a-1].append(b-1)
for j in range(n):
        if len(g2[j]) == 0:
                ne = j
                break
ans = [[-1,-1] for i in range(n)]
ans[ne] = [0,0]
def dfs(x,y,z):
        for i in g[x]:
                if i == y:
                        continue
                if ans[i][1] < z+1:
                        ans[i] = [x+1,z+1]
                        dfs(i,x,z+1)
dfs(ne,-1,0)
for i,j in ans:
        print(i)

Fixed submission:
import sys
import heapq
sys.setrecursionlimit(2000000)
n,m = map(int,input().split())
g2 = [[] for i in range(n)]
g = [[] for i in range(n)]
for i in range(n-1+m):
        a,b = map(int,input().split())
        g2[b-1].append(a-1)
        g[a-1].append(b-1)
for j in range(n):
        if len(g2[j]) == 0:
                ne = j
                break
kazu = [0]*n
for i in range(n):
        kazu[i] = len(g2[i])
ans = [0] * n
def dfs(x):
        z = []
        for i in g[x]:
                kazu[i]-=1
                if kazu[i] == 0:
                        z.append(i)
                        ans[i] = x+1
        for i in z:
                dfs(i)
dfs(ne)
for i in ans:
        print(i)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(2000000)
n,m = map(int,input().split())
g2 = [[] for i in range(n)]
g = [[] for i in range(n)]
for i in range(n-1+m):
        a,b = map(int,input().split())
        g2[b-1].append(a-1)
        g[a-1].append(b-1)
for j in range(n):
        if len(g2[j]) == 0:
                ne = j
                break
ans = [[-1,-1] for i in range(n)]
ans[ne] = [0,0]
def dfs(x,y,z):
        for i in g[x]:
                if i == y:
                        continue
                if ans[i][1] < z+1:
                        ans[i] = [x+1,z+1]
                        dfs(i,x,z+1)
dfs(ne,-1,0)
for i,j in ans:
        print(i)

Fixed submission:
import sys
import heapq
sys.setrecursionlimit(2000000)
n,m = map(int,input().split())
g2 = [[] for i in range(n)]
g = [[] for i in range(n)]
for i in range(n-1+m):
        a,b = map(int,input().split())
        g2[b-1].append(a-1)
        g[a-1].append(b-1)
for j in range(n):
        if len(g2[j]) == 0:
                ne = j
                break
kazu = [0]*n
for i in range(n):
        kazu[i] = len(g2[i])
ans = [0] * n
def dfs(x):
        z = []
        for i in g[x]:
                kazu[i]-=1
                if kazu[i] == 0:
                        z.append(i)
                        ans[i] = x+1
        for i in z:
                dfs(i)
dfs(ne)
for i in ans:
        print(i)