Buggy python submission (verdict: Time Limit Exceeded):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

from collections import deque 
def main():
    N = int(readline())
    to = [[] for _ in range(N)]
    wight = [[-1 for _ in range(N)] for _ in range(N)]
    for _ in range(N-1):
        u, v, w = map(int, readline().split())
        u-=1
        v-=1
        to[u].append(v)
        to[v].append(u)
        wight[u][v] = w
        wight[v][u] = w

    dq = deque()
    dq.append(0)
    dist = [-1 for _ in range(N)]
    dist[0] = 0
    while len(dq)>0:
        v = dq.popleft()
        for nx in to[v]:
            if dist[nx] != -1:
                continue
            dist[nx] = (dist[v]+wight[v][nx])%2
            dq.append(nx)

    for i in dist:
        print(i)
if __name__ == '__main__':
    main()


Fixed submission:
#python3
from collections import deque
def main():
    n = int(input()) 
    #隣接リスト表現でグラフを管理する
    #v-uのcostはto[v]においてuが格納されている位置と同じcost[v]リスト位置に保存するような対応関係をとる
    to = [[] for _ in range(n)]
    cost = [[] for _ in range(n)]
    for _ in range(n-1):
        v, u, w  = map(int, input().split())
        v -= 1
        u -= 1
        to[v].append(u)
        cost[v].append(w)
        to[u].append(v)
        cost[u].append(w)

    
    #根からの距離を偶奇をBFSで求める    
    dist = [-1 for _ in range(n)]
    dist[0] = 0
    q = deque()
    q.append(0)
    while len(q) != 0:
        #訪問予定の頂点を取り出す
        val = q.popleft()
        #頂点vに連結している頂点を探索すして訪問予定に追加する
        for i in range(len(to[val])):
            wal = to[val][i]
            cos = cost[val][i]
            if dist[wal] != -1:
                continue
            dist[wal] = (dist[val] + cos)%2 
            q.append(wal)
    
    for i in range(n):
        print(dist[i])
main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

from collections import deque 
def main():
    N = int(readline())
    to = [[] for _ in range(N)]
    wight = [[-1 for _ in range(N)] for _ in range(N)]
    for _ in range(N-1):
        u, v, w = map(int, readline().split())
        u-=1
        v-=1
        to[u].append(v)
        to[v].append(u)
        wight[u][v] = w%2
        wight[v][u] = w%2

    dq = deque()
    dq.append(0)
    dist = [-1 for _ in range(N)]
    dist[0] = 0
    while len(dq)>0:
        v = dq.popleft()
        for nx in to[v]:
            if dist[nx] != -1:
                continue
            dist[nx] = dist[v]^wight[v][nx]
            dq.append(nx)

    for i in dist:
        print(i)
if __name__ == '__main__':
    main()


Fixed submission:
#python3
from collections import deque
def main():
    n = int(input()) 
    #隣接リスト表現でグラフを管理する
    #v-uのcostはto[v]においてuが格納されている位置と同じcost[v]リスト位置に保存するような対応関係をとる
    to = [[] for _ in range(n)]
    cost = [[] for _ in range(n)]
    for _ in range(n-1):
        v, u, w  = map(int, input().split())
        v -= 1
        u -= 1
        to[v].append(u)
        cost[v].append(w)
        to[u].append(v)
        cost[u].append(w)

    
    #根からの距離を偶奇をBFSで求める    
    dist = [-1 for _ in range(n)]
    dist[0] = 0
    q = deque()
    q.append(0)
    while len(q) != 0:
        #訪問予定の頂点を取り出す
        val = q.popleft()
        #頂点vに連結している頂点を探索すして訪問予定に追加する
        for i in range(len(to[val])):
            wal = to[val][i]
            cos = cost[val][i]
            if dist[wal] != -1:
                continue
            dist[wal] = (dist[val] + cos)%2 
            q.append(wal)
    
    for i in range(n):
        print(dist[i])
main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

from collections import deque 
def main():
    N = int(readline())
    to = [[] for _ in range(N)]
    wight = [[-1 for _ in range(N)] for _ in range(N)]
    for _ in range(N-1):
        u, v, w = map(int, readline().split())
        u-=1
        v-=1
        to[u].append(v)
        to[v].append(u)
        wight[u][v] = w%2
        wight[v][u] = w%2

    dq = deque()
    dq.append(0)
    dist = [-1 for _ in range(N)]
    dist[0] = 0
    while len(dq)>0:
        v = dq.popleft()
        for nx in to[v]:
            if dist[nx] != -1:
                continue
            dist[nx] = dist[v]^wight[v][nx]
            dq.append(nx)

    for i in dist:
        print(i)
if __name__ == '__main__':
    main()


Fixed submission:
#python3
from collections import deque
def main():
    n = int(input()) 
    #隣接リスト表現でグラフを管理する
    #v-uのcostはto[v]においてuが格納されている位置と同じcost[v]リスト位置に保存するような対応関係をとる
    to = [[] for _ in range(n)]
    cost = [[] for _ in range(n)]
    for _ in range(n-1):
        v, u, w  = map(int, input().split())
        v -= 1
        u -= 1
        to[v].append(u)
        cost[v].append(w)
        to[u].append(v)
        cost[u].append(w)

    
    #根からの距離を偶奇をBFSで求める    
    dist = [-1 for _ in range(n)]
    dist[0] = 0
    q = deque()
    q.append(0)
    while len(q) != 0:
        #訪問予定の頂点を取り出す
        val = q.popleft()
        #頂点vに連結している頂点を探索すして訪問予定に追加する
        for i in range(len(to[val])):
            wal = to[val][i]
            cos = cost[val][i]
            if dist[wal] != -1:
                continue
            dist[wal] = (dist[val] + cos)%2 
            q.append(wal)
    
    for i in range(n):
        print(dist[i])
main()