Problem:


Score : 400 points

Problem Statement

We have a tree with N vertices numbered 1 to N.
The i-th edge in the tree connects Vertex u_i and Vertex v_i, and its length is w_i.
Your objective is to paint each vertex in the tree white or black (it is fine to paint all vertices the same color) so that the following condition is satisfied:

For any two vertices painted in the same color, the distance between them is an even number.

Find a coloring of the vertices that satisfies the condition and print it. It can be proved that at least one such coloring exists under the constraints of this problem.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq u_i < v_i \leq N

1 \leq w_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
u_1 v_1 w_1
u_2 v_2 w_2
.
.
.
u_{N - 1} v_{N - 1} w_{N - 1}

Output

Print a coloring of the vertices that satisfies the condition, in N lines.
The i-th line should contain 0 if Vertex i is painted white and 1 if it is painted black.

If there are multiple colorings that satisfy the condition, any of them will be accepted.

Sample Input 1

3
1 2 2
2 3 1

Sample Output 1

0
0
1

Sample Input 2

5
2 5 2
2 3 10
1 3 8
3 4 2

Sample Output 2

1
0
1
0
1



Sample input:
3
1 2 2
2 3 1


Sample output:
0
0
1

Submission status: Runtime Error

Solution code:
N = int(input())
visited = [False] * N
colors = [-1] * N
dist = [float('inf')] * N

def dfs(u):
    if visited[u]:
        return

    visited[u] = True
    for (v, w) in edges[u]:
        if visited[v]:
            continue
        if colors[v] != -1:
            continue

        dist[v] = dist[u] + w
        if dist[v] % 2 == 0:
            colors[v] = 0
        else:
            colors[v] = 1
        dfs(v)


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


colors[0] = 0
dist[0] = 0
dfs(0)
for c in colors:
    print(c)

Submission status: Runtime Error

Solution code:
import networkx as nx

N = int(input())
W = [list(map(int, input().split())) for _ in range(N)]
edge_labels = {}
for i in range(N):
    edge_labels[(W[i][0], W[i][1])] = W[i][2]

G = nx.Graph()

G.add_weighted_edged_from(W)

path = dict(nx.all_pairs_dijkasta_path(G))
print(0)
for i in range(1,N):
    road_weight = 0
    for k in range(N-1):
        road_weight += edge_labels[(min(path[0][i][k], path[0][i][k + 1]), max(path[0][i][k], path[0][i][k + 1]))]
        if road_weight % 2 == 0:
            print(0)
        else:
            print(1)

Submission status: Runtime Error

Solution code:
import math
import copy
from copy import deepcopy
import sys
import fractions
import numpy as np
from functools import reduce
# import statistics
import heapq
sys.setrecursionlimit(100001)

# input = sys.stdin.readline
# sys.setrecursionlimit(10**6)

# ===FUNCTION===

def getInputInt():
    inputNum = int(input())
    return inputNum


def getInputListInt():
    outputData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData


def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList


def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList


# ===CODE===

n = int(input())

data = getSomeInputListInt(n-1)

ans = [-1]*n

g = [[] for i in range(n)]
for i in range(n-1):
    u,v,w = map(int,input().split())
    u-=1;v-=1
    g[u].append([v,w])
    g[v].append([u,w])
ans = [-1]*n
def dfs(x,y):
    for i,j in g[x]:
        if ans[i] == -1:
            ans[i] = (y+j)%2
            dfs(i,y+j)
dfs(0,0)
for i in ans:
    print(i)

Submission status: Runtime Error

Solution code:
import math
import copy
from copy import deepcopy
import sys
import fractions
import numpy as np
from functools import reduce
# import statistics
import heapq
sys.setrecursionlimit(100001)

# input = sys.stdin.readline
# sys.setrecursionlimit(10**6)

# ===FUNCTION===

def getInputInt():
    inputNum = int(input())
    return inputNum


def getInputListInt():
    outputData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData


def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList


def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList


# ===CODE===

n = int(input())

data = getSomeInputListInt(n)

ans = [-1]*n

