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

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
 
ans = [0] * N
for i in range(len(A)):
    ans[A[i]-1] = i + 1
 
for a in ans:
    print(a, end=' ')

Buggy submission (verdict: Runtime Error):
N = int(input())
A = list(map(int, input().split()))
 
ans = [0] * N
for i in range(len(A)):
    ans[A[i]-1] = i + 1
 
  for a in ans:
    print(a, end=' ')
Problem p03455 (python)

Fixed submission:
a, b = map(int, input().split())
c = a * b
s = "Even" if c % 2 == 0 else "Odd"
print(s)

Buggy submission (verdict: Runtime Error):
a, b = map(int, imput().split())
c = a * b
s = 'Even' if c % 2 == 0 else 'Odd'
print(s)

Buggy submission (verdict: Runtime Error):
a, b = map(int, imput().split())
s = (a * b) % 2 == 0 ? 'Even' : 'Odd'
print(s)
Problem p03693 (python)

Fixed submission:
a, b, c = map(int, input().split())
s = a * 100 + b * 10 + c
r = "YES" if s % 4 == 0 else "NO"
print(r)

Buggy submission (verdict: Runtime Error):
a, b, c = map(int, input().split)
s = a * 100 + b * 10 + c
r = "YES" if s % 4 == 0 else "NO"
print(r)
Problem p02732 (python)

Fixed submission:
'''
import numpy as np
N = int(input())
A = np.array(list(map(int, input().split(' '))))
num_sum = 0
remove_array = np.array([], int)
for i in range(N):
    c1 = np.count_nonzero(A == i + 1)
    c2 = np.count_nonzero(A == A[i])
    if c2 <= 1:
        remove_array = np.append(remove_array, 0)
    else:
        remove_array = np.append(remove_array, c2 - 1)
    num_sum += int(c1 * (c1 - 1) / 2)
print(*(num_sum - remove_array), sep='\n')
'''
import collections
N = int(input())
A = list(map(int, input().split()))
cnt = collections.Counter(A)
total = 0
for key in cnt.keys():
    total += cnt[key] * (cnt[key] - 1) // 2
for i in range(N):
    tmp = total
    tmp -= cnt[A[i]] * (cnt[A[i]] - 1) // 2
    tmp += (cnt[A[i]] - 1) * (cnt[A[i]] - 2) // 2
    print(tmp)

Buggy submission (verdict: Runtime Error):
N = int(input())
A = list(map(int, input().split(' ')))
num_sum = 0
for i in range(1, N+1):
    c = A.count(i)
    num_sum += int(c * (c - 1) / 2)

for j in A:
    if A.count(j) == 1:
        print(sum(num_sum))
    else:
        print(sum(num_sum) - (A.count(j) - 1))

Buggy submission (verdict: Runtime Error):
N = int(input())
A = list(map(int, input().split(' ')))
num_sum = 0
for i in range(1, N+1):
    c = A.count(i)
    num_sum += int(c * (c - 1) / 2)

for j in A:
    if A.count(j) == 1:
        print(sum(num_list))
    else:
        print(sum(num_list) - (A.count(j) - 1))
Problem p02756 (python)

Fixed submission:
S = input('')
Q = int(input(''))
head, tail, rev_flag = '', '', 1
for i in range(Q):
    char = input()
    if char == '1':
        rev_flag *= -1
    else:
        t, f, c = char.split()
        if (f == '1' and rev_flag == 1) or (f == '2' and rev_flag == -1):
            head = c + head
        elif (f == '2' and rev_flag == 1) or (f == '1' and rev_flag == -1):
            tail = tail + c
if rev_flag == 1:
    print(head, S, tail, sep='')
else:
    print(tail[::-1], S[::-1], head[::-1], sep='')

Buggy submission (verdict: Runtime Error):
S = input('')
Q = int(input(''))
rev_flag = 1
for i in range(Q):
    str = input('')
    if(str.count(' ') == 0 and str == '1'):
        rev_flag = -rev_flags
    elif(str[0] == '2' and ((str[2] == '1' and rev_flag == 1) or (str[2] == '2' and rev_flag == -1))):
        S = str[4] + S
    elif(str[0] == '2' and ((str[2] == '2' and rev_flag == 1) or (str[2] == '1' and rev_flag == -1))):
        S = S + str[4]
