alexjercan/bugnet huggingface dataset:
Problem p03038 (Python) — buggy submission, verdict: Time Limit Exceeded

N, M = map(int, input().split())
A_list = list(map(int, input().split()))
A_list.sort()

BC_list = [list(map(int, input().split())) for _ in range(M)]
BC_list = sorted(BC_list, key=lambda x: x[1])

ans = 0
count = 0
i = N - 1
m = M - 1
while count < N:
    if A_list[i] < BC_list[m][1]:
        for _ in range(BC_list[m][0]):
            ans += BC_list[m][1]
            count += 1
            if count >= N:
                break
        m -= 1
    else:
        ans += A_list[i]
        count += 1
        i -= 1

    if m < 0:
        break

for _ in range(count, N):
    ans += A_list[i]
    i -= 1

print(ans)


Error: TLE

Fixed submission:

N, M = map(int, input().split())
A_list = list(map(int, input().split()))
A_list.sort()

BC_list = [list(map(int, input().split())) for _ in range(M)]
BC_list = [BC_list[i][1] for i in range(M) for _ in range(BC_list[i][0])]
A_list.extend(BC_list)
A_list.sort()

print(sum(A_list[-N:]))
Problem p03038 (Python) — buggy submission, verdict: Time Limit Exceeded

import sys

n, m = list(map(int, input().split()))
aaa = list(map(int, input().split()))
aaa.sort()
bc = []
for line in sys.stdin:
    b, c = map(int, line.split())
    bc.append((c, b))
bc.sort(reverse=True)
ans = 0
cnt = 0
for c, b in bc:
    while aaa and aaa[-1] >= c and cnt < n:
        ans += aaa.pop()
        cnt += 1
    if cnt + b >= n:
        ans += c * (n - cnt)
        cnt = n
        break
    ans += c * b
    cnt += b
if cnt < n:
    ans += sum(aaa[cnt - n :])
print(ans)


Error: TLE

Fixed submission:

import sys

n, m = list(map(int, input().split()))
aaa = list(map(int, input().split()))
for line in sys.stdin:
    b, c = list(map(int, line.split()))
    aaa = sorted(aaa + [c] * b, reverse=True)[:n]
print(sum(aaa))
Problem p03038 (Python) — buggy submission, verdict: Time Limit Exceeded

N, M = map(int, input().split())
As = list(map(int, input().split()))
As.sort(reverse=True)

BCs = []
for _ in range(M):
    b, c = map(int, input().split())
    BCs.append({"b": b, "c": c})

BCs = sorted(BCs, key=lambda bc: bc["c"], reverse=True)
# print(BCs)

new_cards = []
for i in range(M):
    new_cards += [BCs[i]["c"] for _ in range(BCs[i]["b"])]
    if len(new_cards) >= N:
        break

if len(new_cards) < N:
    new_cards += [0 for _ in range(N - len(new_cards))]
# print(new_cards)

A_index = 0
new_index = 0
new_As = []
for i in range(N):
    if As[A_index] >= new_cards[new_index]:
        new_As.append(As[A_index])
        A_index += 1
    else:
        new_As.append(new_cards[new_index])
        new_index += 1

print(sum(new_As))


Error: TLE

Fixed submission:

N, M = map(int, input().split())
As = list(map(int, input().split()))

for _ in range(M):
    As.sort()
    b, c = map(int, input().split())
    for i in range(b):
        if As[i] < c:
            As[i] = c
        else:
            break

print(sum(As))
Problem p03038 (Python) — buggy submission, verdict: Time Limit Exceeded

[n, m] = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
l = [[int(j) for j in input().split()] for i in range(m)]
l.sort(reverse=True, key=lambda x: x[1])

k = []
for i in l:
    k += [i[1]] * i[0]
    if len(k) >= n:
        break

k += a
k.sort(reverse=True)
print(sum(k[:n]))


Error: TLE

Fixed submission:

[n, m] = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]

for i in range(m):
    [b, c] = [int(i) for i in input().split()]
    a += [c for i in range(b)]

a.sort(reverse=True)
print(sum(a[:n]))
Problem p03038 (Python) — buggy submission, verdict: Time Limit Exceeded

n, m = map(int, input().split())
a = sorted(list(map(int, input().split())))
bc = [[0] * 2 for _ in range(m)]
for i in range(m):
    bc[i] = list(map(int, input().split()))