def searcher(num):
    for i in range(n-1):
        l = data[i][0]
        r = data[i][1]
        w = data[i][2]

        if l == num and ans[r-1] == -1:
            if w % 2 == 0:
                ans[r-1] = ans[num-1]
            else:
                ans[r-1] = (ans[num-1]+1) % 2
            searcher(r)

        if r == num and ans[l-1] == -1:
            if w % 2 == 0:
                ans[l-1] = ans[num-1]
            else:
                ans[l-1] = (ans[num-1]+1) % 2
            searcher(l)


l = data[0][0]
r = data[0][1]
w = data[0][2]

if w % 2 == 0:
    ans[r-1] = 0
    ans[l-1] = 0
else:
    ans[r-1] = 1
    ans[l-1] = 1

searcher(r)
searcher(l)

for a in ans:
    print(a)

Submission status: Runtime Error

Solution code:
import math
import copy
from copy import deepcopy
import sys
import fractions
import numpy as np
from functools import reduce
# import statistics
import heapq
sys.setrecursionlimit(100001)

# input = sys.stdin.readline
# sys.setrecursionlimit(10**6)

# ===FUNCTION===

def getInputInt():
    inputNum = int(input())
    return inputNum


def getInputListInt():
    outputData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData


def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList


def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList


# ===CODE===

n = int(input())

data = getSomeInputListInt(n)

ans = [-1]*n

def searcher(num):
    for i in range(n-1):
        l = data[i][0]
        r = data[i][1]
        w = data[i][2]

        if l == num and ans[r-1] == -1:
            if w % 2 == 0:
                ans[r-1] = ans[num-1]
            else:
                ans[r-1] = (ans[num-1]+1) % 2
            searcher(r)

        if r == num and ans[l-1] == -1:
            if w % 2 == 0:
                ans[l-1] = ans[num-1]
            else:
                ans[l-1] = (ans[num-1]+1) % 2
            searcher(l)


l = data[0][0]
r = data[0][1]
w = data[0][2]

if w % 2 == 0:
    ans[r-1] = 1
    ans[l-1] = 1
else:
    ans[r-1] = 0
    ans[l-1] = 0

searcher(r)
searcher(l)

for a in ans:
    print(a)

Submission status: Runtime Error

Solution code:
import sys
from collections import deque
input = sys.stdin.readline
N = int(input())
L = [deque([]) for _ in range(N)]
ans = [-1] * N

for n in range(N-1):
    u, v, w = [int(x) for x in input().strip().split()]
    L[u-1].append((v, w))
    L[v-1].append((u, w))

def dfs(x, w):
    if ans[x-1] >= 0:
        return
    ans[x-1] = w % 2
    for nx, w_ in L[x-1]:
        dfs(nx, (w + w_) % 2)
    return

dfs(1, 0)

for a in ans:
    print(a)

Submission status: Runtime Error

Solution code:
N = int(input())

G = []
for i in range(N):
    G.append([])

color = [None] * N

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

# for i in range(N):
#     print(G[i])

def dfs(v, c):
    color[v] = c

    # vと隣接する点
    for p, d in G[v]:
        # 訪問済み
        if color[p] is not None:
            continue
        if d % 2 == 0:
            dfs(p, c)
        else:
            dfs(p, c ^ 1)  # 排他的論理和で0, 1をひっくり返す

# 0の頂点を0で塗る
dfs(0, 0)

# print(color)
for col in color:
    print(col)

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!
def dfs(num,hiyo):
    ans[num]=hiyo
    for to,cos in way[num]:
        if ans[to]==-1:
            dfs(to,hiyo+cos)
    
    return 

N=int(input())

way=[[] for i in range(N)]
ans=[-1]*N

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

#lis.sort(key=lambda x:(x[0],x[1]))

dfs(0,0)
#print(ans)
for item in ans:
    print(item%2)

Submission status: Runtime Error

Solution code:
n=int(input())
color=[-1 for i in range(n)]
color[0]=1
G=[[] for i in range(n)]
for i in range(n-1):
  a,b,d=map(int,input().split())
  G[a-1].append([b-1,d])
  G[b-1].append([a-1,d])
  
def dfs(x):
  
  for i,dis in G[x]:
    
    if color[i] != -1: continue
    if dis %2==1:
      color[i]=1-color[x]
    else:
      color[i]=color[x]
    dfs(i)