if rev_flag == 1:
    print(S)
elif rev_flag == -1:
    print(S[::-1])
Problem p02761 (python)

Fixed submission:
import collections as cl
N, M = map(int, input().split())
dic = cl.defaultdict(lambda:-1)
num = 0
for i in range(0, M):
    s, c = map(int, input().split())
    if N - s in dic and dic[N-s] != c:
        print(-1)
        exit(0)
    else:
        dic[N-s] = c
if dic[N-1] == -1:
    if N == 1:
        dic[N-1] = 0
    else:
        dic[N-1] = 1
for j in dic.keys():
    num += dic[j] * (10 ** j)
if N > 1 and num < 10 ** (N - 1):
    print(-1)
else:
    print(num)

Buggy submission (verdict: Runtime Error):
N, M = map(int, input('').split(' '))
list = []
flag = []
num = 0
for j in range(N):
    if(j == N - 1):
        list.append(1)
    else:
        list.append(0)
    flag.append(0)
if(M == 0):
    M = 1
for i in range(M):
    s, c = map(int, input('').split(' '))
    s = int(s)
    if flag[N - s] == 1 and list[N - s] != c:
        print(-1)
        exit(0)
    else:
        list[N - s] = c
        flag[N - s] = 1
for j in range(N):
    num += list[j] * 10 ** j
if num > 0:
    print(num)
else:
    print(-1)

Buggy submission (verdict: Runtime Error):
N, M = map(int, input('').split(' '))
list = []
flag = []
num = 0
for j in range(N):
    list.append(0)
    flag.append(0)
if(M == 0):
    M = 1
for i in range(M):
    s, c = map(int, input('').split(' '))
    s = int(s)
    if flag[N - s] == 1 and list[N - s] != c:
        print(-1)
        exit(0)
    else:
        list[N - s] = c
        flag[N - s] = 1
for j in range(N):
    num += list[j] * 10 ** j
if num > 0:
    print(num)
else:
    print(-1)
Problem p02791 (python)

Fixed submission:
N = int(input())
P = list(map(int, input().split(' ')))
cnt = 1
j = P[0]
for i in range(1, N):
    j = min(j, P[i])
    if P[i] <= j:
        cnt += 1
print(cnt)

Buggy submission (verdict: Runtime Error):
N = int(input())
P = list(map(int, input().split(' ')))
print(sum(1 for i in range(N) if P[i] <= min(P[0:i])))

Buggy submission (verdict: Runtime Error):
N = int(input())
P = list(map(int, input().split(' ')))
print(sum(1 for i in range(N) if P[i] <= min(P[:i])))
Problem p02939 (python)

Fixed submission:
s = str(input())

j = 0
count = len(s)

s = [_ for _ in s]
for i in range(0, len(s)-1):
    if j+1 <= len(s)-1:
        if s[j] == s[j+1]:
            j = j + 3
            count -= 1
        else:
            j = j + 1
            continue

print(count)

Buggy submission (verdict: Runtime Error):
s = str(input())

j = 0
count = len(s)

s = [_ for _ in s]
for i in range(0, len(s)-1):
    print(j)
    print(s)
    if j+1 <= len(s)-1:
        if s[j] == s[j+1]:
            j = j + 3
            count -= 1
        else:
            j = j + 1
            continue

print(count)

Buggy submission (verdict: Runtime Error):
s = str(input())

s = [_ for _ in s]
for i in range(0, len(s)-1):
    print(s)
    if i+2 <= len(s)-1:
        if s[i] == s[i+1]:
            s[i+1] = ''.join([s[i+1], s.pop()])

if s[-1] == s[-2]:
    print(len(s) - 1)
else:
    print(len(s))

Buggy submission (verdict: Runtime Error):
s = str(input())

