Buggy python submission (verdict: Time Limit Exceeded):
#import sys
#sys.setrecursionlimit(10**8)
from collections import deque

n, m = map(int, input().split())
depth = [0 for _ in range(n)]
chil = [[] for _ in range(n)]
rank = [0 for _ in range(n)]
parent = [-1 for _ in range(n)]
visited = [False for _ in range(n)]
for _ in range(n+m-1):
	a, b = map(int, input().split())
	chil[a-1].append(b-1)
	rank[b-1] += 1

#print(chil)
for i in range(n):
	if rank[i] == 0:
		par = i
		break

parent[par] = -1

def bfs(x):
	que = deque([[x, 0]])
	while que:
		l = que.pop()
		y, d = l[0], l[1]
		if d < depth[y]:
			continue
		for c in chil[y]:
			if depth[c] == -1:
				depth[c] = d+1
				parent[c] = y
				que.appendleft([c, d+1])
			elif depth[c] < d+1:
				depth[c] = d+1
				parent[c] = y
				que.appendleft([c, d+1])

	return

bfs(par)
#def dfs(x, d):
#	for c in chil[x]:
#		if depth[c] == -1:
#			depth[c] = d+1
#			parent[c] = x
#			dfs(c, d+1)
#		elif depth[c] < d+1:
#			depth[c] = d+1
#			parent[c] = x
#			dfs(c, d+1)
#
#	return
#
#dfs(par, 0)

for p in parent:
	print(p+1)

Fixed submission:
import sys
sys.setrecursionlimit(10**8)
#from collections import deque

n, m = map(int, input().split())
depth = [0 for _ in range(n)]
chil = [[] for _ in range(n)]
rank = [0 for _ in range(n)]
parent = [-1 for _ in range(n)]
#visited = [False for _ in range(n)]
for _ in range(n+m-1):
	a, b = map(int, input().split())
	chil[a-1].append(b-1)
	rank[b-1] += 1

#print(chil)
for i in range(n):
	if rank[i] == 0:
		par = i
		break

parent[par] = -1

#def bfs(x):
#	que = deque([[x, 0]])
#	while que:
#		l = que.pop()
#		y, d = l[0], l[1]
#		print(y, d)
#		if d < depth[y]:
#			continue
#		for c in chil[y]:
#			if depth[c] == -1:
#				depth[c] = d+1
#				parent[c] = y
#				que.appendleft([c, d+1])
#			elif depth[c] < d+1:
#				depth[c] = d+1
#				parent[c] = y
#				que.appendleft([c, d+1])
#
#	return

#bfs(par)

def dfs(x):
	true_chil = []
	for c in chil[x]:
		if rank[c] == 1:
			true_chil.append(c)
		else:
			rank[c] -= 1
	for t in true_chil:
		parent[t] = x
		dfs(t)

	return

dfs(par)

for p in parent:
	print(p+1)

Buggy python submission (verdict: Time Limit Exceeded):
#import sys
#sys.setrecursionlimit(10**8)
from collections import deque

n, m = map(int, input().split())
depth = [0 for _ in range(n)]
chil = [[] for _ in range(n)]
rank = [0 for _ in range(n)]
parent = [-1 for _ in range(n)]
visited = [False for _ in range(n)]
for _ in range(n+m-1):
	a, b = map(int, input().split())
	chil[a-1].append(b-1)
	rank[b-1] += 1

#print(chil)
for i in range(n):
	if rank[i] == 0:
		par = i
		break

parent[par] = -1

def bfs(x):
	que = deque([[x, 0]])
	while que:
		l = que.pop()
		y, d = l[0], l[1]
		if d < depth[y]:
			continue
		for c in chil[y]:
			if depth[c] == -1:
				depth[c] = d+1
				parent[c] = y
				que.appendleft([c, d+1])
			elif depth[c] < d+1:
				depth[c] = d+1
				parent[c] = y
				que.appendleft([c, d+1])

	return

bfs(par)
#def dfs(x, d):
#	for c in chil[x]:
#		if depth[c] == -1:
#			depth[c] = d+1
#			parent[c] = x
#			dfs(c, d+1)
#		elif depth[c] < d+1:
#			depth[c] = d+1
#			parent[c] = x
#			dfs(c, d+1)
#
#	return
#
#dfs(par, 0)

for p in parent:
	print(p+1)

