Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())


from heapq import heappush, heappop


def dijkstra(graph: list, start, INF=float('inf')) -> list:
    n = len(graph)
    dist = [INF] * n
    visited = [False] * n

    dist[start] = 0
    que = [(0, start)]

    while que:
        cost, cur = heappop(que)
        if visited[cur]:
            continue

        visited[cur] = True

        for edgecost, nextnode in graph[cur]:
            nextcost = cost + edgecost
            if nextcost < dist[nextnode]:
                dist[nextnode] = nextcost
                heappush(que, (nextcost, nextnode))

    return dist

n, m = li()

edges = []
ins = [[] for _ in range(n)]
ots = [[] for _ in range(n)]

for _ in range(m):
    u,v = li_()
    ins[v].append(u)
    ots[u].append(v)
    edges.append((u, v))

graph = [set() for _ in range(n)]

for ui, vi in edges:
    for st in ins[ui]:
        for to in ots[vi]:
            graph[st].add((1,to))

s, t = li_()
INF = 10**8+1

dist = dijkstra(graph, s, INF)

print(-1 if dist[t] == INF else dist[t])

Fixed submission:
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())


from heapq import heappush, heappop

def dijkstra(graph: list, start, INF=float('inf')) -> list:
    n = len(graph)
    dist = [INF ] *n
    visited = [False ] *n

    dist[start] = 0
    que = [(0, start)]

    while que:
        cost, cur = heappop(que)

        if visited[cur]:
            continue

        visited[cur] = True

        for nextnode, edgecost in graph[cur]:
            nextcost = cost + edgecost
            if nextcost < dist[nextnode]:
                dist[nextnode] = nextcost
                heappush(que, (nextcost, nextnode))

    return dist


n, m = li()

graph = [[] for _ in range(3*n)]
for _ in range(m):
    u, v = li_()
    for i in range(3):
        graph[i*n + u].append([(i+1)%3 * n + v, 1])

s, t = li_()
INF = 1<<62
dist = dijkstra(graph, s, INF)