s = [_ for _ in s]
for i in range(0, len(s)-1):
    print(s)
    if i+2 <= len(s)-1:
        if s[i] == s[i+1]:
            s[i+1] = s[i+1] + s.pop(i+2)
            i = 0
    
    if s[-1] == s[-2]:
        print(len(s) - 1)
    else:
        print(len(s))
Problem p03155 (python)

Fixed submission:
N = int(input())
H = int(input())
W = int(input())

print((N-H+1)*(N-W+1))

Buggy submission (verdict: Runtime Error):
def searchAokiGetCard(xi, A):
    Adistance = list(map(lambda ai: abs(ai-xi), A))
    Adismin = min(Adistance)
    geta = xi - Adismin
    if geta not in A:
        geta = xi + Adismin
    A.remove(geta)
    return A


N, Q = map(int, input().split())
A = list(map(int, input().split()))
Aorig = A.copy()

X = []
for i in range(Q):
    X.append(int(input()))

A.sort()
for xi in X:
    sum = 0
    A = Aorig.copy()
    for _ in A:
        At = A.pop()
        sum = sum + At
        if A != []:
            A = searchAokiGetCard(xi, A)

    if A != []:
        print(sum + max(A))
    else:
        print(sum)
Problem p03192 (python)

Fixed submission:
s = str(input())
count = 0
for i in s:
    if i == str(2):
        count = count + 1
print(count)

Buggy submission (verdict: Runtime Error):
def usage():
    product = 1
    N, P = map(int, input().split())

    fct = factorize(P)

    for i in range(len(fct)):
        fct[i][1] = fct[i][1]//N

        if fct[i][1] >= 1:
            product = product*(fct[i][0]**fct[i][1])
    print(product)
    
def factorize(n):
    fct = []  # prime factor
    b, e = 2, 0  # base, exponent
    while b * b <= n:
        while n % b == 0:
            n = n // b
            e = e + 1
        if e > 0:
            fct.append([b, e])
        b, e = b + 1, 0
    if n > 1:
        fct.append([n, 1])
    return fct

if __name__ == '__main__':
    usage()

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

count=0

for i in range(N):
    A, B = map(int, input().split())
    if (A >= H)&(B >= W):
        count = count + 1

print(count)
Problem p02694 (python)

Fixed submission:
import math

# X
X = int(input())

A = 100
i = 0

while True:
    if X <= A :
        print(i)
        break
    else:
        i += 1
        A = math.floor(A*1.01)

Buggy submission (verdict: Runtime Error):
# X
X = int(input())

A = 100
i = 0

while true:
    if X <= A :
        print(0)
        break
    else:
        i += 1
        A = math.floor(A*1.1) :
Problem p03001 (python)

Fixed submission:
#W, H, x, y
W, H, x, y = map(int, input().split())

S = W*H/2
if x==W/2 and y == H/2:
    print(S, 1)
else:
    print(S, 0)

Buggy submission (verdict: Runtime Error):
#W, H, x, y
W, H, x, y = map(int, input().split())

S = W*H/2
if x==W/2 and y == H/2:
    print(S, 1)
else:
    print(S, 0)
#W, H, x, y
W, H, x, y = map(int, input().split())

S = W*H/2
if x==W/2 and y == H/2:
    print(S, 1)
else:
    print(S, 0)
Problem p03062 (python)

Fixed submission:
import numpy as np
n = int(input())
a = list(map(int, input().split()))

mynas=0
for i in range(n):
    if a[i]<0:
        mynas += 1

a=np.array(a)
a=np.abs(a)
a=np.sort(a)
goukei=0

if mynas % 2 ==0:
    for j in range(n):
        goukei += a[j]
else:
    for k in range(n):
        goukei+=a[k]
    goukei-=a[0]*2

print(goukei)

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

mynas=0
for i in range(n):
    if a[i]<0:
        mynas += 1

goukei=0
if mynas % 2 ==0:
    for j in range(n):
        goukei+=abs(a[j])