bc = sorted(bc, key=lambda x: x[1], reverse=True)
mat = []
for b, c in bc:
    if len(mat) + b > n:
        mat.extend([c] * (n - len(mat)))
        break
    else:
        mat.extend([c] * b)

for i, t in enumerate(a):
    if i == len(mat):
        break
    if t < mat[i]:
        a[i] = mat[i]
    else:
        break

print(sum(a))


Error: TLE

Fixed submission:

n, m = map(int, input().split())
a = sorted(list(map(int, input().split())))
mat = []
for _ in range(m):
    b, c = map(int, input().split())
    mat.extend([c] * b)
mat = sorted(mat, reverse=True)[:n]
for i, t in enumerate(a):
    if i == len(mat):
        break
    if t < mat[i]:
        a[i] = mat[i]
    else:
        break

print(sum(a))
Problem p03038 (Python) — buggy submission, verdict: Time Limit Exceeded

import sys

N, M = map(int, input().split())
A = list(map(int, sys.stdin.readline().rsplit()))
BC = [list(map(int, sys.stdin.readline().rsplit())) for _ in range(M)]
A.sort()
BC.sort(reverse=True, key=lambda x: x[1])
C = []

for b, c in BC:
    if len(C) >= N:
        break
    for _ in range(b):
        C.append(c)

C.sort(reverse=True)

res = 0
lenC = len(C)
for i in range(N):
    if i < lenC and A[i] < C[i]:
        res += C[i]
    else:
        res += A[i]
print(res)


Error: TLE

Fixed submission:

import sys

N, M = map(int, input().split())
A = list(map(int, sys.stdin.readline().rsplit()))
A.sort()
C = []

for _ in range(M):
    b, c = map(int, input().split())
    C.extend([c] * b)

C.sort(reverse=True)

res = 0
for i in range(N):
    if i < len(C):
        if A[i] < C[i]:
            res += C[i]
        else:
            res += A[i]
    else:
        res += A[i]
print(res)
Problem p03038 (Python) — buggy submission, verdict: Time Limit Exceeded

import bisect

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

BC = []
for _ in range(M):
    b, c = map(int, input().split())
    BC.append((b, c))

BC.sort(reverse=True, key=lambda x: x[1])

ans, i = 0, 0
for bc in BC:
    ti = bisect.bisect(A, bc[1])
    if (ti > i) and (bc[0] >= (ti - i)):
        ans += bc[1] * (ti - i)
        i = ti
    elif (ti > i) and (bc[0] < (ti - i)):
        ans += bc[1] * bc[0]
        i = i + bc[0]

print(ans + sum(A[i:]))


Error: TLE

Fixed submission:

import bisect

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

BC = []
for _ in range(M):
    b, c = map(int, input().split())
    BC.append((b, c))

BC.sort(reverse=True, key=lambda x: x[1])

ans, i = 0, 0
for bc in BC:
    ti = bisect.bisect(A[i:], bc[1]) + i
    if (ti > i) and (bc[0] >= (ti - i)):
        ans += bc[1] * (ti - i)
        i = ti
    elif (ti > i) and (bc[0] < (ti - i)):
        ans += bc[1] * bc[0]
        i = i + bc[0]

print(ans + sum(A[i:]))
Problem p03038 (Python) — buggy submission, verdict: Time Limit Exceeded

import sys

fin = sys.stdin.readline

N, M = map(int, fin().split())
As = list(map(int, fin().split()))
BC = [tuple(map(int, fin().split())) for _ in range(M)]

As = sorted(As)
# BC = sorted(BC, key=lambda x: x[1], reverse=True)

BC_ = {}
for num, val in BC:
    if val in BC_:
        BC_[val] += num
    else:
        BC_[val] = num

BC_ = sorted(BC_.items(), reverse=True)

#
# def f(a, n, v):
#     if a[min(n-1, len(a)-1)] < v:
#         return v * min(n, len(a)), a[n:]
#     count = 0
#     for i in range(min(n, len(a))):
#         if a[i] < v:
#             count += 1
#         else:
#             break
#     return v * count, a[count:]


