Buggy python submission (verdict: Time Limit Exceeded):
N,M = map(int,input().split())

# 隣接リスト
A = [[] for _ in range(N)]
# 根
root = -1
R = [True]*N

for i in range(N-1+M):
    a,b = map(int,input().split())
    # a -> b
    A[a-1].append(b-1)
    # a -> b のため、bには親がいる
    R[b-1] = False

# 親判定
for i in range(N):
    if R[i]:
        root = i
        break

# 出力すべき木の親を確定させる
P = [-1]*N
P[root] = -1

from collections import deque
Q = deque([root])
while Q!=deque([]):
    tmpQ = deque([])
    for q in Q:
        for i in A[q]:
            # 親の更新
            P[i] = q
            # 自身をtmpQに
            tmpQ.append(i)
    Q = tmpQ

for i in range(N):
    print(P[i]+1)





Fixed submission:
N,M = map(int,input().split())
AB = [tuple(map(int,input().split())) for _ in range(N-1+M)]

A = [[] for _ in range(N)]
P = [0]*N
C = [0]*N
root = -1

for (a,b) in AB:
    # a -> b
    A[a-1].append(b-1)
    C[b-1] += 1

for i in range(N):
    if C[i]==0:
        root = i
P[root] = -1

from collections import deque
que = deque((a,root) for a in A[root])
while que:
    n,par = que.popleft()
    C[n] -= 1
    if C[n]==0:
        P[n] = par
        for i in A[n]:
            que.append((i,n))

for p in P:
    print(p+1)

Buggy python submission (verdict: Time Limit Exceeded):
N,M = map(int,input().split())

# 隣接リスト
A = [[] for _ in range(N)]
# 根
root = -1
R = [True]*N

for i in range(N-1+M):
    a,b = map(int,input().split())
    # a -> b
    A[a-1].append(b-1)
    # a -> b のため、bには親がいる
    R[b-1] = False

# 親判定
for i in range(N):
    if R[i]:
        root = i
        break

# 出力すべき木の親を確定させる
P = [-1]*N
P[root] = -1

from collections import deque
Q = deque([root])
while Q!=[]:
    tmpQ = deque([])
    for q in Q:
        for i in A[q]:
            # 親の更新
            P[i] = q
            # 自身をtmpQに
            tmpQ.append(i)
    Q = tmpQ

for i in range(N):
    print(P[i]+1)





Fixed submission:
N,M = map(int,input().split())
AB = [tuple(map(int,input().split())) for _ in range(N-1+M)]

A = [[] for _ in range(N)]
P = [0]*N
C = [0]*N
root = -1

for (a,b) in AB:
    # a -> b
    A[a-1].append(b-1)
    C[b-1] += 1

for i in range(N):
    if C[i]==0:
        root = i
P[root] = -1

from collections import deque
que = deque((a,root) for a in A[root])
while que:
    n,par = que.popleft()
    C[n] -= 1
    if C[n]==0:
        P[n] = par
        for i in A[n]:
            que.append((i,n))

for p in P:
    print(p+1)

Buggy python submission (verdict: Time Limit Exceeded):
N,M = map(int,input().split())

# 隣接リスト
A = [[] for _ in range(N)]
# 根
root = -1
R = [True]*N

for i in range(N-1+M):
    a,b = map(int,input().split())
    # a -> b
    A[a-1].append(b-1)
    # a -> b のため、bには親がいる
    R[b-1] = False

# 親判定
for i in range(N):
    if R[i]:
        root = i
        break

# 出力すべき木の親を確定させる
P = [-1]*N
P[root] = -1

Q = [root]
while Q!=[]:
    tmpQ = []
    for q in Q:
        for i in A[q]:
            # 親の更新
            P[i] = q
            # 自身をtmpQに
            tmpQ.append(i)
    Q = tmpQ

for i in range(N):
    print(P[i]+1)





Fixed submission:
N,M = map(int,input().split())
AB = [tuple(map(int,input().split())) for _ in range(N-1+M)]

A = [[] for _ in range(N)]
P = [0]*N
C = [0]*N
root = -1

for (a,b) in AB:
    # a -> b
    A[a-1].append(b-1)
    C[b-1] += 1

for i in range(N):
    if C[i]==0:
        root = i
P[root] = -1

from collections import deque
que = deque((a,root) for a in A[root])
while que:
    n,par = que.popleft()
    C[n] -= 1
    if C[n]==0:
        P[n] = par
        for i in A[n]:
            que.append((i,n))

for p in P:
    print(p+1)

Buggy python submission (verdict: Time Limit Exceeded):
N,M = map(int,input().split())
AB = [tuple(map(int,input().split())) for _ in range(N-1+M)]

# 隣接リスト
A = [[] for _ in range(N)]

# 根
root = -1
R = [True]*N
for (a,b) in AB:
    R[b-1] = False
    A[a-1].append(b-1)

for i in range(N):
    if R[i]:
        root = i
        break

# 親
P = [-1]*N
P[root] = -1

Q = [root]
while Q!=[]:
    tmpQ = []
    for q in Q:
        for i in A[q]:
            # 親の更新
            P[i] = q
            # 自身をtmpQに
            tmpQ.append(i)
    Q = tmpQ

for i in range(N):
    print(P[i]+1)





Fixed submission:
N,M = map(int,input().split())
AB = [tuple(map(int,input().split())) for _ in range(N-1+M)]

A = [[] for _ in range(N)]
P = [0]*N
C = [0]*N
root = -1

for (a,b) in AB:
    # a -> b
    A[a-1].append(b-1)
    C[b-1] += 1

for i in range(N):
    if C[i]==0:
        root = i
P[root] = -1

from collections import deque
que = deque((a,root) for a in A[root])
while que:
    n,par = que.popleft()
    C[n] -= 1
    if C[n]==0:
        P[n] = par
        for i in A[n]:
            que.append((i,n))

for p in P:
    print(p+1)