Fixed submission:
import sys
sys.setrecursionlimit(10**8)
#from collections import deque

n, m = map(int, input().split())
depth = [0 for _ in range(n)]
chil = [[] for _ in range(n)]
rank = [0 for _ in range(n)]
parent = [-1 for _ in range(n)]
#visited = [False for _ in range(n)]
for _ in range(n+m-1):
	a, b = map(int, input().split())
	chil[a-1].append(b-1)
	rank[b-1] += 1

#print(chil)
for i in range(n):
	if rank[i] == 0:
		par = i
		break

parent[par] = -1

#def bfs(x):
#	que = deque([[x, 0]])
#	while que:
#		l = que.pop()
#		y, d = l[0], l[1]
#		print(y, d)
#		if d < depth[y]:
#			continue
#		for c in chil[y]:
#			if depth[c] == -1:
#				depth[c] = d+1
#				parent[c] = y
#				que.appendleft([c, d+1])
#			elif depth[c] < d+1:
#				depth[c] = d+1
#				parent[c] = y
#				que.appendleft([c, d+1])
#
#	return

#bfs(par)

def dfs(x):
	true_chil = []
	for c in chil[x]:
		if rank[c] == 1:
			true_chil.append(c)
		else:
			rank[c] -= 1
	for t in true_chil:
		parent[t] = x
		dfs(t)

	return

dfs(par)

for p in parent:
	print(p+1)

Buggy python submission (verdict: Time Limit Exceeded):
#import sys
#sys.setrecursionlimit(10**8)
from collections import deque

n, m = map(int, input().split())
depth = [0 for _ in range(n)]
chil = [[] for _ in range(n)]
rank = [0 for _ in range(n)]
parent = [-1 for _ in range(n)]
visited = [False for _ in range(n)]
for _ in range(n+m-1):
	a, b = map(int, input().split())
	chil[a-1].append(b-1)
	rank[b-1] += 1

#print(chil)
for i in range(n):
	if rank[i] == 0:
		par = i
		break

parent[par] = -1

def bfs(x):
	que = deque([[x, 0]])
	while que:
		l = que.pop()
		y, d = l[0], l[1]
		for c in chil[y]:
			if depth[c] == -1:
				depth[c] = d+1
				parent[c] = y
				que.appendleft([c, d+1])
			elif depth[c] < d+1:
				depth[c] = d+1
				parent[c] = y
				que.appendleft([c, d+1])

	return

bfs(par)
#def dfs(x, d):
#	for c in chil[x]:
#		if depth[c] == -1:
#			depth[c] = d+1
#			parent[c] = x
#			dfs(c, d+1)
#		elif depth[c] < d+1:
#			depth[c] = d+1
#			parent[c] = x
#			dfs(c, d+1)
#
#	return
#
#dfs(par, 0)

for p in parent:
	print(p+1)

Fixed submission:
import sys
sys.setrecursionlimit(10**8)
#from collections import deque

n, m = map(int, input().split())
depth = [0 for _ in range(n)]
chil = [[] for _ in range(n)]
rank = [0 for _ in range(n)]
parent = [-1 for _ in range(n)]
#visited = [False for _ in range(n)]
for _ in range(n+m-1):
	a, b = map(int, input().split())
	chil[a-1].append(b-1)
	rank[b-1] += 1

#print(chil)
for i in range(n):
	if rank[i] == 0:
		par = i
		break

parent[par] = -1

#def bfs(x):
#	que = deque([[x, 0]])
#	while que:
#		l = que.pop()
#		y, d = l[0], l[1]
#		print(y, d)
#		if d < depth[y]:
#			continue
#		for c in chil[y]:
#			if depth[c] == -1:
#				depth[c] = d+1
#				parent[c] = y
#				que.appendleft([c, d+1])
#			elif depth[c] < d+1:
#				depth[c] = d+1
#				parent[c] = y
#				que.appendleft([c, d+1])
#
#	return

#bfs(par)

def dfs(x):
	true_chil = []
	for c in chil[x]:
		if rank[c] == 1:
			true_chil.append(c)
		else:
			rank[c] -= 1
	for t in true_chil:
		parent[t] = x
		dfs(t)

	return

dfs(par)

for p in parent:
	print(p+1)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**8)