sum__ = 0
now = 0
for val, num in BC_:
    if now >= len(As) or As[now] >= val:
        break

    if As[min(num - 1 + now, len(As) - 1)] < val:
        sum_ = val * min(num, len(As) - now)
        now += min(num, len(As) - now)
    else:
        count = 0
        for i in range(min(num, len(As) - now)):
            if As[i + now] < val:
                count += 1
            else:
                break
        sum_ = val * count
        now += count
    sum__ += sum_

print(sum__ + sum(As[now:]))


Error: TLE

Fixed submission:

import sys

fin = sys.stdin.readline

N, M = map(int, fin().split())
As = list(map(int, fin().split()))
BC = [tuple(map(int, fin().split())) for _ in range(M)]

As = sorted(As)
# BC = sorted(BC, key=lambda x: x[1], reverse=True)

BC_ = {}
for num, val in BC:
    if val in BC_:
        BC_[val] += num
    else:
        BC_[val] = num

BC_ = sorted(BC_.items(), reverse=True)

#
# def f(a, n, v):
#     if a[min(n-1, len(a)-1)] < v:
#         return v * min(n, len(a)), a[n:]
#     count = 0
#     for i in range(min(n, len(a))):
#         if a[i] < v:
#             count += 1
#         else:
#             break
#     return v * count, a[count:]


sum__ = 0
for val, num in BC_:
    if len(As) == 0 or As[0] >= val:
        break

    if As[min(num - 1, len(As) - 1)] < val:
        sum_, As = val * min(num, len(As)), As[num:]
    else:
        count = 0
        for i in range(min(num, len(As))):
            if As[i] < val:
                count += 1
            else:
                break
        sum_, As = val * count, As[count:]
    sum__ += sum_

print(sum__ + sum(As))
Problem p03038 (Python) — buggy submission, verdict: Time Limit Exceeded

import heapq

n, m = map(int, input().split())
a = list(map(int, input().split()))
heapq.heapify(a)
bc = [list(map(int, input().split())) for _ in range(m)]
bc = sorted(bc, key=lambda x: -x[1])
for b, c in bc:
    for _ in range(b):
        x = heapq.heappop(a)
        if c > x:
            heapq.heappush(a, c)
        else:
            heapq.heappush(a, x)
            break
print(sum(a))


Error: TLE

Fixed submission:

import heapq

n, m = map(int, input().split())
a = list(map(int, input().split()))
heapq.heapify(a)
bc = [list(map(int, input().split())) for _ in range(m)]
for b, c in bc:
    for _ in range(b):
        x = heapq.heappop(a)
        if c > x:
            heapq.heappush(a, c)
        else:
            heapq.heappush(a, x)
print(sum(a))
Problem p03038 (Python) — buggy submission, verdict: Time Limit Exceeded

def resolve():
    n, m = list(map(int, input().split()))
    a = list(map(int, input().split()))
    bc = list(list(map(int, input().split())) for _ in range(m))
    pairs = [(ai, 1) for ai in a]
    for b, c in bc:
        pairs.append((c, b))
    pairs.sort(key=lambda x: x[0], reverse=True)
    c = 0
    ans = 0
    for v, num in pairs:
        if c + num <= n:
            ans += v * num
            c += num
        else:
            ans += v * (n - c)
            break
    print(ans)


if __name__ == "__main__":
    resolve()


Error: TLE

Fixed submission:

def resolve():
    n, m = list(map(int, input().split()))
    a = list(map(int, input().split()))
    bc = list(list(map(int, input().split())) for _ in range(m))
    for b, c in bc:
        a += [c for _ in range(b)]
    print(sum(sorted(a, reverse=True)[:n]))


if __name__ == "__main__":
    resolve()
Problem p03040 (Python) — buggy submission, verdict: Runtime Error

from heapq import heappush, heappushpop

Q = int(input())

b = 0
la = []
sl = 0
ra = []
sr = 0

cnt = 0
for _ in range(Q):
    q = tuple(map(int, input().split()))
    # print(la, ra)
    if q[0] == 2:
        # 求値
        x = -la[0]
        cl = (cnt + 1) // 2
        cr = cnt - cl
        print(x, (cl * x - sl) + (sr - cr * x) + b)
    else:
        # 更新
        b += q[2]
        cnt += 1
        if cnt % 2 == 1:
            sr += q[1]
            x = heappushpop(ra, q[1])
            sr -= x
            sl += x
            heappush(la, -x)
        else:
            sl += q[1]
            x = heappushpop(la, -q[1])
            x *= -1
            sl -= x
            sr += x
            heappush(ra, x)