dfs(0)
for i in range(n):
  print(color[i])

Submission status: Runtime Error

Solution code:
n=map(int,input().split())
color=[-1 for i in range(n)]
color[0]=0
G=[[] for i in range(n)]
for i in range(n-1):
  a,b,d=map(int,input().split())
  G[a-1].append([b-1,d])
  G[b-1].append([a-1,d])
  
def dfs(x):
  for i in G[x]:
    if color[i] != -1: return
    color[i]=1-color[x]
    dfs(i)
dfs(0)
print(*color)

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!
import sys
sys.setrecursionlimit(10**9)

def dfs(loc,num):
    root[loc]=num
    for item in plan[loc]:
        if root[item]==-1:
            dfs(item,num)
    return 0

N,M=map(int,input().split())

P=list(map(int,input().split()))

plan=[[]for i in range(N)]
root=[-1]*N

for _ in range(M):
    a,b=map(int,input().split())
    plan[a-1].append(b-1)
    plan[b-1].append(a-1)

for i in range(N):
    if root[i]==-1:
        dfs(i,i)

ans=0
for i in range(N):
    if root[i]==root[P[i]-1]:
        ans+=1
#print(root)
print(ans)

Submission status: Runtime Error

Solution code:
tree = {}
colors = {}


def dfs(v,p,c):
  if not (v in colors):
    colors[v] = c
  nodes = tree[v]
  for node,weight in nodes.items():
    if node == p:
      continue
    if weight % 2 == 0:
      dfs(node,v, c)
    else:
      dfs(node,v,(c+1)%2)

N = int(input())
for i in range(N-1):
  u, v, w = [int(i) for i in input().split()]
  if u in tree:
    tree[u][v] = w
  else:
    tree[u] = {v: w}
    
  if v in tree:
    tree[v][u] = w
  else:
    tree[v] = {u: w}

dfs(1,-1,0)

for c in colors.values():
  print(c)

Submission status: Runtime Error

Solution code:
n=int(input())
if n<2:
    print(1)
    exit()
A=[[] for _ in range(n)]
B=[0 for _ in range(n)]
for i in range(n-1):
    u,v,w=map(int,input().split())
    u-=1
    v-=1
    A[u].append([v,w,0])
    A[v].append([u,w,0])
def s(x):
    for i,a in enumerate(A[x]):
        if a[2]==0:
            if a[1]%2==0:B[a[0]]=B[x]
            else:B[a[0]]=B[x]^1
            A[x][i][2]=1
            for j,z in enumerate(A[a[0]]):
                if z[0]==x:A[a[0]][j][2]=1
            return s(a[0])
s(0)
for i in range(n):
    for a in A[i]:
        if a[2]==0:s(i)
for b in B:print(b)

Submission status: Runtime Error

Solution code:
n=int(input())
if n<2:
    print(1)
    exit()
A=[[] for _ in range(n)]
B=[0 for _ in range(n)]
for i in range(n-1):
    u,v,w=map(int,input().split())
    u-=1
    v-=1
    A[u].append([v,w,0])
    A[v].append([u,w,0])
def s(x):
    for i,a in enumerate(A[x]):
        if a[2]<1:
            if a[1]%2<1:B[a[0]]=B[x]
            else:B[a[0]]=B[x]^1
            A[x][i][2]=1
            for j,z in enumerate(A[a[0]]):
                if z[0]==x:A[a[0]][j][2]=1
            return s(a[0])
s(0)
for b in B:print(b)

Submission status: Runtime Error

Solution code:
from collections import defaultdict, deque

n, m = map(int, input().split())

d = [0]*(n+1)  # カードの整数が判明したら1にする
ddict = defaultdict(list)  # リスト内のカードと関係がある
relation = [[i, 0] for i in range(1, n+1)]  # iはk枚のカードと関係がある
for _ in range(m):
    x, y, z = map(int, input().split())
    ddict[x].append(y)
    ddict[y].append(x)
    relation[x-1][1] += 1
    relation[y-1][1] += 1
    
relation.sort(reverse=True, key=lambda x: x[1])

