Buggy python submission (verdict: Time Limit Exceeded):
import collections

N, M = map(int, input().split())

ans = [0] * (N + 1)
parent = [0] * (N + 1)
parent[0] = N+M
child = [[] for i in range(N+1)]
deq = collections.deque()

for i in range(N+M-1):
    p, c = map(int, input().split())
    parent[c] += 1
    child[p].append(c)
root = parent.index(min(parent))
deq.append(root)
while len(deq) > 0:
    v = deq.pop()
    if len(child[v]) > 0:
        for u in child[v]:
            deq.appendleft(u)
            ans[u] = v
for i in range(1,N+1):
    print(ans[i])

Fixed submission:
import collections

N, M = map(int, input().split())

ans = [0] * (N + 1)
parent = [0] * (N + 1)
parent[0] = N+M
child = [[] for i in range(N+1)]
deq = collections.deque()

for i in range(N+M-1):
    p, c = map(int, input().split())
    parent[c] += 1
    child[p].append(c)
root = parent.index(min(parent))
deq.append(root)
while len(deq) > 0:
    v = deq.pop()
    if len(child[v]) > 0:
        for u in child[v]:
            if parent[u] == 1:
                deq.appendleft(u)
                ans[u] = v
            else:
                parent[u] -= 1
for i in range(1,N+1):
    print(ans[i])

Buggy python submission (verdict: Time Limit Exceeded):
import collections

N, M = map(int, input().split())

ans = [0] * (N + 1)
depth = [0] * (N + 1)
parent = [[] for i in range(N+1)]
child = [[] for i in range(N+1)]
deq = collections.deque()

for i in range(N+M-1):
    p, c = map(int, input().split())
    parent[c].append(p)
    child[p].append(c)
for i in range(1, N+M-1):
    if len(parent[i]) == 0:
        deq.append(i)
        break
while len(deq) > 0:
    v = deq.pop()
    if len(child[v]) > 0:
        for u in child[v]:
            deq.appendleft(u)
            ans[u] = v
for i in range(1,N+1):
    print(ans[i])

Fixed submission:
import collections

N, M = map(int, input().split())

ans = [0] * (N + 1)
parent = [0] * (N + 1)
parent[0] = N+M
child = [[] for i in range(N+1)]
deq = collections.deque()

for i in range(N+M-1):
    p, c = map(int, input().split())
    parent[c] += 1
    child[p].append(c)
root = parent.index(min(parent))
deq.append(root)
while len(deq) > 0:
    v = deq.pop()
    if len(child[v]) > 0:
        for u in child[v]:
            if parent[u] == 1:
                deq.appendleft(u)
                ans[u] = v
            else:
                parent[u] -= 1
for i in range(1,N+1):
    print(ans[i])

Buggy python submission (verdict: Time Limit Exceeded):
import collections

N, M = map(int, input().split())

ans = [0] * (N + 1)
depth = [0] * (N + 1)
parent = [[] for i in range(N+1)]
child = [[] for i in range(N+1)]
deq = collections.deque()

for i in range(N+M-1):
    p, c = map(int, input().split())
    parent[c].append(p)
    child[p].append(c)
for i in range(1, N+M-1):
    if len(parent[i]) == 0:
        deq.append(i)
        break
while len(deq) > 0:
    v = deq.pop()
    if len(child[v]) > 0:
        for u in child[v]:
            deq.appendleft(u)
            ans[u] = v
for i in range(1,N+1):
    print(ans[i])

Fixed submission:
import collections

N, M = map(int, input().split())

ans = [0] * (N + 1)
parent = [0] * (N + 1)
parent[0] = N+M
child = [[] for i in range(N+1)]
deq = collections.deque()

for i in range(N+M-1):
    p, c = map(int, input().split())
    parent[c] += 1
    child[p].append(c)
root = parent.index(min(parent))
deq.append(root)
while len(deq) > 0:
    v = deq.pop()
    if len(child[v]) > 0:
        for u in child[v]:
            if parent[u] == 1:
                deq.appendleft(u)
                ans[u] = v
            else:
                parent[u] -= 1
for i in range(1,N+1):
    print(ans[i])