Error: TypeError: 'map' object is not subscriptable
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03040/Python/s771493021.py", line 15, in <module>
    if q[0] == 2:
TypeError: 'map' object is not subscriptable


Fixed submission:

from heapq import heappush, heappushpop

Q = int(input())

b = 0
la = []
sl = 0
ra = []
sr = 0

cnt = 0
for _ in range(Q):
    q = map(int, input().split())
    # print(la, ra)
    if q[0] == 2:
        # 求値
        x = -la[0]
        cl = (cnt + 1) // 2
        cr = cnt - cl
        print(x, (cl * x - sl) + (sr - cr * x) + b)
    else:
        # 更新
        b += q[2]
        cnt += 1
        if cnt % 2 == 1:
            sr += q[1]
            x = heappushpop(ra, q[1])
            sr -= x
            sl += x
            heappush(la, -x)
        else:
            sl += q[1]
            x = heappushpop(la, -q[1])
            x *= -1
            sl -= x
            sr += x
            heappush(ra, x)
Problem p03040 (Python) — buggy submission, verdict: Runtime Error

# -*- coding: utf-8 -*-
from heapq import heappush, heappop

qn = int(input())

low = []
high = []
ans = 0

for i in range(qn):
    q = [int(i) for i in input().split()]
    if q[0] == 1:
        ans += q[2]
        a = q[1]

        if len(low) >= 1:
            if a < -low[0]:
                ans += -low[0] - a
            elif a > high[0]:
                ans += a - high[0]

        # lowは降順に並べたいので-aを入れる
        heappush(low, -a)
        heappush(high, a)
        if -low[0] > high[0]:
            low0 = heappop(low)
            high0 = heappop(high)
            # lowとhighで正負が逆なので注意する
            heappush(low, -high0)
            heappush(high, -low0)

    elif q[0] == 2:
        print(-low[0], ans)


Error: IndexError: list index out of range
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03040/Python/s610424106.py", line 16, in <module>
    if a < -low[0]:
IndexError: list index out of range


Fixed submission:

# -*- coding: utf-8 -*-
from heapq import heappush, heappop

qn = int(input())

low = []
high = []
ans = 0
num = 0

for i in range(qn):
    q = [int(i) for i in input().split()]
    if q[0] == 1:
        ans += q[2]
        a = q[1]
        if a < -low[0]:
            ans += -low[0] - a
        elif a > high[0]:
            ans += a - high[0]

        # lowは降順に並べたいので-aを入れる
        heappush(low, -a)
        heappush(high, a)
        if -low[0] > high[0]:
            low0 = heappop(low)
            high0 = heappop(high)
            # lowとhighで正負が逆なので注意する
            heappush(low, -high0)
            heappush(high, -low0)
    elif q[0] == 2:
        print(-low[0], ans)
Problem p03041 (Python) — buggy submission, verdict: Runtime Error

n, k = map(int, input().split())
s = input()
print(s[: k - 1] + s[k - 1].lower() + s[k:])


Error: WA

Fixed submission:

n, k = map(int, input().split())
s = input()

print(s.replace(s[k], s[k].lower()))
Problem p03041 (Python) — buggy submission, verdict: Runtime Error

n, k = map(int, input().split())
s = list(input())
s[k - 1] = s[k - 1].lower()
print("".join(s))


Error: WA

Fixed submission:

n, k = map(int, input().split())
s = list(input())
s[k] = s[k].lower()
print("".join(s))
Problem p03041 (Python) — buggy submission, verdict: Runtime Error

N, K = map(int, input().split())
S = list(input())
if S[K - 1] == "A":
    S[K - 1] = "a"
    print("".join(S))
elif S[K - 1] == "B":
    S[K - 1] = "b"
    print("".join(S))
else:
    S[K - 1] = "c"
    print("".join(S))


Error: TypeError: 'str' object does not support item assignment
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03041/Python/s243214892.py", line 4, in <module>
    S[K - 1] = "a"
TypeError: 'str' object does not support item assignment


Fixed submission:

N, K = map(int, input().split())
S = str(input())
if S[K - 1] == "A":
    S[K - 1] = "a"
    print("".join(S))
elif S[K - 1] == "B":
    S[K - 1] = "b"
    print("".join(S))