print(-1 if dist[t] == INF else dist[t] // 3)
Buggy python submission (verdict: Runtime Error):
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

n, k = li()

if k > (n-1) * (n-2):
    print(-1)

else:
    edges = []
    for i in range(1,n):
        edges.append((1, i+1))

    surp = (n-1)*(n-2) // 2 - k
    for frm in range(1, n):
        for to in range(frm+1, n):
            if surp == 0:
                break

            edges.append((frm+1, to+1))
            surp -= 1


print(len(edges))
for ei, ej in edges:
    print(ei, ej)

Fixed submission:
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

n, k = li()

if k > (n-1) * (n-2) // 2:
    print(-1)

else:
    edges = []
    for i in range(1,n):
        edges.append((1, i+1))

    surp = (n-1)*(n-2) // 2 - k
    for frm in range(1, n):
        for to in range(frm+1, n):
            if surp == 0:
                break

            edges.append((frm+1, to+1))
            surp -= 1


    print(len(edges))
    for ei, ej in edges:
        print(ei, ej)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

n, m = li()
s = list(li())
t = list(li())

MOD = 10**9 + 7

dp = [[0]*(m+1) for _ in range(n+1)]
# for i in range(n+1):
#     dp[i][0] = 1
#
# for j in range(m+1):
#     dp[0][j] = 1

for i in range(n):
    for j in range(m):
        if s[i] == t[j]:
            dp[i+1][j+1] = (dp[i][j+1] + dp[i+1][j] + 1) % MOD
        else:
            dp[i + 1][j + 1] = (dp[i+1][j] + dp[i][j+1] - dp[i][j]) % MOD

print((dp[n][m] + 1) % MOD)


Fixed submission:
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

n, m = li()
s = list(li())
t = list(li())

MOD = 10**9 + 7

dp = [[0]*(m+1) for _ in range(n+1)]
# for i in range(n+1):
#     dp[i][0] = 1
#
# for j in range(m+1):
#     dp[0][j] = 1

for i in range(n):
    for j in range(m):
        if s[i] == t[j]:
            dp[i+1][j+1] = (dp[i][j+1] + dp[i+1][j] + 1) % MOD
        else:
            dp[i + 1][j + 1] = (dp[i+1][j] + dp[i][j+1] - dp[i][j]) % MOD

print((dp[n][m] + 1) % MOD)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

from collections import deque

h,w = li()
a = [list(lc()) for _ in range(h)]

que = deque()

dist = [[-1]*w for _ in range(h)]

for i, ai in enumerate(a):
    for j, aij in enumerate(ai):
        if aij == "#":
            que.append((0, (i, j)))
            dist[i][j] = 0
        
drs = [0,1,0,-1]
dcs = [1,0,-1,0]

while que:
    cur, (r,c) = que.popleft()
    dist[r][c] = cur
    
    for dr, dc in zip(drs, dcs):
        if 0 <= r+dr < h and 0 <= c+dc < w:
            if dist[r+dr][c+dc] == -1:
                dist[r+dr][c+dc] = cur + 1
                que.append((cur+1, (r+dr, c+dc)))
                
ans = 0
for di in dist:
    for dij in di:
        ans = max(ans, dij)
print(ans)

Fixed submission:
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

from collections import deque

h,w = li()
a = [list(lc()) for _ in range(h)]

que = deque()

dist = [[-1]*w for _ in range(h)]

for i, ai in enumerate(a):
    for j, aij in enumerate(ai):
        if aij == "#":
            que.append((0, (i, j)))
            dist[i][j] = 0
        
drs = [0,1,0,-1]
dcs = [1,0,-1,0]

while que:
    cur, (r,c) = que.popleft()
    dist[r][c] = cur
    
    for dr, dc in zip(drs, dcs):
        if 0 <= r+dr < h and 0 <= c+dc < w:
            if dist[r+dr][c+dc] == -1:
                dist[r+dr][c+dc] = cur + 1
                que.append((cur+1, (r+dr, c+dc)))
                
ans = 0
for di in dist:
    for dij in di:
        ans = max(ans, dij)
print(ans)
Buggy python submission (verdict: Runtime Error):
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

idx = {chr(i): [] for i in range(65, 92)}

n,q = li()
s = ns()


from_left = [(si, 'L') for si in s]
from_right = [(si, 'R') for si in s[::-1]]

query = [tuple(ls()) for _ in range(q)]

left_cur = 0
right_cur = 0

for qi in query[::-1]:
    if qi == from_left[left_cur]:
        left_cur += 1
        
    if qi == from_right[right_cur]:
        right_cur += 1
        
print(max(n - (left_cur + right_cur), 0))

Fixed submission:
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

# シミュレーション関数
def to_end(num, board, query, goal):
    for (qs, qd) in query:
        if qs == board[num]:
            if qd == 'L':
                num -= 1
            elif qd == 'R':
                num += 1
                
            if num == goal:
                return True
            
    return False


# 左へ二分探索
def find_rightest_left(board, query):
    left = 0
    right = len(board)-1
    
    while right-left > 1:
        mid = (left+right) // 2
        if to_end(mid, board, query, 0):
            left = mid
        else:
            right = mid
            
    return left
            
# 右へ二分探索
def find_leftest_right(board, query):
    R = len(board)-1
    left = 0
    right = R
    
    while right-left > 1:
        mid = (left+right) // 2
        if to_end(mid, board, query, R):
            right = mid
        else:
            left = mid
            
    return R - right
    
n,q = li()
board = "0" + ns() + "0"
query = [tuple(ls()) for _ in range(q)]

rightest_left = find_rightest_left(board, query)
leftest_right = find_leftest_right(board, query)

print(max(n - rightest_left - leftest_right, 0))
Buggy python submission (verdict: Runtime Error):
s = ns()
atcg = set(["A", "T", "C", "G"])

ans = 0
tmp = 0
for si in s:
  if si in atcg:
    tmp += 1
  else:
    ans = max(ans, tmp)
    tmp = 0
    
ans = max(ans, tmp)
print(ans)

Fixed submission:
s = input()
atcg = set(["A", "T", "C", "G"])

ans = 0
tmp = 0
for si in s:
  if si in atcg:
    tmp += 1
  else:
    ans = max(ans, tmp)
    tmp = 0
    
ans = max(ans, tmp)
print(ans)

Buggy python submission (verdict: Runtime Error):
s = ns()
atcg = "ATCG"

ans = 0
tmp = 0
for si in s:
  if si in atcg:
    tmp += 1
  else:
    ans = max(ans, tmp)
    tmp = 0
    
ans = max(ans, tmp)
print(ans)

Fixed submission:
s = input()
atcg = set(["A", "T", "C", "G"])

ans = 0
tmp = 0
for si in s:
  if si in atcg:
    tmp += 1
  else:
    ans = max(ans, tmp)
    tmp = 0
    
ans = max(ans, tmp)
print(ans)
Buggy python submission (verdict: Runtime Error):
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

# i-bit目とj-bit目を入れ替える
def swap_digit(num:int, i:int, j:int):
    return num ^ (1<<i) ^ (1<<j)

# 2つの数のbitが異なる桁を特定する
def different_digit(n: int, a: int, b: int):
    ret = -1
    for digit in range(n, -1, -1):
        if (a^b) & (1<<digit):
            return digit
        
    return ret

# 上位bitから再帰的に決める
def rec(n: int, a: int, b: int):
    if n == 1:
        return [a,b]
    
    dd = different_digit(n,a,b)
    
    if dd == n-1:
        na = a & ~(1<<(n-1))
        nb = b & ~(1<<(n-1))
    else:
        na = swap_digit(a, n-1, dd) & ~(1<<(n-1))
        nb = swap_digit(b, n-1, dd) & ~(1<<(n-1))
        
    first = rec(n-1, na, na^1)
    latte = rec(n-1, na^1, nb)
    
    if a & (1<<(n-1)):
        ret = list(map(lambda x: x | (1<<(n-1)), first)) + latte
    else:
        ret = first + list(map(lambda x: x | (1<<(n-1)), latte))
        
    return [swap_digit(reti, n-1, dd) for reti in ret]


n,a,b = li()
if bin(a).count('1') % 2 == bin(b).count('1') % 2:
    print("NO")
    
else:
    print("YES")
    print(*rec(n,a,b))

Fixed submission:
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

# i-bit目とj-bit目を入れ替える
def swap_digit(num:int, i:int, j:int):
    if num & (1<<i) and not (num & (1<<j)):
        return num - (1<<i) + (1<<j)
    
    elif not (num & (1<<i)) and num & (1<<j):
        return num + (1<<i) - (1<<j)
    
    else:
        return num
    

# 2つの数のbitが異なる桁を特定する
def different_digit(n: int, a: int, b: int):
    ret = n-1

    for digit in range(n-1, -1, -1):
        if (a^b) & (1<<digit):
            return digit
        
    return ret

# 上位bitから再帰的に決める
def rec(n: int, a: int, b: int):
    if n == 1:
        return [a,b]
    
    dd = different_digit(n,a,b)
    
    a = swap_digit(a, n-1, dd)
    b = swap_digit(b, n-1, dd)
    na = a & ((1<<(n-1)) - 1)
    nb = b & ((1<<(n-1)) - 1)
    
        
    first = rec(n-1, na, na^1)
    latte = rec(n-1, na^1, nb)

    if a & (1<<(n-1)):
        ret = list(map(lambda x: x + (1<<(n-1)), first)) + latte
    else:
        ret = first + list(map(lambda x: x + (1<<(n-1)), latte))

    return [swap_digit(reti, n-1, dd) for reti in ret]


n,a,b = li()
if bin(a).count('1') % 2 == bin(b).count('1') % 2:
    print("NO")
    
else:
    print("YES")
    print(*rec(n,a,b))
Buggy python submission (verdict: Runtime Error):
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

num_cost = {1: 2,
            2: 5,
            3: 5,
            4: 4,
            5: 5,
            6: 6,
            7: 3,
            8: 6,
            9: 5}

chosen = {}

n, m = li()
a = list(li())

for ai in a:
    chosen[str(ai)] = num_cost[ai]

dp = ["ng"]*(n+1)
dp[0] = ""
for i in range(n):
    if dp[i] == "ng":
        continue

    for num, cost in chosen.items():
        if i + cost <= n and dp[i+cost] == "ng":
            dp[i+cost] = num + dp[i]

        elif i + cost <= n:
            dp[i+cost] = str(max(int(dp[i+cost]), int(num + dp[i])))

print(dp[n]

Fixed submission:
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

dic = {1: 2,
       2: 5,
       3: 5,
       4: 4,
       5: 5,
       6: 6,
       7: 3,
       8: 7,
       9: 6}

n,m = li()
a = list(li())

use = {}
for ai in a:
    if dic[ai] not in use.keys():
        use.update({dic[ai]: ai})
    else:
        use[dic[ai]] = max(use[dic[ai]], ai)
        
# 最大順
desc = {}
edge = {}
for idx, num in enumerate(sorted(use.values(), reverse=True)):
    desc.update({idx: num})
    edge.update({idx: dic[num]})
    
        
# 遷移
INF = float('inf')
dp = [[-INF]*len(use) for _ in range(n+1)]
dp[0] = [0]*len(use)
for i in range(1, n+1):
    cand = dp[i].copy()
    for idx in range(len(use)):
        if i - edge[idx] < 0:
            continue

        if sum(cand) < sum(dp[i-edge[idx]])+1:
            tmp = dp[i-edge[idx]].copy()
            tmp[idx] += 1
            cand = tmp.copy()
            

        elif sum(cand) == sum(dp[i-edge[idx]])+1:
            tmp = dp[i-edge[idx]].copy()
            tmp[idx] += 1
            
            for orgi, tmpi in zip(cand, tmp):
                if orgi == tmpi:
                    continue
                
                elif orgi > tmpi:
                    break
                
                else:
                    cand = tmp.copy()
                    break
                
    dp[i] = cand
                

ans = ""
for num, cnt in zip(desc.values(), dp[-1]):
    ans = ans + str(num)*cnt
    
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**8) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

# D以下orUより大きい場所への遷移
def to0or4(base: int, nex: int):
    return base + nex

# Dより大きくS以下 or Gより大きくU以下の場所への遷移
def to1or3(base: int, nex: int):
    if nex == 0:
        return base + 2
    
    elif nex % 2:
        return base + 1
    
    else:
        return base


# Sより大きくG以下の場所への遷移
def to2(base: int, nex: int):
    if nex % 2:
        return base
    
    else:
        return base + 1
    
n = ni()
a = [ni() for _ in range(n)]
suma = sum(a)  

# 配るDP
dp = [[suma]*5 for _ in range(n+1)]
dp[0] = [0]*5

for i in range(n):
    for idx in range(5):
        for jdx in range(idx,5):
            
            if jdx == 0 or jdx == 4:
                dp[i+1][jdx] = min(dp[i+1][jdx],
                                   to0or4(dp[i][idx], a[i]))
            
            elif jdx == 1 or jdx == 3:
                dp[i+1][jdx] = min(dp[i+1][jdx],
                                   to1or3(dp[i][idx], a[i]))
                
            elif jdx == 2:
                dp[i+1][jdx] = min(dp[i+1][jdx],
                                   to2(dp[i][idx], a[i]))
   
print(min(dp[n]))

Fixed submission:
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**8) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