ans = 0
for i, r in relation:
    
    if r >=1:  # 他のカードと関係がある
        if d[i] == 0:  # カードの整数は判明していない
            ans += 1
            d[i] = 1
            que = deque(ddict[i])  # 関係のあるカードの整数が判明
            while que:
                s = que.popleft()
                if d[s] == 0:
                    d[s] = 1
                    que += ddict[s]                    
                else:  # 魔法を使う前に判明済み
                    continue
        else:  #カードの整数は判明済み
            continue
            
    else:  # 他のカードと関係がない
        ans += 1
        
print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
l=[]
for i in range(n-1):
    tmp = list(map(int,input().split()))
    l.append(tmp)
l.sort()

from collections import deque
Q = deque()
Q.append(0)

dis = [0]*n
dis[0]=0

while Q:
    num=Q.popleft()

    for tmp in l:
        if tmp[0]-1 == num:
            nxt=tmp[1]-1
            Q.append(nxt)
            dis[nxt] = dis[num] + tmp[2]

for a in dist:
    print(a%2)

Submission status: Runtime Error

Solution code:
class Tree:
    WHITE = 0
    GRAY = 1
    BLACK = 2

    def __init__(self, adj):
        n = len(adj)
        self.adj = adj
        self.colors = [self.WHITE] * n
        self.depths = [-1] * n
        self.depth = 0

    def dfs(self, u):
        if self.colors[u] == self.BLACK:
            return

        self.colors[u] = self.GRAY
        self.depths[u] = self.depth

        for v, w in self.adj[u]:
            if self.colors[v] == self.WHITE:
                self.depth += w
                self.dfs(v)
                self.depth -= w

        self.colors[u] = self.BLACK


if __name__ == "__main__":
    n = int(input())
    uvw = [list(map(int, input().split())) for _ in range(n - 1)]

    g = [[] for _ in range(n)]

    for u, v, w in uvw:
        u -= 1
        v -= 1
        g[u].append((v, w))
        g[v].append((u, w))

    tree = Tree(g)
    tree.dfs(0)
    ans = [x % 2 for x in tree.depths]
    print(*ans, sep="\n")

Submission status: Runtime Error

Solution code:
N = int(input())
 
link = [[] for _ in range(N)]
for _ in range(N - 1):
    u, v, w = map(int, input().split())
    link[u - 1].append((v - 1, w))
    link[v - 1].append((u - 1, w))
 
d = [-1] * N
d[0] = 0
q = [0]
while q:
  i = q.pop(0)
  for n, w in link[i]:
    if d[n] == -1:
      d[n] = d[i] + w
      q.append(n)
      print(q)
 
for i in d:
  print(i % 2)

Submission status: Runtime Error

Solution code:
# # ABC126D.py, ATC001A.py などを参照

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


# 各頂点までの距離をdfsで求める
depth = [-1] * N
depth[0] = 0


def dfs(edge, s):
    for v in edge[s]:
        if depth[v[0]] == -1:  # 未訪問なら更新する
            depth[v[0]] = depth[s] + v[1]
            dfs(edge, v[0])


dfs(edge, 0)
for d in depth:
    if d % 2 == 0:
        print(0)
    else:
        print(1)

Submission status: Runtime Error

Solution code:
n = int(input())
adj = [[] for _ in range(n)]
for _ in range(n-1):
  u, v, w = map(int, input())
  adj[u-1].append((w, v-1))
  adj[v-1].append((w, u-1))
paint = [None] * n
paint[0] = 0
stack = [0]
while stack:
  u = stack.pop()
  for w, v in adj[u]:
    if paint[v] is None:
      stack.append(v)
      paint[v] = (paint[u] + w) % 2
for p in paint:
  print(p)

Submission status: Runtime Error

Solution code:
from collections import deque

N = int(input())
color = [0]*(N+1)
color_val = -1
finded = set()
codebook = {0:0, 1:1}