else:
    S[K - 1] = "c"
    print("".join(S))
Problem p03041 (Python) — buggy submission, verdict: Runtime Error

n, k = map(int, input().split())
s = input()
print(s[: k - 1] + s[k - 1].lower() + s[k:])


Error: TypeError: 'str' object does not support item assignment
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03041/Python/s255611980.py", line 3, in <module>
    s[k - 1] = s[k - 1].lower()
TypeError: 'str' object does not support item assignment


Fixed submission:

n, k = map(int, input().split())
s = input()
s[k - 1] = s[k - 1].lower()
print(s)
Problem p03042 (Python) — buggy submission, verdict: Runtime Error

from datetime import datetime

S = input()
former = int(S[:2])
latter = int(S[2:])
YYMM = False
MMYY = False

try:
    datetime(former + 2000, latter, 1)
    YYMM = True
except:
    pass

try:
    datetime(latter + 2000, former, 1)
    MMYY = True
except:
    pass

if YYMM and MMYY:
    print("AMBIGUOUS")
elif YYMM:
    print("YYMM")
elif MMYY:
    print("MMYY")
else:
    print("NA")


Error: 0

Fixed submission:

def ismonth(month):
    return 0 < month < 13


S = input()

former = int(S[:2])
latter = int(S[2:])

if former < 1 or latter < 1:
    ans = "NA"

elif ismonth(former) and ismonth(latter):
    ans = "AMBIGUOUS"

elif ismonth(former):
    ans = "MMYY"

elif ismonth(latter):
    ans = "YYMM"

print(ans)
Problem p03042 (Python) — buggy submission, verdict: Runtime Error

a = input()
b = int(a[:2])
c = int(a[2:])
if 0 < b <= 12 and 0 < c <= 12:
    print("AMBIGUOUS")
elif 0 < b <= 12:
    print("MMYY")
elif 0 < c <= 12:
    print("YYMM")
else:
    print("NA")


Error: 0

Fixed submission:

a = input()
b = int(a[:2])
c = int(a[2:])
if 0 < b <= 12 and a < c <= 12:
    print("AMBIGUOUS")
elif 0 < b <= 12 and c > 12:
    print("MMYY")
elif b > 12 and 0 < c <= 12:
    print("YYMM")
else:
    print("NA")
Problem p03042 (Python) — buggy submission, verdict: Runtime Error

#!/usr/bin/env python3
import sys


def solve(S: int):

    if 1 <= int(S[2:]) <= 12:
        if 1 <= int(S[:2]) <= 12:
            print("AMBIGUOUS")
        else:
            print("YYMM")
    else:
        if 1 <= int(S[:2]) <= 12:
            print("MMYY")
        else:
            print("NA")

    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word

    tokens = iterate_tokens()
    S = str(next(tokens))  # type: int
    solve(S)


if __name__ == "__main__":
    main()


Error: 0

Fixed submission:

#!/usr/bin/env python3
import sys


def solve(S: int):

    S = str(S)

    if int(S[:2]) > 12 and 1 <= int(S[2:]) <= 12:
        print("YYMM")
    elif 1 <= int(S[:2]) <= 12 and int(S[2:]) > 12:
        print("MMYY")
    elif 1 <= int(S[:2]) <= 12 and 1 <= int(S[2:]) <= 12:
        print("AMBIGUOUS")
    else:
        print("NA")

    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word

    tokens = iterate_tokens()
    S = int(next(tokens))  # type: int
    solve(S)


if __name__ == "__main__":
    main()
Problem p03043 (Python) — buggy submission, verdict: Runtime Error

import math

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

count = 1
while True:
    p += (
        max(0, min(N + 1, math.ceil(K / 2 ** (count - 1))) - math.ceil(K / 2**count))
        * 0.5**count
    )
    count += 1
    if K / 2**count < 0.5:
        break

p += max(0, N - K + 1)
print(p / N)


Error: WA

Fixed submission:

import math

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

for i in range(1, min(K, N + 1)):
    print(math.ceil(math.log2(K / i)))
    p += 2 ** (-math.ceil(math.log2(K / i)))

p += max(0, N - K + 1)
print(p / N)
Problem p03043 (Python) — buggy submission, verdict: Time Limit Exceeded

import decimal


