Problem p03044 (python)

Fixed submission:
import sys
sys.setrecursionlimit(100000000)

N = int(input())

q = {}

for i in range(N):
    q[i] = []

for _ in range(N - 1):
    _u, _v, _w = map(int, input().split())

    q[_u - 1].append((_v - 1, _w))
    q[_v - 1].append((_u - 1, _w))

ans = [None] * N
ans[0] = 0


def search(x, parent_num):
    for s in q[x]:
        if ans[s[0]] is None:
            ans[s[0]] = (parent_num + s[1]) % 2
            search(s[0], ans[s[0]])


search(0, 0)

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

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

N = int(input())

q = {}

for i in range(N):
    q[i] = []

for _ in range(N - 1):
    _u, _v, _w = map(int, input().split())

    q[_u - 1].append((_v - 1, _w))
    q[_v - 1].append((_u - 1, _w))

ans = [None] * N
ans[0] = 0


def search(x, parent_num):
    for s in q[x]:
        if ans[s[0]] is None:
            ans[s[0]] = (parent_num + s[1]) % 2
            search(s[0], ans[s[0]])


search(0, 0)

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

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

N = int(input())

q = {}

for i in range(1, N + 1):
    q[i] = []

for _ in range(N - 1):
    _u, _v, _w = map(int, input().split())

    q[_u].append((_v, _w))
    q[_v].append((_u, _w))

s = 0
ans = [None] * N
ans[s] = 0


def search(x, _ans):
    for s in q[x]:
        if ans[s[0] - 1] is None:
            ans[s[0] - 1] = (_ans + s[1]) % 2
            search(s[0], ans[x - 1])


search(1, 0)

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

Buggy submission (verdict: Runtime Error):
from sys import exit

N = int(input())

q = {}

for i in range(1, N + 1):
    q[i] = []

for _ in range(N - 1):
    _u, _v, _w = map(int, input().split())

    q[_u].append((_v, _w))
    q[_v].append((_u, _w))

reached = []
distances = {}


def search(x, distance):
    # 達したことを記録
    reached.append(x)
    distances[x] = distance

    for s in q[x]:
        if s[0] not in reached:
            search(s[0], distance + s[1])


search(1, 0)

for i in range(1, N + 1):
    if i == 0:
        print(0)
        continue
    if distances[i] % 2 == 0:
        print(0)
    else:
        print(1)

Buggy submission (verdict: Runtime Error):
from sys import exit

N = int(input())

q = {}

for i in range(N):
    q[i + 1] = []

for i in range(N - 1):
    _u, _v, _w = map(int, input().split())

    q[_u].append((_v, _w))
    q[_v].append((_u, _w))

reached = []
distances = {}


def search(x, distance):
    if x in reached:
        # 既に達しているところだったら
        return

    # 達したことを記録
    reached.append(x)
    distances[x] = distance

    for s in q[x]:
        search(s[0], distance + s[1])


search(1, 0)

for i in range(N):
    if i == 0:
        print(0)
        continue
    if distances[i + 1] % 2 == 0:
        print(0)
    else:
        print(1)

Buggy submission (verdict: Runtime Error):
from sys import exit

N = int(input())

q = {}

for i in range(N):
    q[i + 1] = []

for i in range(N - 1):
    _u, _v, _w = map(int, input().split())

    q[_u].append((_v, _w))
    q[_v].append((_u, _w))

reached = []
distances = {}


def search(x, distance):
    if x in reached:
        # 既に達しているところだったら
        return

    # 達したことを記録
    reached.append(x)
    distances[x] = distance

    for s in q[x]:
        search(s[0], distance + s[1])


search(1, 0)

for i in range(N):
    if i == 0:
        print(0)
        continue
    if distances[i + 1] % 2 == 0:
        print(0)
    else:
        print(1)

Buggy submission (verdict: Runtime Error):
from sys import exit

N = int(input())

q = {}

for i in range(N):
    q[i + 1] = []

for i in range(N - 1):
    _u, _v, _w = map(int, input().split())

    q[_u].append((_v, _w))
    q[_v].append((_u, _w))

reached = []
distances = {}


def search(x, distance):
    if x in reached:
        # 既に達しているところだったら
        return

    # 達したことを記録
    reached.append(x)
    distances[x] = distance

    for s in q[x]:
        search(s[0], distance + s[1])


search(1, 0)
print(distances)

for i in range(N):
    if i == 0:
        print(0)
        continue
    if distances[i + 1] % 2 == 0:
        print(0)
    else:
        print(1)

Buggy submission (verdict: Runtime Error):
from sys import exit

N = int(input())

q = {}

for i in range(N):
    q[i + 1] = []

for i in range(N - 1):
    _u, _v, _w = map(int, input().split())

    q[_u].append((_v, _w))
    q[_v].append((_u, _w))

reached = []
distances = {}


def search(x, distance):
    if x in reached:
        # 既に達しているところだったら
        return

    # 達したことを記録
    reached.append(x)
    distances[x] = distance

    for s in q[x]:
        search(s[0], distance + s[1])


search(1, 0)
print(distances)

for i in range(N):
    if i == 0:
        print(0)
        continue
    if distances[i + 1] % 2 == 0:
        print(0)
    else:
        print(1)

Buggy submission (verdict: Runtime Error):
from sys import exit

N = int(input())

u = []
v = []
w = []

for i in range(N - 1):
    _u, _v, _w = map(int, input().split())
    u.append(_u)
    v.append(_v)
    w.append(_w)

reached = []
distances = {}


def my_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default


def search(x, distance):
    if x in reached:
        # 既に達しているところだったら
        return

    # 達したことを記録
    reached.append(x)
    distances[x] = distance

    while True:
        if x in u:
            index = u.index(x)
            new_x = v[index]
            new_distance = distance + w[index]

            u.pop(index)
            v.pop(index)
            w.pop(index)

            search(new_x, new_distance)
        elif x in v:
            index = v.index(x)
            new_x = u[index]
            new_distance = distance + w[index]

            u.pop(index)
            v.pop(index)
            w.pop(index)

            search(new_x, new_distance)
        else:
            break


search(1, 0)

for i in range(N):
    if i == 0:
        print(0)
        continue
    if distances[i + 1] % 2 == 0:
        print(0)
    else:
        print(1)