Buggy python submission (verdict: Runtime Error):
#!/usr/bin/env python
# coding: utf-8

import sys
sys.setrecursionlimit(10000)

def paint(k, same, diff, visited, csame, cdiff):
    if k in visited:
        return
    if k in same:
        return
    csame.add(k)
    visited.add(k)
    if k in diff:
        for e in diff[k]:
            if e in visited:
                continue
            paint(e, same, diff, visited, cdiff, csame)
    if k in same:
        for e in same[k]:
            if e in visited:
                continue
            paint(e, same, diff, visited, csame, cdiff)

def main():
    n = int(input())
    same = {}
    diff = {}
    for _ in range(n-1):
        u, v, w = list(map(int, input().split()))
        if w % 2 == 0:
            same.setdefault(u, [])
            same.setdefault(v, [])
            same[u].append(v)
            same[v].append(u)
        else:
            diff.setdefault(u, [])
            diff.setdefault(v, [])
            diff[u].append(v)
            diff[v].append(u)
    white = set()
    black = set()
    visited = set()
    for k in diff:
        paint(k, same, diff, visited, white, black)
    ret = []
    for i in range(n):
        if i+1 in white:
            ret.append("0")
        else:
            ret.append("1")
    print("\n".join(ret))


if __name__ == '__main__':
    main()


Fixed submission:
#!/usr/bin/env python
# coding: utf-8

def main():
    n = int(input())
    g = [dict() for _ in range(n)]
    for _ in range(n-1):
        u, v, w = list(map(int, input().split()))
        g[u-1][v-1] = w
        g[v-1][u-1] = w
    dist = [-1 for _ in range(n)]
    dist[0] = 0
    q = [0]
    while len(q) > 0:
        f = q.pop()
        for t in g[f]:
            if dist[t] != -1:
                continue
            dist[t] = dist[f] + g[f][t]
            q.append(t)
    for i in range(n):
        if dist[i] % 2 == 0:
            print(0)
        else:
            print(1)


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Runtime Error):
#!/usr/bin/env python
# coding: utf-8

import math

def paint(k, v, diff, visited, same, opposite):
    if k in visited:
        return
    if k in same:
        return
    same.add(k)
    visited.add(k)
    for e in v:
        if e in visited:
            continue
        paint(e, diff[e], diff, visited, opposite, same)

def main():
    n = int(input())
    diff = {}
    for _ in range(n-1):
        u, v, w = list(map(int, input().split()))
        if w % 2 == 1:
            diff.setdefault(u, [])
            diff.setdefault(v, [])
            diff[u].append(v)
            diff[v].append(u)
    white = set()
    black = set()
    visited = set()
    for k in diff:
        paint(k, diff[k], diff, visited, white, black)
    ret = []
    for i in range(n):
        if i+1 in white:
            ret.append("0")
        else:
            ret.append("1")
    print("\n".join(ret))


if __name__ == '__main__':
    main()


Fixed submission:
#!/usr/bin/env python
# coding: utf-8

def main():
    n = int(input())
    g = [dict() for _ in range(n)]
    for _ in range(n-1):
        u, v, w = list(map(int, input().split()))
        g[u-1][v-1] = w
        g[v-1][u-1] = w
    dist = [-1 for _ in range(n)]
    dist[0] = 0
    q = [0]
    while len(q) > 0:
        f = q.pop()
        for t in g[f]:
            if dist[t] != -1:
                continue
            dist[t] = dist[f] + g[f][t]
            q.append(t)
    for i in range(n):
        if dist[i] % 2 == 0:
            print(0)
        else:
            print(1)


if __name__ == '__main__':
    main()