for _ in range(N-1):
    u, v, w = map(int, input().split())
    if u in finded and v in finded:
        if color[u] in codebook:
            codebook[color[v]] = (codebook[color[u]] + (w % 2)) % 2
            if color[v] % 2 == 0:
                codebook[color[v] + 1] = (codebook[color[v]] + 1) % 2
            else:
                codebook[color[v] - 1] = (codebook[color[v]] + 1) % 2
        else:
            codebook[color[u]] = (codebook[color[v]] + (w % 2)) % 2
            if color[u] % 2 == 0:
                codebook[color[u] + 1] = (codebook[color[u]] + 1) % 2
            else:
                codebook[color[u] - 1] = (codebook[color[u]] + 1) % 2
    elif u in finded:
        finded.add(v)
        color[v] = (color[u] + (w % 2)) % 2 + color[u]//2 * 2
    elif v in finded:
        finded.add(u)
        color[u] = (color[v] + (w % 2)) % 2 + color[v]//2 * 2
    else:
        finded.add(u)
        finded.add(v)
        color_val += 1
        if w % 2 == 0:
            color[u], color[v] = 2 * color_val, 2 * color_val
        if w % 2 != 0:
            color[u], color[v] = 2 * color_val, 2 * color_val + 1

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

Submission status: Runtime Error

Solution code:
def dfs(v):
    check[v] = True
    for u, w in T[v]:
        if check[u]:
            continue
        dist[u] = dist[v] + w
        dfs(u)

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

dist = [0] * (N + 1)
check = [False] * (N + 1)
dfs(1)

print(*[d % 2 for d in dist[1:]], sep="\n")

Submission status: Runtime Error

Solution code:
class branch:
    def __init__(self, num:int, top:int, long:int):
        self.num = num

        self.top = [top]
        self.long = [long]

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


def make_list(num[0],num[1],num[2]):
    global list
    if list[num[0]]==None:
        list.insert(num[0],branch(1,num[1],num[2]))
    else:
        if my_index(list[num[0]].top)!=-1:
            list[num[0]].num += 1
            list[num[0]].top.append(num[1])
            list[num[0]].long.append(num[2])
            for i in range(list[num[1]].num):
                make_list(num[1],list[num[1]].top[i],num[2]+list[num[1]].top[i])
        else:
            if list[num[0]].long[my_index(list[num[0]].top)] > num[2]:
                list[num[0]].long[my_index(list[num[0]].top)] = num[2]
                for i in range(list[num[1]].num):
                    make_list(num[1],list[num[1]].top[i],num[2]+list[num[1]].top[i])

    if list[num[1]]==None:
        list.insert(num[1],branch(1,num[0],num[2]))
    else:
        list[num[1]].num += 1
        list[num[1]].top.append(num[0])
        list[num[1]].long.append(num[2])
        for i in range(list[num[0]].num):
            make_list(num[0],list[num[0]].top[i],num[2]+list[num[0]].top[i])
    else:
        if list[num[1]].long[my_index(list[num[1]].top)] > num[2]:
            list[num[1]].long[my_index(list[num[1]].top)] = num[2]
            for i in range(list[num[0]].num):
                make_list(num[0],list[num[0]].top[i],num[2]+list[num[0]].top[i])




N = 0
top = []
list = []

N = int(input())
for i in range(N+1):
    top.insert(i,2)

for i in range(N+1):
    list.insert(i,None)


for i in range(N-1):
    Input = input().split()
    num = [(int(Input[0]))]
    num.append(int(Input[1]))
    num.append(int(Input[2]))
    make_list(num[0],num[1],num[2])


for i in range(1,N+1,1):
    if list[i]==None:
        top[i] = 0
    else:
        if top[i] == 2:
            top[i] = 0;
            for j in range(list[i].num):
                if top[list[i].top[j]] != 2:
                    if list[i].long[j] % 2:
                        top[i] = top[list[i].top[j]]^1
                    break

            for j in range(list[i].num):
                if list[i].long[j] % 2:
                    top[list[i].top[j]] = top[i]^1
        else:
            for j in range(list[i].num):
                if list[i].long[j] % 2:
                    top[list[i].top[j]] = top[i]^1
    print(top[i])

Submission status: Runtime Error

Solution code:
from collections import defaultdict
n=int(input())
routes=[tuple(map(int,input().split())) for i in range(n-1)]

edges=defaultdict(list)
for v1,v2,dist in routes:
  edges[v1-1].append([v2-1,dist])
  edges[v2-1].append([v1-1,dist])
