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

Fixed submission:
digit_1 = int(input()) % 10

if digit_1 in [2 , 4 , 5 ,7 ,9]:
    print('hon')

elif digit_1 in [0 , 1 , 6 , 8]:
    print('pon')

elif digit_1 == 3:
    print("bon")

Buggy submission (verdict: Runtime Error):
hon = input()

digit_1 = hon % 10

if digit_1 == 2 or 4 or 5 or 7 or 9:
    print('hon')

elif digit_1 == 0 or 1 or 6 or 8:
    print('pon')

elif digit_1 == 3:
    print("bon")
Problem p02885 (python)

Fixed submission:
a , b = map(int,input().split())
if a <= b *2:
	print(0)
else:
	print(a - b *2)

Buggy submission (verdict: Runtime Error):
n = int(input())
d = list(map(int,input().split()))
comb_sum = 0
for i in range(n-1):
  for j in range(i+1,n):
    comb_sum += d[i] * d[j]
print(comb_sum)
Problem p02921 (python)

Fixed submission:
import sys
a = []
for l in sys.stdin:
    a.append(l)
count = 0
for i in range(3):
  if a[0][i] == a[1][i]:
    count += 1
print(count)

Buggy submission (verdict: Runtime Error):
perint("TEST")
Problem p03433 (python)

Fixed submission:
print('Yes' if int(input())%500<=int(input()) else 'No')

Buggy submission (verdict: Runtime Error):
x = int(input())
y = [int(i) for i in input().split()]
y.sort(reverse=True)
a = 0
b = 0
for i in range(1, x+1):
    if(i%2) != 0:
        a += y[i-1]
    else:
        b += y[i-1]
print(a-b)
Problem p03854 (python)

Fixed submission:
dreamer = "dreamer"
eraser = "eraser"
dream = "dream"
erase = "erase"


def main():
    S = input()

    while len(S):
        lens = len(S)
        if S[-len(dreamer) :] == dreamer:
            S = S[: -len(dreamer)]
        if S[-len(eraser) :] == eraser:
            S = S[: -len(eraser)]
        if S[-len(dream) :] == dream:
            S = S[: -len(dream)]
        if S[-len(erase) :] == erase:
            S = S[: -len(erase)]
        if lens == len(S):
            print("NO")
            return
    print("YES")
    return


main()

Buggy submission (verdict: Runtime Error):
erasedream
Problem p02957 (python)

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

b = a[0] + a[1]