# D以下orUより大きい場所への遷移
def to0or4(base: int, nex: int):
    return base + nex

# Dより大きくS以下 or Gより大きくU以下の場所への遷移
def to1or3(base: int, nex: int):
    if nex == 0:
        return base + 2
    
    elif nex % 2:
        return base + 1
    
    else:
        return base


# Sより大きくG以下の場所への遷移
def to2(base: int, nex: int):
    if nex % 2:
        return base
    
    else:
        return base + 1
    
n = ni()
a = [ni() for _ in range(n)]
suma = sum(a)  

# 配るDP
dp = [[suma]*5 for _ in range(n+1)]
dp[0] = [0]*5

for i in range(n):
    for idx in range(5):
        for jdx in range(idx,5):
            
            if jdx == 0 or jdx == 4:
                dp[i+1][jdx] = min(dp[i+1][jdx],
                                   to0or4(dp[i][idx], a[i]))
            
            elif jdx == 1 or jdx == 3:
                dp[i+1][jdx] = min(dp[i+1][jdx],
                                   to1or3(dp[i][idx], a[i]))
                
            elif jdx == 2:
                dp[i+1][jdx] = min(dp[i+1][jdx],
                                   to2(dp[i][idx], a[i]))
   
print(min(dp[n]))
Buggy python submission (verdict: Runtime Error):
import sys
#stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

from collections import deque

n, m = li()
graph = [[] for _ in range(n)]

ins = [0]*n
par = [0]*n

# 親をたどるグラフを作る
for _ in range(n-1+m):
    a,b = li_()
    graph[a].append(b)
    ins[b] += 1

# 入次数が尽きたものからスタートに
start = ins.index(0)
que = deque([start])
while que:
    cur = que.popleft()
    for child in graph[cur]:
        ins[child] -= 1
        if ins[child] == 0:
            que.append(child)
            par[child] = cur+1
    
for pi in par:
    print(pi)

Fixed submission:
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

from collections import deque

n, m = li()
graph = [[] for _ in range(n)]

ins = [0]*n
par = [0]*n

# 親をたどるグラフを作る
for _ in range(n-1+m):
    a,b = li_()
    graph[a].append(b)
    ins[b] += 1

# 入次数が尽きたものからスタートに
start = ins.index(0)
que = deque([start])
while que:
    cur = que.popleft()
    for child in graph[cur]:
        ins[child] -= 1
        if ins[child] == 0:
            que.append(child)
            par[child] = cur+1
    
for pi in par:
    print(pi)