#print(edges)

ans=[-1]*n
ans[0]=0

def dfs(i):
  if ans[i]!=-1:
    return ans[i]
  else:
    for edge in edges[i]:
      v=edge[0]
      dist=edge[1]
      ans[i]=dfs(v)+dist
      return ans[i]
for i in range(n):
  dfs(i)
  
print(*[ans[i]%2 for i in range(n)],sep='\n')

Submission status: Runtime Error

Solution code:
def search(tree, now, pre, t, ans):
  nex_li = tree[now]
  for nex, w in nex_li:
    if pre == nex:
      continue
    t1 = t+w
    if t1%2 == 0:
      ans[nex] = 0
    else:
      ans[nex] = 1
    ans = search(tree, nex, now, t1, ans)
  return ans

def run():
  N = int(input())
  tree = {}
  for n in range(N-1):
    u, v, w = map(int, input().split())
    tree.setdefault(u, []).append((v, w%2))
    tree.setdefault(v, []).append((u, w%2))
  ans = [0]*(N+1)
  ans = search(tree, 1, -1, 0, ans)
  for a in ans[1:]:
    print(a)
  
  
if __name__ == '__main__':
    run()

Submission status: Runtime Error

Solution code:
N = int(input())
import sys
sys.setrecursionlimit(10**9)

N = int(input())
uvw = [[int(x) for x in input().split()] for _ in range(N-1)]


def bfs(uvw, status, check, now_p):
    for i in range(N-1):
        if uvw[i][0] == now_p and check[i] == 0:
            check[i] = 1
            status[uvw[i][1] - 1] = status[now_p - 1] + uvw[i][2]
            bfs(uvw, status, check, uvw[i][1])

        if uvw[i][1] == now_p and check[i] == 0:
            check[i] = 1
            status[uvw[i][0] - 1] = status[now_p - 1] + uvw[i][2]
            bfs(uvw, status, check, uvw[i][0])

    return status


#from 1
status = [0] * N
check =  [0] * (N-1)

status = bfs(uvw, status, check, 1)
for arrr in status:
    print(arrr%2)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**10)

N = int(input())
uvw = [[int(x) for x in input().split()] for _ in range(N-1)]


def bfs(uvw, status, check, now_p):
    for i in range(N-1):
        if uvw[i][0] == now_p and check[i] == 0:
            check[i] = 1
            status[uvw[i][1] - 1] = status[now_p - 1] + uvw[i][2]
            bfs(uvw, status, check, uvw[i][1])

        if uvw[i][1] == now_p and check[i] == 0:
            check[i] = 1
            status[uvw[i][0] - 1] = status[now_p - 1] + uvw[i][2]
            bfs(uvw, status, check, uvw[i][0])

    return status


#from 1
status = [0] * N
check =  [0] * (N-1)

status = bfs(uvw, status, check, 1)
for arrr in status:
    print(arrr%2)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**10)

N = int(input())
uvw = [[int(x) for x in input().split()] for _ in range(N-1)]


def bfs(uvw, status, check, now_p):
    for i in range(N-1):
        if uvw[i][0] == now_p and check[i] == 0:
            check[i] = 1
            status[uvw[i][1] - 1] = status[now_p - 1] + uvw[i][2]
            bfs(uvw, status, check, uvw[i][1])

        if uvw[i][1] == now_p and check[i] == 0:
            check[i] = 1
            status[uvw[i][0] - 1] = status[now_p - 1] + uvw[i][2]
            bfs(uvw, status, check, uvw[i][0])

    return status


#from 1
status = [0] * N
check =  [0] * (N-1)

status = bfs(uvw, status, check, 1)

Submission status: Runtime Error

Solution code:
import numpy as np


def calDistance_dfs(aList, d, node):
    for v in aList[node]:
        if d[v[0]] != -1:
            continue
        else:
            d[v[0]] = d[node] + v[1]
            calDistance_dfs(aList, d, v[0])


n = int(input())
adjacencyList = [[] for i in range(n)]
d = [-1] * n

for i in range(n - 1):
    s = list(map(int, input().split()))
    adjacencyList[s[0] - 1].append((s[1] - 1, s[2]))
    adjacencyList[s[1] - 1].append((s[0] - 1, s[2]))