n, m = map(int, input().split())
depth = [0 for _ in range(n)]
chil = [[] for _ in range(n)]
rank = [0 for _ in range(n)]
parent = [-1 for _ in range(n)]
for _ in range(n+m-1):
	a, b = map(int, input().split())
	chil[a-1].append(b-1)
	rank[b-1] += 1

#print(chil)
for i in range(n):
	if rank[i] == 0:
		par = i
		break

parent[par] = -1

def dfs(x, d):
	for c in chil[x]:
		if depth[c] == -1:
			depth[c] = d+1
			parent[c] = x
			dfs(c, d+1)
		elif depth[c] < d+1:
			depth[c] = d+1
			parent[c] = x
			dfs(c, d+1)

	return

dfs(par, 0)

for p in parent:
	print(p+1)

Fixed submission:
import sys
sys.setrecursionlimit(10**8)
#from collections import deque

n, m = map(int, input().split())
depth = [0 for _ in range(n)]
chil = [[] for _ in range(n)]
rank = [0 for _ in range(n)]
parent = [-1 for _ in range(n)]
#visited = [False for _ in range(n)]
for _ in range(n+m-1):
	a, b = map(int, input().split())
	chil[a-1].append(b-1)
	rank[b-1] += 1

#print(chil)
for i in range(n):
	if rank[i] == 0:
		par = i
		break

parent[par] = -1

#def bfs(x):
#	que = deque([[x, 0]])
#	while que:
#		l = que.pop()
#		y, d = l[0], l[1]
#		print(y, d)
#		if d < depth[y]:
#			continue
#		for c in chil[y]:
#			if depth[c] == -1:
#				depth[c] = d+1
#				parent[c] = y
#				que.appendleft([c, d+1])
#			elif depth[c] < d+1:
#				depth[c] = d+1
#				parent[c] = y
#				que.appendleft([c, d+1])
#
#	return

#bfs(par)

def dfs(x):
	true_chil = []
	for c in chil[x]:
		if rank[c] == 1:
			true_chil.append(c)
		else:
			rank[c] -= 1
	for t in true_chil:
		parent[t] = x
		dfs(t)

	return

dfs(par)

for p in parent:
	print(p+1)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**8)

n, m = map(int, input().split())
depth = [0 for _ in range(n)]
chil = [[] for _ in range(n)]
rank = [0 for _ in range(n)]
parent = [-1 for _ in range(n)]
for _ in range(n+m-1):
	a, b = map(int, input().split())
	chil[a-1].append(b-1)
	rank[b-1] += 1

#print(chil)
for i in range(n):
	if rank[i] == 0:
		par = i
		break

parent[par] = -1

def dfs(x, d):
	for c in chil[x]:
		if depth[c] == -1:
			depth[c] = d+1
			parent[c] = x
			dfs(c, d+1)
		elif depth[c] < d+1:
			depth[c] = d+1
			parent[c] = x
			dfs(c, d+1)

	return

dfs(par, 0)

for p in parent:
	print(p+1)

Fixed submission:
import sys
sys.setrecursionlimit(10**8)
#from collections import deque

n, m = map(int, input().split())
depth = [0 for _ in range(n)]
chil = [[] for _ in range(n)]
rank = [0 for _ in range(n)]
parent = [-1 for _ in range(n)]
#visited = [False for _ in range(n)]
for _ in range(n+m-1):
	a, b = map(int, input().split())
	chil[a-1].append(b-1)
	rank[b-1] += 1

#print(chil)
for i in range(n):
	if rank[i] == 0:
		par = i
		break

parent[par] = -1

#def bfs(x):
#	que = deque([[x, 0]])
#	while que:
#		l = que.pop()
#		y, d = l[0], l[1]
#		print(y, d)
#		if d < depth[y]:
#			continue
#		for c in chil[y]:
#			if depth[c] == -1:
#				depth[c] = d+1
#				parent[c] = y
#				que.appendleft([c, d+1])
#			elif depth[c] < d+1:
#				depth[c] = d+1
#				parent[c] = y
#				que.appendleft([c, d+1])
#
#	return

#bfs(par)

def dfs(x):
	true_chil = []
	for c in chil[x]:
		if rank[c] == 1:
			true_chil.append(c)
		else:
			rank[c] -= 1
	for t in true_chil:
		parent[t] = x
		dfs(t)

	return

dfs(par)

for p in parent:
	print(p+1)