else:
    a=np.array(a)
    np.abs(a)
    a=np.sort(a)
    for k in range(n):
        goukei+=a[i]
    goukei-=a[0]*2

print(goukei)
Problem p03068 (python)

Fixed submission:
n = int(input())
s = input()
k = int(input())

for i in range(n):
    if s[i]!=s[k-1]:
        s = s.replace(s[i], '*')
print(s)

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

for i in range(n):
    if s[i]!=s[k]:
        s = s.replace(s[i], '*')
print(s)

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

for i in range(n):
    if s[i]!=s[k]:
        s = s.replace(s[i], '*')
print(s)

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

for i in range(n):
    if s[i]!=s[k]:
        s = s.replace(s[i], '*')
print(s)
Problem p03069 (python)

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

a = []
#  .##..#.##.
w = s.count(".")
b = s.count("#")
m = min(w,b)

bb=0

for i in range(n):
    if s[i]=='.':
        w-=1
        a.append(bb+w)
    if s[i]=='#':
        bb += 1
        a.append(bb+w)
if len(a)>0:
    I = min(a)
    print(min(m,I))
else:
    print(m)

Buggy submission (verdict: Runtime Error):
n = int(input())
s = input()
a = []
#  .##..#.##.
w = s.count(".")
b = s.count("#")
m = min(w,b)

bb=0

for i in range(n):
    if s[i]=='.':
        w-=1
    if s[i]=='#':
        bb += 1
        a.append(bb+w)
n=min(a)
print(min(m,n))

Buggy submission (verdict: Runtime Error):
n = int(input())
s = input()
#white
i=0
numw=0
while s[i]==".":
    numw+=1
    i+=1
    if i == n:
        break
#  ..##.#..
w = s.count(".")
answ = w-numw

#black
j=-1
numb=0
while s[j]=="#":
    numb+=1
    j-=1
    if j*(-1) == n:
        break
b = s.count('#')
ansb=b-numb

#b&w
bw=0
k=numw
if k!=0:
    while s[k]=="#":
        k+=1
        bw+=1
        if k==n:
            break
wb=0
l=numb*(-1)-1
if l!=-1:
    while s[l]==".":
        l-=1
        wb+=1
        if l==n*(-1):
            break
ans3=bw+wb
print(min(ansb,answ,ans3))

Buggy submission (verdict: Runtime Error):
n = int(input())
s = input()
#white
i=0
numw=0
while s[i]==".":
    numw+=1
    i+=1
    if i == n:
        break
#  ..##.#..
w = s.count(".")
answ = (w-numw).copy()

#black
j=-1
numb=0
while s[j]=="#":
    numb+=1
    j-=1
    if j*(-1) == n:
        break
b = s.count('#')
ansb=(b-numb).copy()

#b&w
bw=0
k=numw.copy()
if k!=0:
    while s[k]=="#":
        k+=1
        bw+=1
        if k==n:
            break
wb=0
l=(numb*(-1)-1).copy()
if l!=-1:
    while s[l]==".":
        l-=1
        wb+=1
        if l==n*(-1):
            break
ans3=bw+wb
print(min(ansb,answ,ans3))

Buggy submission (verdict: Runtime Error):
n = int(input())
s = input()
#white
i=0
numw=0
while s[i]==".":
    numw+=1
    i+=1
    if i == n:
        break
#  ..##.#..
w = s.count(".")
answ = w-numw

#black
j=-1
numb=0
while s[j]=="#":
    numb+=1
    j-=1
    if j*(-1) == n:
        break
b = s.count('#')
ansb=b-numb

#b&w
bw=0
k=numw-1
if k!=0:
    while s[k]=="#":
        k+=1
        bw+=1
        if k==n:
            break
wb=0
l=numb*(-1)-1
if l!=-1:
    while s[l]==".":
        l-=1
        wb+=1
        if l==n*(-1):
            break
ans3=bw+wb

print(min(ansb,answ,ans3))

Buggy submission (verdict: Runtime Error):
n = int(input())
s = input()
#white
i=0
num=0
while s[i]==".":
    num+=1
    i+=1
    if i == n:
        break