def main():
    N, K = map(int, input().split())
    ans = 0
    for n in range(1, N + 1):
        s = 0
        s += n
        c = 0
        while K > s:
            s *= 2
            c += 1
        ans += decimal.Decimal((1 / N) * (1 / 2) ** c)
    print(ans)


main()


Error: TLE

Fixed submission:

import decimal


def main():
    N, K = map(int, input().split())
    s = N
    c = 0
    while K > s:
        s *= 2
        c += 1
    cp = (1 / 2) ** c
    ans = decimal.Decimal((1 / N) * cp)
    cc = c
    for n in range(N - 1, 0, -1):
        s = n
        c = 0
        while K > s:
            s *= 2
            c += 1
        cp *= 1 / 2 ** (c - cc)
        ans += decimal.Decimal((1 / N) * cp)
        cc = c
    print(ans)


main()
Problem p03043 (Python) — buggy submission, verdict: Time Limit Exceeded

def main():
    N, K = map(int, input().split())
    ans = 0
    for n in range(1, N + 1):
        s = n
        c = 1
        while K > s:
            s *= 2
            c /= 2
        ans += (1 / N) * c
    print(ans)


main()


Error: TLE

Fixed submission:

import decimal


def main():
    N, K = map(int, input().split())
    ans = 0
    for n in range(1, N + 1):
        s = n
        c = 1
        while K > s:
            s *= 2
            c /= 2
        ans += decimal.Decimal((1 / N) * c)
    print(ans)


main()
Problem p03043 (Python) — buggy submission, verdict: Time Limit Exceeded

n, k = map(int, input().split())
sum = 0.0
for i in range(1, n + 1):
    p = 1.0 / n
    score = i
    while score < k:
        score = score * 2
        p = p * 0.5
    sum += p
print(sum)


Error: TLE

Fixed submission:

from decimal import Decimal

n, k = map(int, input().split())
sum = Decimal(0.0)
for i in range(1, n + 1):
    p = Decimal(1.0 / n)
    score = i
    while score < k:
        score = score * 2
        p = p * Decimal(0.5)
    sum += p
print(str(sum)[:14])
Problem p03044 (Python) — buggy submission, verdict: Runtime Error

import sys

sys.setrecursionlimit(10**6)


class Tree:
    WHITE = 0
    GRAY = 1
    BLACK = 2

    def __init__(self, adj):
        n = len(adj)
        self.adj = adj
        self.colors = [self.WHITE] * n
        self.depths = [-1] * n
        self.depth = 0

    def dfs(self, u):
        if self.colors[u] == self.BLACK:
            return

        self.colors[u] = self.GRAY
        self.depths[u] = self.depth

        for v, w in self.adj[u]:
            if self.colors[v] == self.WHITE:
                self.depth += w
                self.dfs(v)
                self.depth -= w

        self.colors[u] = self.BLACK


if __name__ == "__main__":
    n = int(input())
    uvw = [list(map(int, input().split())) for _ in range(n - 1)]

    g = [[] for _ in range(n)]

    for u, v, w in uvw:
        u -= 1
        v -= 1
        g[u].append((v, w))
        g[v].append((u, w))

    tree = Tree(g)
    tree.dfs(0)
    ans = [x % 2 for x in tree.depths]
    print(*ans, sep="\\n")


Error: 0

Fixed submission:

class Tree:
    WHITE = 0
    GRAY = 1
    BLACK = 2

    def __init__(self, adj):
        n = len(adj)
        self.adj = adj
        self.colors = [self.WHITE] * n
        self.depths = [-1] * n
        self.depth = 0

    def dfs(self, u):
        if self.colors[u] == self.BLACK:
            return

        self.colors[u] = self.GRAY
        self.depths[u] = self.depth

        for v, w in self.adj[u]:
            if self.colors[v] == self.WHITE:
                self.depth += w
                self.dfs(v)
                self.depth -= w

        self.colors[u] = self.BLACK


if __name__ == "__main__":
    n = int(input())
    uvw = [list(map(int, input().split())) for _ in range(n - 1)]

    g = [[] for _ in range(n)]

    for u, v, w in uvw:
        u -= 1
        v -= 1
        g[u].append((v, w))
        g[v].append((u, w))

    tree = Tree(g)
    tree.dfs(0)
    ans = [x % 2 for x in tree.depths]
    print(*ans, sep="\\n")