Harryxun/GPT-100-sft-dataset huggingface dataset:
Problem p02936 (python)

Fixed submission:
N, Q = map(int, input().split())

ab_list = []
tree_dict = {}
for i in range(N-1):
    a, b = map(int, input().split())
    ab_list.append(a)
    ab_list.append(b)

    if a not in tree_dict:
        tree_dict[a] = [b]
    else:
        tree_dict[a].append(b)

value_list = list(set(ab_list))

px_dict = {}
for i in range(Q):
    p, x = map(int, input().split())
    if p in px_dict:
        px_dict[p] += x
    else:
        px_dict[p] = x

out = ""
for v in value_list:
    if v in px_dict:
        x = px_dict[v]
        if v in tree_dict:
            tree_list = tree_dict[v]
            for t in tree_list:
                if t in px_dict:
                    px_dict[t] += x
                else:
                    px_dict[t] = x
        o = str(px_dict[v])
    else:
        o = str(0)
    out += o + " "

print(out[:-1])

Buggy submission (verdict: Runtime Error):
N, Q = map(int, input().split())

ab_list = []
tree_dict = {}
for i in range(N-1):
    a, b = map(int, input().split())
    ab_list.append(a)
    ab_list.append(b)

    if a not in tree_dict:
        tree_dict[a] = [b]
    else:
        tree_dict[a].append(b)

#value_dict = {k:0 for k in list(set(ab_list))}                                          
value_list = list(set(ab_list))

px_dict = {}
for i in range(Q):
    p, x = map(int, input().split())
    if p in px_dict:
        px_dict[p] += x
    else:
        px_dict[p] = x

out = ""
for v in value_list:
    x = px_dict[v]
    if v in tree_dict:
        tree_list = tree_dict[v]
        for t in tree_list:
            if t in px_dict:
                px_dict[t] += x
            else:
                px_dict[t] = x
    if v in px_dict:
        o = str(px_dict[v])
    else:
        o = str(0)
    out += o + " "

print(out)
Problem p02995 (python)

Fixed submission:
def gcd(x, y):
    if y == 0:
        return x
    else:
        return gcd(y, x % y)

A, B, C, D = map(int, input().split())

bc = B // C
bd = B // D
be = B // int((C*D/gcd(C, D)))
BB = B - bc - bd + be

ac = (A-1) // C
ad = (A-1) // D
ae = (A-1) // int(C*D/gcd(C, D))
AA = (A-1) - ac - ad + ae

print(BB-AA)

Buggy submission (verdict: Runtime Error):
import math

A, B, C, D = map(int, input().split())

bc = B // C
bd = B // D
be = B // int((C*D/math.gcd(C, D)))
BB = B - bc - bd + be

ac = (A-1) // C
ad = (A-1) // D
ae = (A-1) // int(C*D/math.gcd(C, D))
AA = (A-1) - ac - ad + ae

print(BB-AA)
Problem p02999 (python)

Fixed submission:
x, a = input().split()
x = int(x)
a = int(a)

if x < a:
  print(0)
else:
  print(10)

Buggy submission (verdict: Runtime Error):
x, a = input()
x = int(x)
a = int(a)

if x < a:
  print(0)
else:
  print(10)
Problem p03105 (python)

Fixed submission:
a,b,c=input().split()

a=int(a)
b=int(b)
c=int(c)

res=0
if b/a > c:
    res=c
else:
    res=b/a

print(int(res))

Buggy submission (verdict: Runtime Error):
a,b,c=input().split()

a=int(a)
b=int(b)
c=int(c)

res=0
if b/a > c:
    res=c
else:
    res=b/a