#  ..##.#..
n = s.count(".")
answ = n-num

#black
j=-1
numb=0
while s[j]=="#":
    numb+=1
    j-=1
    if j*(-1) == n:
        break
m = s.count('#')
ansb=m-numb

if ansb<=answ:
    print(ansb)
else:
    print(answ)

Buggy submission (verdict: Runtime Error):
n = int(input())
s = input()
#white
i=0
num=0
while s[i]==".":
    num+=1
    i+=1
    if i == n:
        break
#  ..##.#..
n = s.count(".")
answ = n-num

#black
j=-1
numb
while s[j]=="#":
    numb+=1
    j-=1
    if j*(-1) == n:
        break
m = s.count('#')
ansb=m-numb

if ansb<=answ:
    print(ansb)
else:
    print(answ)
Problem p03087 (python)

Fixed submission:
n, q = map(int,input().split())
s = input()
a =[0]*n
#print(s[0:])
for i in range(1,n):
    a[i]=a[i-1]
    if s[i]=="C" and s[i-1] =="A":
        a[i]+=1
for j in range(q):
    ans = 0
    l, r = map(int,input().split())
    ans = a[r-1]-a[l-1]
    print(ans)

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

#print(s[0:])
for i in range(n):
    if s[i]=="A" and s[i+1] =="C":
        s = s[:i]+"a"+s[i+1:]
for j in range(q):
    ans = 0
    l, r = map(int,input().split())
    ans = s[l-1:r-1].count("a")
    print(ans)
Problem p03101 (python)

Fixed submission:
H, W= map(int, input().split())
h, w= map(int, input().split())

A = h * W
B = H * w

a = H * W - (A + B - h * w)
print(a)

Buggy submission (verdict: Runtime Error):
import numpy as np
N, M, C= map(int, input().split())
B = []
B.append(list(map(int, input().split())))
arrB = np.array(B)
#A = np.array()
#for i in range(N):
#    A[i] = input()


#a = list()
#for i in range(M):
#    a.append(map(int, input().split()))

#a = list(a)

A = []
for i in range(N):
    A.append(list(map(int, input().split())))

arrA = np.array(A)

D = np.empty((0,M), int)
for i in range(N):
    D = np.append(D, arrB*arrA[i], axis=0)
#    C.append(arrB*arrA[i])


F = np.empty((0,N), int)
for i in range(N):
    F = np.append(F, np.sum(D[i]))

E = F + C
t=0
for i in range(N):
    if E[i]>0:
        t = t+1
    else:
        t =t



#print(arrB)
#print(arrA)
#print(D)
#print(E)
#print(F)
print(t)
Problem p03105 (python)

Fixed submission:
a,b,c = map(int, input().split())
A = int(b/a)
if A<c:
  ans = A
else:
  ans = c
print(ans)

Buggy submission (verdict: Runtime Error):
a,b,c = map(int, input().sprit())
A = b/a
if A<c:
  ans = A
else:
  ans = c
print(ans)

Buggy submission (verdict: Runtime Error):
a,b,c = map(int, input())
A = b/a
if A<c:
  ans = A
else:
  ans = c
print(ans)
Problem p03106 (python)

Fixed submission:
a, b, k = map(int, input().split())
c = 0
for i in range(min(a, b), 0, -1):
    if a % i == b % i == 0:
        c += 1
    if c == k:
        print(i)
        break

Buggy submission (verdict: Runtime Error):
if a % i == b % i == 0:
        c += 1
    if c == k:
        print(i)
        break
Problem p03478 (python)

Fixed submission:
n, a, b = map(int, input().split())
#4569=n
t = 0
for i in range(1, n+1):
    ans = 0
    s = i
    while s!=0:
        amari = s%10
        s = int(s/10)
        ans += amari
    if a <= ans <= b:
        t += i
print(t)

Buggy submission (verdict: Runtime Error):
n, a, b = map(int,input().split())
ans = 0
for i in range(1,n+1):
    s = sum(map(int,list(str(i))))
    if a <= s and s <= b:
        ans += i