if b % 2 == 0:
  print(b//2)
else:
  print('IMPOSSIBLE')

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

re = abs(a[0] - a[1])

for i in range(a[0] - a[1]):
  if abs(a[0] - i) == abs(a[1] - i):
    print(i)
    yes = 1
    break

if yes != 1:
  print('IMPOSSIBLE')

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

re = abs(a[0] - a[1])

for i in range(a[0] - a[1]):
  if abs(a[0] - i) == abs(a[1] - i):
    print(i)
    yes = 1
    break

if yes != 1:
  print('IMPOSSIBLE)
Problem p02994 (python)

Fixed submission:
import numpy as np
x, y = map(int, input().split())
A = []

for i in range(x):
    A.append(y)
    y += 1

B = np.abs(A)

we = min(B)
if we not in A:
    we = - + we
    A.remove(we)
else:
    A.remove(we)

cnt = 0
for i in A:
    cnt += i

print(cnt)

Buggy submission (verdict: Runtime Error):
import numpy as np
x, y = map(int, input().split())
A = []

for i in range(x):
    A.append(y)
    y += 1

B = np.abs(A)

we = min(B)

A.remove(we)
cnt = 0
for i in A:
    cnt += i

print(cnt)
Problem p04030 (python)

Fixed submission:
r = []
for i in input():
    if 'B' == i:
        if r:
            r.pop()
    else:
        r.append(i)
print(''.join(r))

Buggy submission (verdict: Runtime Error):
r = []
for i in input():
    if 'B' == i:
        if r:
            r.pop()
    else:
        r.append(i)
print(''.join(r)
Problem p02681 (python)

Fixed submission:
S=input()
T=input()
flag=0
 
S_length=len(S)
for num in range(S_length):
  if S[num]!=T[num]:
    flag=-1
 
if flag==0:
  print('Yes')
else:
  print('No')

Buggy submission (verdict: Runtime Error):
S,T=input().split()
flag=0

S_length=len(S)
for num in range(S_length):
  if S[num]!=T[num]:
    flag=-1

if flag==0:
  print('Yes')
  else:
    print('No')

Buggy submission (verdict: Runtime Error):
S=input()
T=input()

if S in T:
  print('Yes')
  else:
    print('No')

Buggy submission (verdict: Runtime Error):
S=input()
T=input()

if S in T
	print('Yes')
  else
  	print('No')
Problem p03280 (python)

Fixed submission:
x,y=map(int,input().split())
print((x-1)*(y-1))

Buggy submission (verdict: Runtime Error):
x = int(input())
y = int(input())
print((x-1)*(y-1))
Problem p03796 (python)

Fixed submission:
import math
N = int(input())
 
print(math.factorial(N)%(10**9+7))

Buggy submission (verdict: Runtime Error):
import math
N = int(input())
 
print(factrial(N)%(10**9+7))

Buggy submission (verdict: Runtime Error):
import math
N = int(inport())
 
print(math.factorial(N)%(10**9+7))

Buggy submission (verdict: Runtime Error):
import math
N = int(inport())
 
print(math.factrial(N)%(10**9+7))

Buggy submission (verdict: Runtime Error):
import math
N = int(inport())

print(factrial(N)%(10**9+7))
Problem p02553 (python)

Fixed submission:
a = list(map(int,input().split()))
a_1,a_2 = [],[]
a_1.append(a[0])
a_1.append(a[1])
a_2.append(a[2])
a_2.append(a[3])

#print(a_1,a_2)
max_a = -(10**30)
for i in a_1:
    for l in a_2:
        if(i*l > max_a):
            max_a = i*l

print(max_a)

Buggy submission (verdict: Runtime Error):
a = list(map(int,input().split()))
max_a = -10**30
import itertools
for i in itertools.combinations(a,2) :
    #print(i[0]*i[1])
    if( i[0]*i[1] > max_a):
        max_a = i[0]*i[i]

print(max_a)
Problem p02555 (python)

Fixed submission:

"""a = int(input())

import itertools
a_list = [i for i in range(3,a+1)]
#print(a_list)

for i in itertools.combinations_with_replacement(a_list,len(a_list)):
    if(sum(i) == a):
        print(sum(i))
        """

MOD = 10**9 + 7
S = int(input())

dp = [1] * 2001
dp[0] = dp[1] = dp[2] = 0
"""
for i in range(S+1):
    for j in range(3, i):
        dp[i] += dp[i-j]
    print(i,dp[0:S+1])

"""

for i in range(3,S+1):
    for l in range(i+1-3):
        dp[i] += dp[l]

    #print(i,dp[0:i+1])

      

print(dp[S] % MOD)

Buggy submission (verdict: Runtime Error):

"""a = int(input())

import itertools
a_list = [i for i in range(3,a+1)]
#print(a_list)

for i in itertools.combinations_with_replacement(a_list,len(a_list)):
    if(sum(i) == a):
        print(sum(i))
        """

MOD = 10**9 + 7
S = int(input())

dp = [1] * 2001
dp[0] = dp[1] = dp[2] = 0
"""
for i in range(S+1):
    for j in range(3, i):
        dp[i] += dp[i-j]
    print(i,dp[0:S+1])

"""

for i in range(3,S+1):
    for l in range(i+1-3):
        dp[i] += dp[l]

    print(i,dp[0:i+1])

      

print(dp[S] % MOD)
Problem p02616 (python)

Fixed submission:
n,k = map(int,input().split())
a = list(map(int,input().split()))

s = sorted(a,reverse=True)
mod = 10**9 + 7
ans = 1
#全部の要素が負の数であり奇数個の場合は大きい順に取ったものが答え
if k % 2 == 1 and s[0] < 0:
        for i in range(k):
            ans = ans * s[i] % mod
        print(ans)
        exit()#終了

l = 0
r = n - 1
#次の処理のための計算　（kが奇数だった場合)
#この処理の必要性
#奇数個だった場合三個のうち二つで正の数の最大値を作れば良い
if k % 2 == 1:
  ans = ans * s[l]
  l += 1


for _ in range(k // 2):
  ml = s[l] * s[l + 1]
  mr = s[r] * s[r - 1]
  if ml > mr:
    ans = ans * ml % mod
    l += 2
  else:
    ans = ans * mr % mod
    r -= 2

print(ans)

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

s = sorted(a,reverse=True)
mod = 10**9 + 7
ans = 1
#全部の要素が負の数であり奇数個の場合は大きい順に取ったものが答え
if k % 2 == 1 and s[0] < 0:
        for i in range(k):
            ans = ans * s[i] % mod
        print(ans)
        exit()　#終了

l = 0
r = n - 1
#次の処理のための計算　（kが奇数だった場合)
#この処理の必要性
#奇数個だった場合三個のうち二つで正の数の最大値を作れば良い
if k % 2 == 1:
  ans = ans * s[l]
  l += 1


for _ in range(k // 2):
  ml = s[l] * s[l + 1]
  mr = s[r] * s[r - 1]
  if ml > mr:
    ans = ans * ml % mod
    l += 2
  else:
    ans = ans * mr % mod
    r -= 2

print(ans)
Problem p02687 (python)

Fixed submission:
input_l = input()

if(input_l =="ARC"):
  print("ABC")
  
else:
  print("ARC")

Buggy submission (verdict: Runtime Error):
a,b = map(int,input().split())
lis = [0]*b
for i in range(a):
  c = int(input())
  d = input().split()
  
  for l in range(c):
    lis[int(d[l])-1] += 1
    
count = 0
for i in range(b):
  if(lis[i] == 0):
    count += 1

print(count)
Problem p02641 (python)

Fixed submission:
X, N = map(int, input().split())
P = list(map(int, input().split()))

min_val = -1
for i in range(-1000, 1000):
    if i in P:
        continue
    if abs(min_val - X) > abs(X - i):
        min_val = i
if len(P) == 0:
    print(X)
else:
    print(min_val)

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

A.sort()
L = []
pre_val = A[0]
count = 0
for a in A:
    if pre_val == a:
        count += 1
    else:
        temp = [pre_val, count]
        L.append(temp)
        count = 1
        pre_val = a
L.append([pre_val, count])

res = 0
for i in range(len(L)):
    flag = False
    if L[i][1] != 1:
        continue
    Ai = L[i][0]
    for j in range(i):
        Aj = L[j][0]
        if Ai % Aj == 0:
            flag = True
            break
    if flag is False:
        res += 1
print(res)

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

A.sort()
L = []
pre_val = A[0]
count = 0
for a in A:
    if pre_val == a:
        count += 1
    else:
        temp = [pre_val, count]
        L.append(temp)
        count = 1
        pre_val = a
L.append([pre_val, count])

res = 0
for i in range(len(L)):
    flag = False
    if A[i][1] != 1:
        continue
    Ai = L[i][0]
    for j in range(i):
        Aj = L[j][0]
        if Ai % Aj == 0:
            flag = True
            break
    if flag is False:
        res += 1
print(res)
N = int(input())
A = list(map(int, input().split()))

A.sort()
L = []
pre_val = A[0]
count = 0
for a in A:
    if pre_val == a:
        count += 1
    else:
        temp = [pre_val, count]
        L.append(temp)
        count = 1
        pre_val = a
L.append([pre_val, count])

res = 0
for i in range(len(L)):
    flag = False
    if A[i][1] != 1:
        continue
    Ai = L[i][0]
    for j in range(i):
        Aj = L[j][0]
        if Ai % Aj == 0:
            flag = True
            break
    if flag is False:
        res += 1
print(res)
Problem p02658 (python)

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
res = 1
A.sort()
for i in range(N):
    res *= A[i]
    if res > (10 ** 18):
        print(-1)
        exit()
    elif res == 0:
        print(0)
        exit()
print(res)

Buggy submission (verdict: Runtime Error):
from decimal import Decimal
N = int(input())
A = list(map(str, input().split()))
res = 1
for i in range(N):
    res = Decimal(str(res)) * Decimal(A[i])
if res > (10 ** 18):
    print(-1)
    exit()
print(res)

Buggy submission (verdict: Runtime Error):
from decimal import Decimal
N = int(input())
A = list(map(str, input().split()))
res = 1
for i in range(N):
    res = Decimal(str(res)) * Decimal(A[i])
if res > (10 ** 18):
    print(-1)
    exit()
print(res)

Buggy submission (verdict: Runtime Error):
from decimal import Decimal
N = int(input())
A = list(map(str, input().split()))
res = 1
for i in range(N):
    res = Decimal(str(res)) * Decimal(A[i])
if res > (10 ** 18):
    print(-1)
    exit()
print(res)

Buggy submission (verdict: Runtime Error):
import numpy as np
n = int(input())
a = list(map(int, input().split()))
res = np.prod(a)
if res > (10**18):
    print(-1)
else:
    print(res)
Problem p02582 (python)

Fixed submission:
whether=str(input())
if whether[0]==whether[1]==whether[2]=="R":
    print ("3")
elif whether[0]==whether[1]==whether[2]=="S":
    print ("0")
elif whether[0]==whether[1]=="R" or whether[1]==whether[2]=="R":
    print ("2")
else:
    print ("1")

Buggy submission (verdict: Runtime Error):
whether=input(str())
if whether[1]==whether[2]==whether[3]=="R":
    print ("3")
elif whether[1]==whether[2]==whether[3]=="S":
    print ("0")
elif whether[1]==whether[2]=="R" or whether[2]==whether[3]=="R":
    print ("2")
else:
    print ("1")

Buggy submission (verdict: Runtime Error):
whether=input(str())
if whether[1]==whether[2]==whether[3]=="R":
    print ("3")
elif whether[1]==whether[2]==whether[3]=="S":
    print ("0")
elif whether[1]==whether[2]=="R" or whether[2]==whether[3]=="R":
    print ("2")
else:
    print ("1")
Problem p02609 (python)

Fixed submission:
N = int(input())
X = input()
cnt = X.count('1')
Y, Z = 0, 0
for i in range(N):
    if cnt > 1:
        Y = (Y + int(X[i])*pow(2, N-i-1, cnt-1))%(cnt-1)
    Z = (Z + int(X[i])*pow(2, N-i-1, cnt+1))%(cnt+1)

def popcount(n):
    c = bin(n).count('1')
    return c

for i in range(N):
    if X[i] == '1':
        pc = cnt - 1
        if pc <= 0:
            print(0)
            continue
        else:
            Y_i = (Y - pow(2, N-i-1, cnt-1))%(cnt-1)
            ans = 1
            while Y_i > 0:
                Y_i = Y_i % popcount(Y_i)
                ans += 1
            print(ans)
    else:
        Z_i = (Z + pow(2, N-i-1, cnt+1))%(cnt+1)
        ans = 1
        while Z_i > 0:
            Z_i = Z_i % popcount(Z_i)
            ans += 1
        print(ans)

Buggy submission (verdict: Runtime Error):
N = int(input())
X = input()
cnt = X.count('1')
Y, Z = 0, 0
for i in range(N):
    Y = (Y + int(X[i])*pow(2, N-i-1, cnt-1))%(cnt-1)
    Z = (Z + int(X[i])*pow(2, N-i-1, cnt+1))%(cnt+1)

def popcount(n):
    c = bin(n).count('1')
    return c

for i in range(N):
    if X[i] == '1':
        pc = cnt - 1
        if pc == 0:
            print(0)
            continue
        else:
            Y_i = (Y - pow(2, N-i-1, cnt-1))%(cnt-1)
            ans = 1
            while Y_i > 0:
                Y_i = Y_i % popcount(Y_i)
                ans += 1
            print(ans)
    else:
        Z_i = (Z + pow(2, N-i-1, cnt+1))%(cnt+1)
        ans = 1
        while Z_i > 0:
            Z_i = Z_i % popcount(Z_i)
            ans += 1
        print(ans)
Problem p02642 (python)

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

A.sort()
A_max = A[-1]

checked = [False] * (A_max+1)
ans = 0
for i in range(N):
    if i < N-1:
        if A[i] == A[i+1]:
            for j in range(A[i], A_max+1, A[i]):
                checked[j] = True
    if checked[A[i]]:
        pass
    else:
        ans += 1
        for j in range(A[i], A_max+1, A[i]):
            checked[j] = True

print(ans)

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

A = sorted(A, reverse=True)

ans = 0
for i in range(N-1):
    B = A[(i+1):]
    if all(A[i] % B[j] != 0 for j in range(len(B))):
        ans += 1

if A[-1] != A[-2]:
    ans += 1

print(ans)
Problem p02678 (python)

Fixed submission:
from collections import deque
import sys
sys.setrecursionlimit(10 ** 9)

N, M = map(int, input().split())
path = [list(map(int, input().split())) for i in range(M)]

edges = [[] for j in range(N+1)]
for p in path:
    x, y = p
    edges[x].append(y)
    edges[y].append(x)

que = deque([])
dic = {1: 0}
def bfs(now):
    e = edges[now]
    for q in e:
        if not q in dic:
            dic[q] = now
            que.append(q)
    try:
        n = que.popleft()
        bfs(n)
    except IndexError:
        pass

bfs(1)

if len(dic) != N:
    print("No")
else:
    print('Yes')
    dic_sort = sorted(dic.items())
    for elem in dic_sort:
        if elem[0] != 1:
            print(elem[1])

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

N, M = map(int, input().split())
path = [list(map(int, input().split())) for i in range(M)]

edges = [[] for j in range(N+1)]
for p in path:
    x, y = p
    edges[x].append(y)
    edges[y].append(x)

que = deque([])
dic = {1: 0}
def bfs(now):
    e = edges[now]
    for q in e:
        if not q in dic:
            dic[q] = now
            que.append(q)
    try:
        n = que.popleft()
        bfs(n)
    except IndexError:
        pass

bfs(1)

if len(dic) != N:
    print("No")
else:
    print('Yes')
    dic_sort = sorted(dic.items())
    for elem in dic_sort:
        if elem[0] != 1:
            print(elem[1])
Problem p02691 (python)

Fixed submission:
import collections

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

P = [i+A[i] for i in range(N)]
Q = [j-A[j] for j in range(N)]

Q_dict = collections.Counter(Q)

cnt = 0
for i in range(N-1):
    Q_dict[Q[i]] -= 1
    cnt += Q_dict[P[i]]

print(cnt)

Buggy submission (verdict: Runtime Error):
import collections
import numpy as np

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

n1, n2 = np.meshgrid(np.arange(1,N+1),np.arange(1,N+1))
t1, t2 = np.meshgrid(np.array(A), np.array(A))
ans = (np.abs(n2-n1)==t1 + t2)

print(np.count_nonzero(ans)//2)
Problem p02717 (python)

Fixed submission:
x, y, z = map(int, input().split())

print(z, x, y)

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

print(z, x, y)
Problem p02730 (python)

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

if s == s[::-1]:
  #前半部分の回文チェック
  p = int((n-1)/2)
  f = s[:p]
  if f == f[::-1]:
    #後半部分の回文チェック
    q = int((n+3)/2)
    l = s[q-1:]
    if l == l[::-1]:
      print("Yes")
    else:
      print("No")
  else:
    print("No")
else:
  print("No")

Buggy submission (verdict: Runtime Error):
if s == s[::-1]:
  #前半部分の回文チェック
  p = int((n-1)/2)
  f = s[:p]
  if f == f[::-1]:
    #後半部分の回文チェック
    q = int((n+3)/2)
    l = s[q-1:]
    if l == l[::-1]:
      print("Yes")
    else:
      print("No")
  else:
    print("No")
else:
  print("No")
Problem p02900 (python)

Fixed submission:
from fractions import gcd
import math
A, B = map(int, input().split())

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    divisors.sort()
    return divisors

gcd = gcd(A, B)
divisors = make_divisors(gcd)

def is_prime(n):
    if n == 1: return True

    for k in range(2, int(math.sqrt(n)) + 1):
        if n % k == 0:
            return False

    return True

count = 0
for i in range(len(divisors)):
    if is_prime(divisors[i]):
        count += 1

print(count)

Buggy submission (verdict: Runtime Error):
import math
A, B = map(int, input().split())

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    divisors.sort()
    return divisors

gcd = math.gcd(A, B)
divisors = make_divisors(gcd)

def is_prime(n):
    if n == 1: return True

    for k in range(2, int(math.sqrt(n)) + 1):
        if n % k == 0:
            return False

    return True

count = 0
for i in range(len(divisors)):
    if is_prime(divisors[i]):
        count += 1

print(count)
Problem p02922 (python)

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

if B == 1:
    print(0)
else:
    C = A
    cnt = 1
    while C < B:
        C += A - 1
        cnt += 1
    print(cnt)

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

if B == 1:
    print(0)
else:
    C = A
    cnt = 1
    while C < B:
        C += A - 1
        cnt += 1

print(cnt)
Problem p02923 (python)

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

res = []
cnt = 0
for i in range(N-1):
    if H[i+1] <= H[i]:
        cnt += 1
        if i == N - 2:
            res.append(cnt)
    else:
        res.append(cnt)
        cnt = 0
if res == []:
    print(0)
else:
    print(max(res))

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

res = []
cnt = 0
for i in range(N-1):
    if H[i+1] <= H[i]:
        cnt += 1
        if i == N - 2:
            res.append(cnt)
    else:
        res.append(cnt)
        cnt = 0
print(max(res))
Problem p02997 (python)

Fixed submission:
import math

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

upper = (N-1)*(N-2) // 2
if upper < K:
    print(-1)
else:
    diff = upper - K
    edges = []
    for i in range(2, N+1):
        edges.append((1, i))
    j = 2
    while diff > 0:
        k = j + 1
        while k <= N and diff > 0:
            edges.append((j, k))
            k += 1
            diff -= 1
        j += 1
    print(len(edges))
    for e in edges:
        print(*e)

Buggy submission (verdict: Runtime Error):
import math

def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


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

if N == 2:
    if K == 0:
        print(1)
        print(1, 2)
    else:
        print(-1)
else:
    cmb = combinations_count(N-1, 2)
    if cmb < K or K < N - 2:
        print(-1)
    else:
        d = cmb - K
        edges = []
        for i in range(1, d+1):
            edges.append((i, i+1))
        for j in range(1, N - d):
            edges.append((i+1, i+1+j))
        print(N-1)
        for e in edges:
            print(*e)
Problem p03000 (python)

Fixed submission:
N, X = map(int, input().split())
L = list(map(int, input().split()))

d = 0
cnt = 0
while d <= X:
    cnt += 1
    if cnt == N + 1:
        break
    d += L[cnt-1]
print(cnt)

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

d = 0
cnt = 0
while d <= X:
    cnt += 1
    d += L[cnt-1]
    if cnt == N + 1:
        break
print(cnt)
Problem p03044 (python)

Fixed submission:
import sys
sys.setrecursionlimit(1000000000)

N = int(input())
u_v_w = [list(map(int, input().split())) for i in range(N-1)]

w = [u_v_w[i][2] for i in range(N-1)]

tree = [[] for i in range(N)]

for i in range(N-1):
    u = u_v_w[i][0]
    v = u_v_w[i][1]
    tree[u-1].append((v,w[i]))
    tree[v-1].append((u,w[i]))

ans = [0] * N
ans[0] = 1
def dfs(now):
    global ans
    for next in tree[now-1]:
        if ans[next[0]-1]:
            continue
        if next[1] % 2 == 0:
            ans[next[0]-1] = ans[now-1]
        else:
            ans[next[0]-1] = ans[now-1] * (-1)
        dfs(next[0])

dfs(1)
for i in range(len(ans)):
    if ans[i] == 1:
        print(0)
    else:
        print(1)

Buggy submission (verdict: Runtime Error):
N = int(input())
u_v_w = [list(map(int, input().split())) for i in range(N-1)]

w = [u_v_w[i][2] for i in range(N-1)]

tree = [[] for i in range(N)]

for i in range(N-1):
    u = u_v_w[i][0]
    v = u_v_w[i][1]
    tree[u-1].append((v,w[i]))
    tree[v-1].append((u,w[i]))

ans = [0] * N
ans[0] = 1
def dfs(now):
    global ans
    for next in tree[now-1]:
        if ans[next[0]-1]:
            continue
        if next[1] % 2 == 0:
            ans[next[0]-1] = ans[now-1]
        else:
            ans[next[0]-1] = ans[now-1] * (-1)
        dfs(next[0])

dfs(1)
for i in range(len(ans)):
    if ans[i] == 1:
        print(0)
    else:
        print(1)
Problem p03579 (python)

Fixed submission:
import sys

sys.setrecursionlimit(10**9)

N, M = map(int, input().split())
edges = [list(map(int, input().split())) for i in range(M)]

#隣接リストで管理
adj = [[] for j in range(N)]
for edge in edges:
    x, y = edge
    adj[x-1].append(y)
    adj[y-1].append(x)

color = [0] * N

#二部グラフ判定
def dfs(node, c):
    color[node-1] = c
    for i in adj[node-1]:
        if c == color[i-1]:
            return False
        if color[i-1] == 0 and not dfs(i, -c):
            return False
    return True

if dfs(1, 1):
    black = color.count(1)
    white = N - black
    print(black * white - M)
else:
    print(N * (N-1) // 2 - M)

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())
edges = [list(map(int, input().split())) for i in range(M)]

#隣接リストで管理
adj = [[] for j in range(N)]
for edge in edges:
    x, y = edge
    adj[x-1].append(y)
    adj[y-1].append(x)

color = [0] * N

#二部グラフ判定
def dfs(node, c):
    color[node-1] = c
    for i in adj[node-1]:
        if c == color[i-1]:
            return False
        if color[i-1] == 0 and not dfs(i, -c):
            return False
    return True

if dfs(1, 1):
    black = color.count(1)
    white = N - black
    print(black * white - M)
else:
    print(N * (N-1) // 2 - M)