print(int(res)
Problem p03206 (python)

Fixed submission:
a = int(input())

if a == 25:
    print("Christmas")
elif a == 24:
    print("Christmas Eve")
elif a == 23:
    print("Christmas Eve Eve")
elif a == 22:
    print("Christmas Eve Eve Eve")

Buggy submission (verdict: Runtime Error):
N, X = input().split()

N = int(N)
X = int(X)

all = 2**(N+2)-3
b_all = 0
for i in range(N+1):
    if i > 0:
        b_all += 2**i

p_all = all - b_all

first = 1
last = all

get = 0
for i in range(N):
    if i < N-1:
        if (first + last) / 2 == X:
            get += (p_all + 1) / 2
            break

        elif (first + last) / 2 < X:
            get += (p_all + 1) / 2
            p_all -= (p_all + 1) / 2
            first = (first + last) / 2 + 1
            last = last - 1

            if first == X:
                break
        elif (first + last) / 2 > X:
            p_all = (p_all - 1) / 2
            last = (first + last) / 2 - 1
            first = first + 1

            if last == X:
                get += p_all
                break
    else:
        if first == X:
            break
        elif last == X:
            get += 3
        else:
            get += X - first
            break

print(int(get))
Problem p02677 (python)

Fixed submission:
import math

# 余弦定理

a, b, h, m = map(int, input().split())

# 12時方向に対して、分針が作る角度
b_degree = 6 * m

# 12時方向に対して、時針が作る角度
# 時針は1時間に30度動くことにも注意
# 10時40分の時、時針は10時の位置から+40/60*30度動いている
a_degree = 30 * h + m / 60 * 30

# 分針が時針を追い越しているか、追い越していないかで場合分け
# 分針が時針を追い越してない
# 分針が0の場合を考慮してminとる
if a_degree > b_degree:
    degree1 = a_degree - b_degree
    degree2 = 360 - degree1
    degree = min(degree1, degree2)
    length = math.sqrt(a**2 + b**2 - 2*a*b*math.cos(math.radians(degree)))
elif a_degree < b_degree:
    degree = b_degree - a_degree
    length = math.sqrt(a**2 + b**2 - 2*a*b*math.cos(math.radians(degree))) 
else:
    length = abs(b - a)

print(length)

Buggy submission (verdict: Runtime Error):
import math

# 余弦定理

a, b, h, m = map(int, input().split())

# 12時方向に対して、分針が作る角度
b_degree = 6 * m

# 12時方向に対して、時針が作る角度
# 時針は1時間に30度動くことにも注意
# 10時40分の時、時針は10時の位置から+40/60*30度動いている
a_degree = 30 * h + m / 60 * 30

print(a_degree, b_degree)

# 分針が時針を追い越しているか、追い越していないかで場合分け
# 分針が時針を追い越してない
# 分針が0の場合を考慮してminとる
if a_degree > b_degree:
    degree1 = a_degree - b_degree
    degree2 = 360 - degree1
    degree = min(degree1, degree2)
    print(degree)
    length = math.sqrt(a**2 + b**2 - 2*a*b*math.cos(math.radians(degree)))
elif a_degree < b_degree:
    degree = b_degree - a_degree
    length = math.sqrt(a**2 + b**2 - 2*a*b*math.cos(math.radians(degree))) 
else:
    lenght = b - a

print(length)
Problem p02802 (python)

Fixed submission:
n, m=map(int,input().split())
ac = 0
ans=[True] * n
wa=[0] * n

for i in range(m):
    question, result=input().split()
    num = int(question) - 1
    if result == "AC" and ans[num]:
        ans[num]=False
        ac+=1
    # WAかつACされていない場合
    elif result == "WA" and ans[num]:
        wa[num] += 1
 
wa_sum = 0

for i in range(n):
    # 問題がACされている場合、その問題番号のWA回数を足す
    if not ans[i]:
        wa_sum += wa[i]
 
print(ac, wa_sum)

Buggy submission (verdict: Runtime Error):
n, m = map(int, input().split())

ac = 0
ans = [True] * n
wa = [0] * n

for i in range(m):
    question_number, result = input().split()
    num = int(question_number) - 1
    if result == 'AC' and ans[num]:
        ans[num] = False
        ac += 1
    elif result == 'WA' and ans[num]:
        wa[num] += 1

wa_sum = 0

for wa_num in wa:
    if not ans[num]:
        wa_sum += wa_num 

print(ac, wa_sum)

Buggy submission (verdict: Runtime Error):
# acするまでのwaをそれぞれの問題でカウントする
cnts = [0] * n

# key：acした問題番号、value：waした回数の辞書を作成する
at_dict = {}

for i in range(m):
    question, result = map(str, input().split())
    if result == 'AC':
        # ACした時点で上記で作成した辞書に、key：問題番号、value:wa回数を記入
        at_dict[question] = cnts[int(question)-1]
    elif result == 'WA':
        # WAなら上記のwa回数のリストにカウント
        cnts[int(question)-1] += 1
    
ac = len(at_dict)
wa = sum(v for v in at_dict.values())

print(ac, wa)
Problem p02882 (python)

Fixed submission:
import math
a, b, x = map(int, input().split())

if x < a*a*b/2:
    tan = 2*x / (a*b*b)
    deg = math.degrees(math.atan(tan))
    print(90-deg)
elif x == a*a*b:
    print(0)
else:
    i = a**3
    j = 2*(a*a*b-x)
    tan = i/j
    deg = math.degrees(math.atan(tan))
    print(90-deg)

Buggy submission (verdict: Runtime Error):
import math
a, b, x = map(int, input().split())

if x < a*a*b/2:
    tan = 2*x / (a*b*b)
else:
    i = a**3
    j = 2*(a*a*b-x)
    tan = i/j

deg = math.degrees(math.atan(tan))
print(90-deg)

Buggy submission (verdict: Runtime Error):
import math
a, b, x = map(int, input().split())
i = a**3
j = 2*(a*a*b-x)
tan1 = i/j
deg = math.degrees(math.atan(tan1))
if x < a*a*b/2:
    tan2 = 2*x / (a*b*b)
    deg = math.degrees(math.atan(tan2))

print(90-deg)

Buggy submission (verdict: Runtime Error):
mport math
a, b, x = map(int, input().split())
i = a**3
j = 2*(a*a*b-x)
tan1 = i/j
deg = math.degrees(math.atan(tan1))
if x < a*a*b/2:
    tan2 = 2*x / (a*b*b)
    deg = math.degrees(math.atan(tan2))

print(90-deg)

Buggy submission (verdict: Runtime Error):
import math
a, b, x = map(int, input().split())
i = a**3
j = 2*(a*a*b-x)
tan1 = i/j
deg = math.degrees(math.atan(tan1))
if deg < 45:
    tan2 = 2*x / (a*b*b)
    deg = math.degrees(math.atan(tan2))

print(90-deg)
Problem p02971 (python)

Fixed submission:
import copy
n = int(input())
a = [int(input()) for _ in range(n)]
 
b = copy.deepcopy(a)
b.sort()
max1 = b[-1]
max2 = b[-2]
 
for i in a:
    if i == max1:
        print(max2)
    else:
        print(max1)

Buggy submission (verdict: Runtime Error):
# リストのうちmaxのインデックス以外は全てmaxを出力、maxのインデックスは2番目に大きい値を出力する
n = int(input())
a = list(int(input()) for _ in range(n))

# リストのmaxとmaxのインデックスを取得
a_max = max(a)
max_index_all = [i for i, x in enumerate(a) if x == a_max]

# リストからmaxを全部削除し、2番目に大きい値を取得
for i in max_index_all:
    a.pop(i)
a_second_max = max(a)


for i in range(n):
    # maxのインデックスでは、2番目に大きな値を出力
    if i in max_index_all:
        print(a_second_max)
    # max以外のインデックスでは、maxを出力
    else:
        print(a_max)
Problem p03061 (python)

Fixed submission:
from math import gcd

n = int(input())
A = list(map(int, input().split()))

if n == 2:
    print(max(A))
else:
    # 最初の3個のうちの2個を選んで、最大の最大公約数
    # 最大公約数に選ばれなかったものを交換（rがaより大きいとき）
    r = max(gcd(A[0], A[1]), gcd(A[1], A[2]), gcd(A[2], A[0]))
    # リストaの最初3個の最大公約数
    a = gcd(A[0], gcd(A[1], A[2]))

    for i in range(3, n):
        r = max(gcd(A[i], r), a)
        a = gcd(A[i], a)

    print(r)

Buggy submission (verdict: Runtime Error):
from math import gcd

n = int(input())
A = list(map(int, input().split()))

if n == 2:
    print(max(a))
else:
    # 最初の3個のうちの2個を選んで、最大の最大公約数
    # 最大公約数に選ばれなかったものを交換（rがaより大きいとき）
    r = max(gcd(A[0], A[1]), gcd(A[1], A[2]), gcd(A[2], A[0]))
    # リストaの最初3個の最大公約数
    a = gcd(A[0], gcd(A[1], A[2]))

    for i in range(3, n):
        r = max(gcd(A[i], r), a)
        a = gcd(A[i], a)

    print(r)

Buggy submission (verdict: Runtime Error):
from math import gcd

n = int(input())
a = list(map(int, input().split()))

if n == 2:
    print(max(a))
else:
    # 最初の3個のうちの2個を選んで、最大の最大公約数
    # 最大公約数に選ばれなかったものを交換（rがaより大きいとき）
    r = max(gcd(a[0], a[1]), gcd(a[1], a[2]), gcd(a[2], a[0]))
    # リストaの最初3個の最大公約数
    a = gcd(a[0], gcd(a[1], a[2]))

    for i in range(3, n):
        r = max(gcd(a[i], r), a)
        a = gcd(a[i], a)

    print(r)
Problem p03254 (python)

Fixed submission:
N, x = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
if x == sum(a):
    print(N)
elif x > sum(a):
    print(N-1)
else:
    for i in range(N):
        if sum(a[:i+1]) > x:
            print(i)
            break

Buggy submission (verdict: Runtime Error):
n, x = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
sum = 0
people = 0

if x == sum(a):
    print(n)
elif x > sum(a):
    print(n-1)
else:
    for i in a:
        sum += i
        people += 1
        if sum > x:
            print(people-1)
            break
Problem p03331 (python)

Fixed submission:
n = int(input())
s = []

for num in range(1, n//2+1):
    a = num
    b = n - num
    a_s = sum(list(map(int, str(a))))
    b_s = sum(list(map(int, str(b))))
    c = a_s + b_s
    s.append(c)

print(min(s))

Buggy submission (verdict: Runtime Error):
n = int(input())
s = []

for num in range(2, n//2):
    a = num
    b = n - num
    a_s = sum(list(map(int, str(a))))
    b_s = sum(list(map(int, str(b))))
    c = a_s + b_s
    s.append(c)

print(min(s))

Buggy submission (verdict: Runtime Error):
n = int(input())
s = []

for num in range(2, n//2+1):
    a = num
    b = n - num
    a_s = sum(list(map(int, str(a))))
    b_s = sum(list(map(int, str(b))))
    c = a_s + b_s
    s.append(c)

print(min(s))

Buggy submission (verdict: Runtime Error):
n = int(input())
s = []

for num in range(2, n-1):
    a = num
    b = n - num
    a_s = sum(list(map(int, str(a))))
    b_s = sum(list(map(int, str(b))))
    c = a_s + b_s
    s.append(c)

print(min(s))
Problem p03471 (python)

Fixed submission:
n, y = map(int, input().split())
result = 0

for i in range(n+1):
    for j in range(n+1):
        k = n - i - j
        z = 10000*i + 5000*j + 1000*k
        if z == y and k >= 0:
            result += 1
            print(i, j, k)
            exit()

if result == 0:
    print('-1 -1 -1')

Buggy submission (verdict: Runtime Error):
n, y = map(int, input().split())
k = 0
result = 0

for i in range(n + 1):
    for j in range(n + 1):
        k = n - i - j
        ans = 10000 * i + 5000 * j + 1000 * k
        if (ans == y):
            print(a, b, c)
            result += 1
            exit()
            
if result == 0:
    print('-1 -1 -1')

Buggy submission (verdict: Runtime Error):
N,Y = map(int,input().split())
for x in range(N+1):
  for y in range(N-x+1):
    if 10000*x+5000*y+1000*(N-x-y) == Y:
      print(x,y,N-x-y)
      exit()
print(-1,-1,-1)
N,Y = map(int,input().split())
for x in range(N+1):
  for y in range(N-x+1):
    if 10000*x+5000*y+1000*(N-x-y) == Y:
      print(x,y,N-x-y)
      exit()
print(-1,-1,-1)

Buggy submission (verdict: Runtime Error):
n, y = map(int, input())
k = 0

for i in range(n+1):
  for j in range(n+1):
    k = n - (i + j)
    z = 10000*i + 5000*j + 1000*k
    if z == y:
      print(i, j, k)
      exit()

print(-1, -1, -1)
Problem p03478 (python)

Fixed submission:
N, A, B = map(int, input().split())
result = 0

for num in range(1, N+1):
    num_10000 = num // 10000
    num_1000 = (num - 10000*num_10000) // 1000
    num_100 = (num - 10000*num_10000 - 1000*num_1000) // 100
    num_10 = (num - 10000*num_10000 - 1000*num_1000 - 100*num_100) // 10
    num_1 = num - 10000*num_10000 - 1000*num_1000 - 100*num_100 - 10*num_10
    sum = num_1 + num_10 + num_100 + num_1000 + num_10000
    if A <= sum <= B:
        result += num
        
print(result)

Buggy submission (verdict: Runtime Error):
N = int(input())
A, B = map(int, input().split())
result = 0

for num in range(1, N+1):
    num_1000 = num // 1000
    num_100 = (num - 1000*num_1000) // 100
    num_10 = (num - 1000*num_1000 - 100*num_100) // 10
    num_1 = num - 1000*num_1000 - 100*num_100 - 10*num_10
    sum = num_1 + num_10 + num_100 + num_1000
    if A <= sum <= B:
        result += num

print(result)

Buggy submission (verdict: Runtime Error):
N = int(input())
A, B = map(int, input().split())
result = 0

for num in range(1, N+1):
    num_1000 = num // 1000
    num_100 = (num - 1000*num_1000) // 100
    num_10 = (num - 1000*num_1000 - 100*num_100) // 10
    num_1 = num - 1000*num_1000 - 100*num_100 - 10*num_10
    sum = num_1 + num_10 + num_100 + num_1000
    if A <= sum <= B:
        result += num

print(result)
Problem p03486 (python)

Fixed submission:
s = list(input())
t = list(input())

s.sort()
t.sort(reverse=True)

if ''.join(s) < ''.join(t):
    print('Yes')
else:
    print('No')

Buggy submission (verdict: Runtime Error):
s = input()
t = input()
s = list(s)
t = list(t)

s.sort(reverse=True)
s_final = s[-1]
s_semifinal = s[-2]
s_len = len(s)
t.sort(reverse=True)
t_first = t[0]
t_second = t[1]
t_len = len(t)

alpha = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
num = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26]
dic = dict(zip(alpha, num))

if dic[s_final] < dic[t_first]:
    print('Yes')
elif dic[s_final] == dic[t_first] and dic[s_semifinal] < dic[t_second]:
    print('Yes')
elif dic[s_final] == dic[t_first] and dic[s_semifinal] == dic[t_second] and s_len < t_len:
    print('Yes')
else:
    print('No')
Problem p03814 (python)

Fixed submission:
s = input()

print(s.rfind('Z') - s.find('A') + 1)

Buggy submission (verdict: Runtime Error):
s = list(input())
a_index = [i for i, x in enumerate(s) if x == 'A']
z_index = [i for i, x in enumerate(s) if x == 'Z']
distance = []

for i in a_index:
    for j in z_index:
        if i <= j:
            dis = j-i
            distance.append(dis)
        else:
            break

print(max(distance)+1)

Buggy submission (verdict: Runtime Error):
s = list(input())
a_index = [i for i, x in enumerate(s) if x == 'A']
z_index = [i for i, x in enumerate(s) if x == 'Z']
distance = []

for i in a_index:
    for j in z_index:
        if i <= j:
            dis = abs(i-j)
            distance.append(dis)
        else:
            break

print(max(distance)+1)

Buggy submission (verdict: Runtime Error):
s = list(input())
a_index = [i for i, x in enumerate(s) if x == 'A']
z_index = [i for i, x in enumerate(s) if x == 'Z']
distance = []

for i in a_index:
    for j in z_index:
        if i < j:
            dis = abs(i-j)
            distance.append(dis)
        else:
            break

print(max(distance)+1)
Problem p03078 (python)

Fixed submission:
# -*- coding: utf-8 -*-
from sys import stdin
import heapq
# import numpy as np
import sys
sys.setrecursionlimit(10**6)


s_in = lambda: stdin.readline()[:-1]  # s = s_in()
d_in = lambda: int(stdin.readline())  # N = d_in()
ds_in = lambda: list(map(int, stdin.readline().split()))  # List = ds_in()

X, Y, Z, K = ds_in()
A_list = ds_in()
B_list = ds_in()
C_list = ds_in()

A_list.sort(reverse=True)
B_list.sort(reverse=True)
C_list.sort(reverse=True)


def search(cand, searched):
    if len(searched) > K-1:
        exit()
    current = searched[-1][1]
    cand1 = (current[0]+1, current[1], current[2])
    cand2 = (current[0], current[1]+1, current[2])
    cand3 = (current[0], current[1], current[2]+1)
    if cand1[0] < len(A_list):
        val = A_list[cand1[0]] + B_list[cand1[1]] + C_list[cand1[2]]
        if (-val, cand1) not in cand:
            heapq.heappush(cand, (-val, cand1))
    if cand2[1] < len(B_list):
        val = A_list[cand2[0]] + B_list[cand2[1]] + C_list[cand2[2]]
        if (-val, cand2) not in cand:
            heapq.heappush(cand, (-val, cand2))
    if cand3[2] < len(C_list):
        val = A_list[cand3[0]] + B_list[cand3[1]] + C_list[cand3[2]]
        if (-val, cand3) not in cand:
            heapq.heappush(cand, (-val, cand3))
    next_ = heapq.heappop(cand)
    print(-next_[0])
    searched.append(next_)
    # print('cand', cand)
    # print('searched', searched)
    search(cand=cand, searched=searched)


searched = [(-(A_list[0] + B_list[0] + C_list[0]), (0, 0, 0))]
cand = []
print(-searched[0][0])
search(cand=cand, searched=searched)

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
from sys import stdin
import heapq
# import numpy as np

s_in = lambda: stdin.readline()[:-1]  # s = s_in()
d_in = lambda: int(stdin.readline())  # N = d_in()
ds_in = lambda: list(map(int, stdin.readline().split()))  # List = ds_in()

X, Y, Z, K = ds_in()
A_list = ds_in()
B_list = ds_in()
C_list = ds_in()

A_list.sort(reverse=True)
B_list.sort(reverse=True)
C_list.sort(reverse=True)


def search(cand, searched):
    if len(searched) > K-1:
        exit()
    current = searched[-1][1]
    cand1 = (current[0]+1, current[1], current[2])
    cand2 = (current[0], current[1]+1, current[2])
    cand3 = (current[0], current[1], current[2]+1)
    if cand1[0] < len(A_list):
        val = A_list[cand1[0]] + B_list[cand1[1]] + C_list[cand1[2]]
        if (-val, cand1) not in cand:
            heapq.heappush(cand, (-val, cand1))
    if cand2[1] < len(B_list):
        val = A_list[cand2[0]] + B_list[cand2[1]] + C_list[cand2[2]]
        if (-val, cand2) not in cand:
            heapq.heappush(cand, (-val, cand2))
    if cand3[2] < len(C_list):
        val = A_list[cand3[0]] + B_list[cand3[1]] + C_list[cand3[2]]
        if (-val, cand3) not in cand:
            heapq.heappush(cand, (-val, cand3))
    next_ = heapq.heappop(cand)
    print(-next_[0])
    searched.append(next_)
    search(cand=cand, searched=searched)


searched = [(-(A_list[0] + B_list[0] + C_list[0]), (0, 0, 0))]
cand = []
print(-searched[0][0])
search(cand=cand, searched=searched)
Problem p03142 (python)

Fixed submission:
# -*- coding: utf-8 -*-
from sys import stdin
# import sys
# sys.setrecursionlimit(10 ** 5+1)
import numpy as np
# import IPython

s_in = lambda: stdin.readline()[:-1]  # s = s_in()
d_in = lambda: int(stdin.readline())  # N = d_in()
ds_in = lambda: list(map(int, stdin.readline().split()))  # List = ds_in()


N, M = ds_in()

child = {i+1: [] for i in range(N)}
parent = {i+1: [] for i in range(N)}
inflow = {i+1: 0 for i in range(N)}
for _ in range(N+M-1):
    a, b = ds_in()
    child[a].append(b)
    parent[b].append(a)
    inflow[b] += 1

candidates = []
for i, j in inflow.items():
    if j == 0:
        candidates.append(i)

sorted_edges = []
while len(sorted_edges) < N:
    par = candidates.pop()
    sorted_edges.append(par)
    for chi in child[par]:
        inflow[chi] -= 1
        if inflow[chi] == 0:
            candidates.append(chi)


sorted_index = np.zeros(N+1, dtype=int)
sorted_index[sorted_edges] = np.arange(N)

for i in range(1, N+1):
    if i == sorted_edges[0]:
        print(0)
    else:
        print(parent[i][np.argmax(sorted_index[parent[i]])])

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
from sys import stdin
# import numpy as np
# import IPython

s_in = lambda: stdin.readline()[:-1]  # s = s_in()
d_in = lambda: int(stdin.readline())  # N = d_in()
ds_in = lambda: list(map(int, stdin.readline().split()))  # List = ds_in()


N, M = ds_in()

to_par = {i: [] for i in range(1, N+1)}
to_chi = {i: [] for i in range(1, N+1)}
con_list = []
for _ in range(N - 1 + M):
    a, b = ds_in()
    to_par[b].append(a)


def find_all_parents(child):
    """
    child : int
    """
    all_parents = []
    for p in to_par[child]:
        all_parents.append(p)
        all_parents.extend(find_all_parents(p))
    all_parents = list(set(all_parents))
    return all_parents


parents_dict = {i: find_all_parents(i) for i in range(1, N+1)}

ans = {i: 0 for i in range(1, N+1)}
for child in range(1, N+1):
    par_cand = to_par[child]
    all_par = []
    for p in par_cand:
        all_par.extend(parents_dict[p])
    all_par = list(set(all_par))
    for p in par_cand:
        if p not in all_par:
            ans[child] = p
            break

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


# IPython.embed()