print(ans)
n, a, b = map(int,input().split())
ans = 0
for i in range(1,n+1):
    s = sum(map(int,list(str(i))))
    if a <= s and s <= b:
        ans += i
print(ans)
Problem p03777 (python)

Fixed submission:
a,b = input().split()
 
if a==b:
  print('H')
else:
  print('D')

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

if a==b:
  print('H')
else:
  print('D')

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

if (a==b):
  print('H')
else:
  print('D')
Problem p02850 (python)

Fixed submission:
from collections import defaultdict
import sys
sys.setrecursionlimit(10**7)

N = int(input())
ab = [tuple(map(int, input().split())) for _ in range(N - 1)]

G = defaultdict(list)

v = [-1 for _ in range(N - 1)]

for i, (a, b) in enumerate(ab):
    G[a].append((b, i))
    G[b].append((a, i))

def check(p, trg, pc):
    # print(p, trg, pc)
    c = 0
    for next_, vi in G[trg]:
        if next_ != p:
            if c == pc:
                c += 1
            check(trg, next_, c)
            v[vi] = c
            c += 1

check(0, 1, -1)
print(len(set(v)))
for c in v:
    print(c + 1)

Buggy submission (verdict: Runtime Error):
from collections import defaultdict

N = int(input())
ab = [tuple(map(int, input().split())) for _ in range(N - 1)]

G = defaultdict(list)

v = [-1 for _ in range(N - 1)]

for i, (a, b) in enumerate(ab):
    G[a].append((b, i))
    G[b].append((a, i))

def check(p, trg, pc):
    # print(p, trg, pc)
    c = 0
    for next_, vi in G[trg]:
        if next_ != p:
            if c == pc:
                c += 1
            check(trg, next_, c)
            if vi in v:
                v[vi] = c
            c += 1

check(0, 1, -1)
print(len(set(v)))
for c in v:
    print(c + 1)

Buggy submission (verdict: Runtime Error):
from collections import defaultdict

N = int(input())
ab = [tuple(map(int, input().split())) for _ in range(N - 1)]

G = defaultdict(list)

v = [-1 for _ in range(N - 1)]

for i, (a, b) in enumerate(ab):
    G[a].append((b, i))
    G[b].append((a, i))

def check(p, trg, pc):
    # print(p, trg, pc)
    c = 0
    for next_, vi in G[trg]:
        if next_ != p:
            if c == pc:
                c += 1
            check(trg, next_, c)
            v[vi] = c
            c += 1

check(0, 1, -1)
print(len(set(v)))
for c in v:
    print(c + 1)

Buggy submission (verdict: Runtime Error):
from collections import defaultdict

N = int(input())
ab = [tuple(map(int, input().split())) for _ in range(N - 1)]

G = defaultdict(list)

v = [-1 for _ in range(N - 1)]

for i, (a, b) in enumerate(ab):
    G[a].append((b, i))
    G[b].append((a, i))

def check(p, trg, pc):
    # print(p, trg, pc)
    c = 0
    for next_, vi in G[trg]:
        if next_ != p:
            if c == pc:
                c += 1
            check(trg, next_, c)
            v[vi] = c
            c += 1

# print(G)

check(0, 1, 0)
print(len(set(v)))
for c in v:
    print(c + 1)
Problem p02913 (python)

Fixed submission:
N = int(input())
S = input()

dp = [[0 for _ in range(N)] for _ in range(N)]

ans = 0
for i in range(N):
    for j in range(i + 1, N):
        if S[i] == S[j]:
            tmp = 1
            if i > 0:
                tmp += dp[i - 1][j - 1]
            if j - i < tmp:
                continue
            dp[i][j] = tmp
        ans = max(ans, dp[i][j])
print(ans)

Buggy submission (verdict: Runtime Error):
from random import shuffle
N = int(input())
S = input()

def check(l):
    r = range(N - 2 * l + 1)
    shuffle(r)
    for i in r:
        if S[i : i + l] in S[i + l :]:
            return True

