Problem p03044 (python)

Fixed submission:
N = int(input())

connections = [[] for _ in range(N)]

for _ in range(N-1):
    u, v, w = map(int, input().split())
    u -= 1
    v -= 1
    connections[u].append((v, w))
    connections[v].append((u, w))

COLOR_WHITE = 0
COLOR_BLACK = 1
COLOR_NONE = 2

colors = [COLOR_NONE for _ in range(N)]
colors[0] = COLOR_WHITE

stack = [(0, 0)]

while stack:
    root, abs_distance = stack.pop()
    for node, distance in connections[root]:
        if colors[node] == COLOR_NONE:
            new_distance = abs_distance+distance
            colors[node] = COLOR_WHITE if (new_distance) % 2 == 0 else COLOR_BLACK
            stack += [(node, new_distance)]

for c in colors:
    print(c)

Buggy submission (verdict: Runtime Error):
N = int(input())

class Tree():
    def __init__(self):
        self.connections = []

    def set_connection(self, root, distance):
        self.connections.append((root, distance))

trees = [Tree() for _ in range(N)]
for _ in range(N-1):
    u, v, w = map(int, input().split())
    trees[u-1].set_connection(v-1, w)
    trees[v-1].set_connection(u-1, w)

COLOR_WHITE = 0
COLOR_BLACK = 1
COLOR_NONE = 2

colors = [COLOR_NONE for _ in range(N)]

def set_color(node, distance):
    for root, d in node.connections:
        if colors[root] == COLOR_NONE:
            new_distance = distance + d
            colors[root] = COLOR_WHITE if new_distance % 2 == 0 else COLOR_BLACK
            set_color(trees[root], new_distance)

colors[0] = COLOR_WHITE
set_color(trees[0], 0)
for c in colors:
    print(c)

Buggy submission (verdict: Runtime Error):
N = int(input())

class UnionFind():
    def __init__(self, N):
        self.nodes = [i for i in range(N)]

    def root(self, x):
        if self.nodes[x] != x:
            self.nodes[x] = self.root(self.nodes[x])
        return self.nodes[x]

    def same(self, x, y):
        return self.root(x) == self.root(y)

    def unite(self, x, y):
        if self.same(x, y):
            return
        self.nodes[self.root(x)] = self.root(y)

tree = UnionFind(N)
anti_list = []
for _ in range(N-1):
    u, v, w = map(int, input().split())
    if w % 2 == 0:
        tree.unite(u-1, v-1)
    else:
        anti_list.append((u-1, v-1))

while len(anti_list) > 1:
    base_u, base_v = anti_list[0]
    new_anti_list = [(base_u, base_v)]
    for anti_u, anti_v in anti_list[1:]:
        u_root = tree.root(anti_u)
        v_root = tree.root(anti_v)
        if u_root == base_u:
            tree.unite(base_v, v_root)
        elif u_root == base_v:
            tree.unite(base_u, v_root)
        elif v_root == base_u:
            tree.unite(base_v, u_root)
        elif v_root == base_v:
            tree.unite(base_u, u_root)
        else:
            new_anti_list.append((u_root, v_root))
    anti_list = new_anti_list

for i in range(N):
    print(1 if tree.same(0, i) else 0)

Buggy submission (verdict: Runtime Error):
N = int(input())

class UnionFind():
    def __init__(self, N):
        self.nodes = [i for i in range(N)]

    def root(self, x):
        if self.nodes[x] != x:
            orig_root = self.nodes[x]
            self.nodes[x] = self.root(self.nodes[x])
        return self.nodes[x]

    def same(self, x, y):
        return self.root(x) == self.root(y)

    def unite(self, x, y):
        if self.same(x, y):
            return
        self.nodes[self.root(x)] = self.root(y)

tree = UnionFind(N)
anti_list = []
for _ in range(N-1):
    u, v, w = map(int, input().split())
    if w % 2 == 0:
        tree.unite(u-1, v-1)
    else:
        anti_list.append((u-1, v-1))

while len(anti_list) > 1:
    base_u, base_v = anti_list[0]
    base = [tree.root(base_u), tree.root(base_v)]
    new_anti_list = [(base_u, base_v)]
    for anti_u, anti_v in anti_list[1:]:
        u_root = tree.root(u)
        v_root = tree.root(v)
        if u_root == base_u:
            tree.unite(base_v, v_root)
        elif u_root == base_v:
            tree.unite(base_u, v_root)
        elif v_root == base_u:
            tree.unite(base_v, u_root)
        elif v_root == base_v:
            tree.unite(base_u, u_root)
        else:
            new_anti_list.append((u_root, v_root))
    anti_list = new_anti_list

for i in range(N):
    print(1 if tree.same(0, i) else 0)