d[0] = 0
calDistance_dfs(adjacencyList, d, 0)
for i in d:
    print(i % 2)

Submission status: Runtime Error

Solution code:
import numpy as np

n = int(input())
c = np.array([[0] * n] * n, dtype=np.uint8)
for i in range(n - 1):
    s = list(map(int, input().split()))
    c[s[0], s[1]] = s[2]
    c[s[1], s[0]] = s[2]



def calDistance_dfs(c, d, start=0):
    d[start] = 0
    stack = []
    stack.append(start)
    while len(stack) != 0:
        current = stack.pop()
        for idx, val in enumerate(c[current, :]):
            if val!=0 and d[idx]==-1:
                stack.append(idx)
                d[idx] = d[current] + val


d = np.array([-1] * n)
calDistance_dfs(c, d, 0)

for i in d:
    print(d%2)

Submission status: Runtime Error

Solution code:
import numpy as np
import sys

sys.setrecursionlimit(5000)

N = int(input())

tree = [[] for i in range(N)]

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


color = - np.ones(N)
color[0] = 0

def dfs(V):
    for p, w in tree[V]:
        if color[p] == -1:
            color[p] = color[V] if w % 2 == 0 else 1 - color[V] 
            dfs(p)

dfs(0)

for c in color:
    print(int(c))

Submission status: Runtime Error

Solution code:
import sys

sys.setrecursionlimit(5000)

N = int(input())

tree = [[] for i in range(N)]

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


color = - np.ones(N)
color[0] = 0

def dfs(V):
    for p, w in tree[V]:
        if color[p] == -1:
            color[p] = color[V] if w % 2 == 0 else 1 - color[V] 
            dfs(p)

dfs(0)

for c in color:
    print(int(c))

Submission status: Runtime Error

Solution code:

sys.setrecursionlimit(5000)

N = int(input())

tree = [[] for i in range(N)]

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


color = - np.ones(N)
color[0] = 0

def dfs(V):
    for p, w in tree[V]:
        if color[p] == -1:
            color[p] = color[V] if w % 2 == 0 else 1 - color[V] 
            dfs(p)

dfs(0)

for c in color:
    print(int(c))

Submission status: Runtime Error

Solution code:
import numpy as np

N = int(input())

tree = [[] for i in range(N)]

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


color = - np.ones(N)
color[0] = 0

def dfs(V):
    for p, w in tree[V]:
        if color[p] == -1:
            color[p] = color[V] if w % 2 == 0 else 1 - color[V] 
            dfs(p)

dfs(0)

for c in color:
    print(int(c))

Submission status: Runtime Error

Solution code:
n = int(input())
edges = [[] for _ in range(n+1)]
colors = [None] * (n+1)

for i in range(n-1):
    u, v, w = [int(x) for x in input().split()]
    if w & 1 == 0:
        edges[u].append((v, 0))
        edges[v].append((u, 0))
    else:
        edges[u].append((v, 1))
        edges[v].append((u, 1))

colors[1] = 0


def color(node, c):
    colors[node] = c
    for i, j in edges[node]:
        if colors[i] is None:
            new_color = (c + j)%2
            color(i, new_color)

color(1,1)

for i in colors[1:]:
    print(i)

Submission status: Runtime Error

Solution code:
n = int(input())
tree = [[] for _ in range(n)]
for _ in range(n-1):
    u, v, w = map(int, input().split())
    tree[u-1].append((v-1, w))
    tree[v-1].append((u-1, w))

ans = [0 for _ in range(n)]

def journey(u, p, bit):
    ans[u] = bit
    for v, w in tree[u]:
        if v == p: continue
        if w % 2 == 0: journey(v, u, bit)
        else: journey(v, u, 1-bit)
    
journey(0, -1, 0)
[print(x) for x in ans]

Submission status: Runtime Error

Solution code:
n = int(input())
tree = [[] for _ in range(n)]
for _ in range(n-1):
    u, v, w = map(int, input().split())
    tree[u-1].append((v-1, w))
    tree[v-1].append((u-1, w))

visited = [False for _ in range(n)]
visited[1] = True
ans = [0 for _ in range(n)]