ok = 0
ng = N // 2 + 1

while ng - ok > 1:
    m = (ok + ng) // 2
    if check(m):
        ok = m
    else:
        ng = m

print(ok)

Buggy submission (verdict: Runtime Error):
from itertools import count

N = int(input())
S = input()

abc = 'abcdefghijklmnopqrstuvwxyz'

T = ['']

flag = True
for i in count():
    T2 = T.copy()
    T.clear()
    for t in T2:
        for c in abc:
            if S.count(t + c) > 1:
                T.append(t + c)
                flag = False
    if flag:
        print(i)
        break
    flag = True
Problem p02985 (python)

Fixed submission:
import sys
from collections import defaultdict

sys.setrecursionlimit(10**5)

N, K = map(int, input().split())
a_bs = [tuple(map(int, input().split())) for _ in range(N - 1)]

def culc_tree(t, used_num):
    tree[t][1] = False
    global answer
    k = K - used_num
    answer = (answer * k) % (10 ** 9 + 7)
    childs = [next_ for next_ in tree[t][0] if tree[next_][1]]
    for i, child in enumerate(childs, 2):
        culc_tree(child, i)


tree = defaultdict(lambda: [[], True])
for a, b in a_bs:
    tree[a][0].append(b)
    tree[b][0].append(a)

answer = K
tree[1][1] = False
for i, child in enumerate(tree[1][0], 1):
    culc_tree(child, i)
print(answer)

Buggy submission (verdict: Runtime Error):
from collections import defaultdict
N, K = map(int, input().split())
a_bs = [tuple(map(int, input().split())) for _ in range(N - 1)]

def culc_tree(t, used_num):
    tree[t][1] = False
    global answer
    k = K - used_num
    print(t, k)
    answer = (answer * k) % (10 ** 9 + 7)
    childs = [next_ for next_ in tree[t][0] if tree[next_][1]]
    for i, child in enumerate(childs, 2):
        culc_tree(child, i)


tree = defaultdict(lambda: [[], True])
for a, b in a_bs:
    tree[a][0].append(b)
    tree[b][0].append(a)

answer = K
tree[1][1] = False
for i, child in enumerate(tree[1][0], 1):
    culc_tree(child, i)
print(answer)

Buggy submission (verdict: Runtime Error):
from collections import defaultdict
N, K = map(int, input().split())
a_bs = [tuple(map(int, input().split())) for _ in range(N - 1)]

def search_tree(t, queue):
    if tree[t][1]:
        return -1
    tree[t][1] = True
    global sum_
    k = K - sum(queue)
    sum_ = (sum_ * k) % (10**9 + 7)
    queue[0] = queue[1]
    queue[1] = 1
    for next_ in tree[t][0]:
        queue[0] += search_tree(next_, queue[:])
        queue[0] += 1
    return 0


tree = defaultdict(lambda: [[], False])
for a, b in a_bs:
    tree[a][0].append(b)
    tree[b][0].append(a)

sum_ = 1
search_tree(1, [0, 0])
print(sum_)
Problem p02997 (python)

Fixed submission:
import itertools

def main():
    N, K = map(int, input().split())
    max_ = int((N - 1) * (N - 2) / 2)
    if K > max_:
        print(-1)
        return
    M = N - 1 + max_ - K
    print(M)
    for i in range(2, N + 1):
        print(1, i)
    for u, v in [(x[0], x[1]) for x in itertools.combinations(range(2, N + 1), 2)][:max_ - K]:
        print(u, v)

main()

Buggy submission (verdict: Runtime Error):
from operator import mul
from functools import reduce

def cmb(n, r):
    r = min(n - r, r)
    if r == 0:
        return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1, r + 1))
    return over // under

def main():
    N, K = map(int, input().split())
    max_ = cmb(N - 1, 2)
    if N <= 2 or K > max_:
        print(-1)
        return
    M = N - 1 + max_ - K
    print(M)
    for i in range(2, N + 1):
        print(1, i)
    for i in range(2, 2 + max_ - K):
        print(i, i + 1)


main()