Buggy python submission (verdict: Runtime Error):
def visit(current, v, dist, color):
    for ele in v[current]:
        if color[ele] != -1:
            continue
        if dist[(ele, current)] % 2 == 0:
            color[ele] = color[current]
        else:
            if color[current] == 0:
                color[ele] = 1
            else:
                color[ele] = 0
        visit(ele, v, dist, color)

def main():

    num = int(input())

    v = [[] for i in range(num + 1)]

    dist = {}

    for i in range(1, num):
        a, b, d = map(int, input().split())
        v[a].append(b)
        v[b].append(a)
        dist[(a, b)] = d
        dist[(b, a)] = d

    color = [-1 for i in range(num + 1)]
    color[1] = 0
    visit(1, v, dist, color)

    for i in range(1, num + 1):
        print(color[i])

if __name__ == '__main__':
    main()

Fixed submission:
import sys


def visit(current, v, dist, color):
    for ele in v[current]:
        if color[ele] != -1:
            continue
        if dist[(ele, current)] % 2 == 0:
            color[ele] = color[current]
        else:
            if color[current] == 0:
                color[ele] = 1
            else:
                color[ele] = 0
        visit(ele, v, dist, color)

def main():
    sys.setrecursionlimit(100100)
    num = int(input())

    v = [[] for i in range(num + 1)]

    dist = {}

    for i in range(1, num):
        a, b, d = map(int, input().split())
        v[a].append(b)
        v[b].append(a)
        dist[(a, b)] = d
        dist[(b, a)] = d

    color = [-1 for i in range(num + 1)]
    color[1] = 0
    visit(1, v, dist, color)

    for i in range(1, num + 1):
        print(color[i])

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict


def get_data(data_dic, num, check_data):
    for i in range(len(data_dic))[::-1]:
        if data_dic[i][0] == num:
            check_data.add(data_dic[i])
            data_dic.pop(i)
        elif data_dic[i][1] == num:
            data_add = (data_dic[i][1], data_dic[i][0], data_dic[i][2])
            check_data.add(data_add)
            data_dic.pop(i)
    return check_data, data_dic


def main():
    num = int(input())


    data_dic = [tuple(map(int, input().split())) for i in range(num - 1)]
    parent_data = [-1 for i in range(num + 1)]
    dist_data = [0 for i in range(num + 1)]

    check_data_now, data_dic = get_data(data_dic, 1, set())
    check_data_next = set()

    while len(check_data_now) > 0:
        parent_list = set()
        for u, v, w in check_data_now:
            parent_data[v] = u
            dist_data[v] = w % 2
            parent_list.add(v)


        for ele in parent_list:
            check_data_next, data_dic = get_data(data_dic, ele, check_data_next)
        check_data_now = check_data_next
        check_data_next = set()

    dist_ori_data = [-1 for i in range(num + 1)]
    dist_ori_data[1] = 0

    for i in range(1, num + 1):
        if dist_ori_data[i] != -1:
            continue
        dist = 0
        now_data = i
        while parent_data[now_data] != -1:
            if dist_ori_data[now_data] != -1:
                dist += dist_ori_data[now_data]
                break
            else:
                dist += dist_data[now_data]
                now_data = parent_data[now_data]
        dist_ori_data[i] = dist % 2

    for i in range(1, num + 1):
        dist = dist_ori_data[i]
        if dist % 2 == 0:
            print(0)
        else:
            print(1)








if __name__ == '__main__':
    main()



Fixed submission:
import sys


def visit(current, v, dist, color):
    for ele in v[current]:
        if color[ele] != -1:
            continue
        if dist[(ele, current)] % 2 == 0:
            color[ele] = color[current]
        else:
            if color[current] == 0:
                color[ele] = 1
            else:
                color[ele] = 0
        visit(ele, v, dist, color)

def main():
    sys.setrecursionlimit(100100)
    num = int(input())

    v = [[] for i in range(num + 1)]

    dist = {}

    for i in range(1, num):
        a, b, d = map(int, input().split())
        v[a].append(b)
        v[b].append(a)
        dist[(a, b)] = d
        dist[(b, a)] = d

    color = [-1 for i in range(num + 1)]
    color[1] = 0
    visit(1, v, dist, color)

    for i in range(1, num + 1):
        print(color[i])

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict


def get_data(data_dic, num, check_data):
    for i in range(len(data_dic))[::-1]:
        if data_dic[i][0] == num:
            check_data.add(data_dic[i])
            data_dic.pop(i)
        elif data_dic[i][1] == num:
            data_add = (data_dic[i][1], data_dic[i][0], data_dic[i][2])
            check_data.add(data_add)
            data_dic.pop(i)
    return check_data, data_dic


def main():
    num = int(input())


    data_dic = [tuple(map(int, input().split())) for i in range(num - 1)]
    parent_data = [-1 for i in range(num + 1)]
    dist_data = [0 for i in range(num + 1)]

    check_data_now, data_dic = get_data(data_dic, 1, set())
    check_data_next = set()

    while len(check_data_now) > 0:
        parent_list = set()
        for u, v, w in check_data_now:
            parent_data[v] = u
            dist_data[v] = w % 2
            parent_list.add(v)


        for ele in parent_list:
            check_data_next, data_dic = get_data(data_dic, ele, check_data_next)
        check_data_now = check_data_next
        check_data_next = set()


    for i in range(1, num + 1):
        now_data = i
        dist = 0

        while parent_data[now_data] != -1:
            dist += dist_data[now_data]
            now_data = parent_data[now_data]
        if dist % 2 == 0:
            print(0)
        else:
            print(1)








if __name__ == '__main__':
    main()



Fixed submission:
import sys


def visit(current, v, dist, color):
    for ele in v[current]:
        if color[ele] != -1:
            continue
        if dist[(ele, current)] % 2 == 0:
            color[ele] = color[current]
        else:
            if color[current] == 0:
                color[ele] = 1
            else:
                color[ele] = 0
        visit(ele, v, dist, color)

def main():
    sys.setrecursionlimit(100100)
    num = int(input())

    v = [[] for i in range(num + 1)]

    dist = {}

    for i in range(1, num):
        a, b, d = map(int, input().split())
        v[a].append(b)
        v[b].append(a)
        dist[(a, b)] = d
        dist[(b, a)] = d

    color = [-1 for i in range(num + 1)]
    color[1] = 0
    visit(1, v, dist, color)

    for i in range(1, num + 1):
        print(color[i])

if __name__ == '__main__':
    main()