def journey(next, bit):
    for pos in next:
        v, w = pos
        if visited[v]: continue
        visited[v] = True
        if w % 2 == 0: 
            ans[v] = bit
            journey(tree[v], bit)
        else: 
            ans[v] = 1-bit
            journey(tree[v], 1-bit)
    
journey(tree[1], 0)
for x in ans:
    print(x)

Submission status: Runtime Error

Solution code:
n = int(input())
tree = [[] for _ in range(n)]
for _ in range(n-1):
    u, v, w = map(int, input().split())
    tree[u-1].append((v-1, w))
    tree[v-1].append((u-1, w))

visited = [False for _ in range(n)]
visited[0] = True
ans = [0 for _ in range(n)]

def journey(next, bit):
    for pos in next:
        v, w = pos
        if visited[v]: continue
        visited[v] = True
        if w % 2 == 0: 
            ans[v] = bit
            journey(tree[v], bit)
        else: 
            ans[v] = 1-bit
            journey(tree[v], 1-bit)
    
journey(tree[0], 0)
for x in ans:
    print(x)

Submission status: Runtime Error

Solution code:
n = int(input())
tree = [[] for _ in range(n)]
for _ in range(n-1):
    u, v, w = map(int, input().split())
    tree[u-1].append((v-1, w))
    tree[v-1].append((u-1, w))

visited = [False for _ in range(n)]
visited[0] = True
ans = [0 for _ in range(n)]

def journey(next, bit):
    for pos in next:
        v, w = pos
        if visited[v]: continue
        visited[v] = True
        if w % 2 == 0: 
            ans[v] = bit
            journey(tree[v], bit)
        else: 
            ans[v] = 1-bit
            journey(tree[v], 1-bit)
    
journey(tree[0], 0)
[print(x) for x in ans]

Submission status: Runtime Error

Solution code:
N = int(input())
V = {}
if N == 1:
  print(0)
  exit(0)

# Vertice
for i in range(N-1):
  u,v,w = list(map(int, input().split()))
  u,v = u-1, v-1 #1-index -> 0-index
  if u not in V:
    V[u] = [[v,w]]
  else:
    V[u].append([v,w])
  if v not in V:
    V[v] = [[u,w]]
  else:
    V[v].append([u,w])
     
#print(V)
ANS = [-1] * N
visited = [False] * N

def dfs(pos):
  visited[pos] = True
  for x,w in V[pos]:
    if visited[x]:
      continue
    if w % 2 == 0:
      ANS[x] = ANS[pos]
    else:
      ANS[x] = 1 - ANS[pos]
    #print(pos,x,ANS)
    dfs(x)
  return

ANS[0] = 0
dfs(0)

for a in ANS:
  print(a)

Submission status: Runtime Error

Solution code:
N = int(input())
V = {}

# Vertice
for i in range(N-1):
  u,v,w = list(map(int, input().split()))
  u,v = u-1, v-1 #1-index -> 0-index
  if u not in V:
    V[u] = [[v,w]]
  else:
    V[u].append([v,w])
  if v not in V:
    V[v] = [[u,w]]
  else:
    V[v].append([u,w])
     
#print(V)
ANS = [-1] * N
visited = [False] * N

def dfs(pos):
  visited[pos] = True
  for x,w in V[pos]:
    if visited[x]:
      continue
    if w % 2 == 0:
      ANS[x] = ANS[pos]
    else:
      ANS[x] = 1 - ANS[pos]
    #print(pos,x,ANS)
    dfs(x)
  return

ANS[0] = 0
dfs(0)

for a in ANS:
  print(a)

Submission status: Runtime Error

Solution code:
n = int(input())
tree = [[] for _ in range(n)]
color = [0]*n
for _ in range(n-1):
  a,b,c = map(int,input().split())
  a -= 1
  b -= 1
  tree[a].append((b,c))
  tree[b].append((a,c))
  
  
def dfs(i,before,cost):
  if cost == 1:
    color[i] = 1
  for j in tree[i]:
    if j[0] == before:
      continue
    dfs(j[0],i,(cost + j[1])%2 )
    
dfs(0,-1,0)

for i in range(n):
  print(color[i])