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: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys

sys.setrecursionlimit(10 ** 7)

# 1 ✕ 1 (int)
N = int(input().rstrip())

# N ✕ M (2D array)
A = list(map(int, input().rstrip().split()) for _ in range(N - 1))

items = [[] for _ in range(N)]
colors = [-1 for _ in range(N)]

for i in range(N - 1):
    a, b, w = A[i]
    items[a - 1].append((b - 1, w))
    items[b - 1].append((a - 1, w))


def dfs(v, c):
    colors[v] = c
    for k, w in items[v]:
        cc = colors[k]
        nc = c if w % 2 == 0 else not c
        if cc != -1 and colors[i] != nc:
            return False
        if cc == -1 and not dfs(k, nc):
            return False

    return True


# print(items)

dfs(0, True)
for c in colors:
    print(int(not c))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6)
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N=int(readline())
m=map(int,read().split())
UVW=zip(m,m,m)

graph =[[] for i in range(N+1)]
for u,v,w in UVW:
    graph[u].append((v,w))
    graph[v].append((u,w))
color=[0]*(N+1)

def dfs(cur,par,dist):
    color[cur]=dist
    for dst,w in graph[cur]:
        if (dst==par):
            continue
        dfs(dst,cur,dist^(w&1))
dfs(0,-1,0)
print('\n'.join(map(str,color[1:])))

Submission status: Wrong Answer

Solution code:
N = int(input())
class UnionFind():
    def __init__(self,n):
        self.n=n
        self.parents = [i for i in range(n+1)]
        self.size = [1]*(n+1)
    def find(self,x):
        if self.parents[x]==x:
            return x
        else:
            self.parents[x]=self.find(self.parents[x])
            return self.parents[x]
    def unite(self,x,y):
        xRoot = self.find(x)
        yRoot = self.find(y)
        if xRoot == yRoot:
            return
        if self.size[xRoot]>self.size[yRoot]:
            self.parents[yRoot] = xRoot
        else:
            self.parents[xRoot] = yRoot
            if self.size[xRoot] == self.size[yRoot]:
                self.size[yRoot]+=1
tree = UnionFind(2*N)
for _ in range(N-1):
    u,v,w = map(int,input().split())
    if w%2==0:
        tree.unite(u,v)
    else:
        tree.unite(u,N+v)
        tree.unite(v,N+u)
print(0)
for i in range(2,N+1):
    if tree.find(1)==tree.find(i):
        print(0)
    else:
        print(1)

Submission status: Wrong Answer

Solution code:
N = int(input())
uvw = [list(map(int,input().split())) for i in range(N-1)]

graph = [[] for _ in range(N+1)]
for u,v,w in uvw:
    graph[u].append((v,w))
    graph[v].append((u,w))

ans = [0] * (N+1)

from collections import deque
todo = deque([(1,0)])
seen = [-1] * (N+1)

while todo:
    v,d = todo.pop()
    seen[v] = 0
    if d % 2 == 0:
        ans[v] = 0
    else:
        ans[v] = 1
    for u,w in graph[v]:
        if seen[u]== 0:
            continue
        todo.append((u,w))

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

Submission status: Wrong Answer

Solution code:
N = int(input())
uvw = [list(map(int,input().split())) for i in range(N-1)]

graph = [[] for _ in range(N+1)]
for u,v,w in uvw:
    graph[u].append([v,w%2])
    graph[v].append([u,w%2])

ans = [0] * (N+1)

from collections import deque
todo = deque([1])
seen = [True] + [False] * N
while todo:
    x = todo.pop()
    seen[x] = True
    for y in graph[x]:
        if seen[y[0]]:
            continue
        if y[1] == 0:
            ans[y[0]] = 0
        else:
            ans[y[0]] = 1
        todo.append(y[0])

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

Submission status: Wrong Answer

Solution code:
N = int(input())
uvw = [list(map(int,input().split())) for i in range(N-1)]

graph = [[] for _ in range(N+1)]
for u,v,w in uvw:
    graph[u].append([v,w%2])
    graph[v].append([u,w%2])

ans = [0] * (N+1)

from collections import deque
todo = deque([1])
seen = [False] * (N+1)
while todo:
    x = todo.pop()
    seen[x] = True
    for y in graph[x]:
        if seen[y[0]]:
            continue
        if y[1] == 0:
            ans[y[0]] = 0
        else:
            ans[y[0]] = 1
        todo.append(y[0])

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

Submission status: Wrong Answer

Solution code:
N = int(input())
uvw = [list(map(int,input().split())) for i in range(N-1)]

graph = [[] for _ in range(N+1)]
for u,v,w in uvw:
    graph[u].append([v,w%2])
    graph[v].append([u,w%2])

ans = [0] * (N+1)

from collections import deque
todo = deque([1])
seen = [0] * (N+1)
while todo:
    x = todo.pop()
    seen[x] = 1
    for y in graph[x]:
        if seen[y[0]] == 1:
            continue
        else:
            if y[1] == 0:
                ans[y[0]] = 0
            else:
                ans[y[0]] = 1
            todo.append(y[0])

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

Submission status: Wrong Answer

Solution code:
N = int(input())
uvw = [list(map(int,input().split())) for i in range(N-1)]

graph = [[] for _ in range(N+1)]
for u,v,w in uvw:
    graph[u].append([v,w%2])
    graph[v].append([u,w%2])

ans = [0] * (N+1)

from collections import deque
todo = deque([1])
seen = [0] * (N+1)
while todo:
    x = todo.pop()
    seen[x] = 1
    for y in graph[x]:
        if seen[y[0]] == 1:
            continue
        if y[1] == 0:
            ans[y[0]] = 0
        else:
            ans[y[0]] = 1
        todo.append(y[0])

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

Submission status: Wrong Answer

Solution code:
n    = int(input())
li   = [list(map(int,input().split())) for i in range(n-1)]
clr  = [0]*n
done = [0]*n
tmp  = 0

for i in li:
  if i[0]>i[1]:
    tmp  = i[0]
    i[0] = i[1]
    i[1] = tmp
  i[2] %= 2

li.sort()

for i in li:
  if done[i[0]-1]==0 and done[i[1]-1]==0:
    if i[2]==0:
      clr[i[1]-1] = clr[i[0]-1]
    else:
      clr[i[1]-1] = clr[i[0]-1]^1
    done[i[0]-1] = 1
    done[i[1]-1] = 1
  elif done[i[0]-1]==1 and done[i[1]-1]==0:
    if i[2]==0:
      clr[i[1]-1] = clr[i[0]-1]
    else:
      clr[i[1]-1] = clr[i[0]-1]^1
    done[i[1]-1] = 1
  elif done[i[0]-1]==0 and done[i[1]-1]==1:
    if i[2]==0:
      clr[i[0]-1] = clr[i[1]-1]
    else:
      clr[i[0]-1] = clr[i[1]-1]^1
    done[i[0]-1] = 1
  else:
    continue

for i in clr:
  print(i)

Submission status: Wrong Answer

Solution code:
n    = int(input())
li   = [list(map(int,input().split())) for i in range(n-1)]
clr  = [0]*n
done = [0]*n
tmp  = 0

for i in li:
  if i[0]>i[1]:
    tmp  = i[0]
    i[0] = i[1]
    i[1] = tmp
  i[2] %= 2

li.sort()

for i in li:
  if done[i[0]-1]==0 and done[i[1]-1]==0:
    if i[2]==0:
      clr[i[1]-1] = clr[i[0]-1]
    if i[2]==1:
      clr[i[1]-1] = clr[i[0]-1]^1
    done[i[0]-1] = 1
    done[i[1]-1] = 1
  elif done[i[0]-1]==1 and done[i[1]-1]==0:
    if i[2]==0:
      clr[i[1]-1] = clr[i[0]-1]
    if i[2]==1:
      clr[i[1]-1] = clr[i[0]-1]^1
    done[i[1]-1] = 1
  elif done[i[0]-1]==0 and done[i[1]-1]==1:
    if i[2]==0:
      clr[i[0]-1] = clr[i[1]-1]
    if i[2]==1:
      clr[i[0]-1] = clr[i[1]-1]^1
    done[i[0]-1] = 1
  else:
    continue

for i in clr:
  print(i)

Submission status: Wrong Answer

Solution code:
n    = int(input())
li   = [list(map(int,input().split())) for i in range(n-1)]
clr  = [0]*n
done = [0]*n
tmp  = 0

for i in li:
  if i[0]>i[1]:
    tmp  = i[0]
    i[0] = i[1]
    i[1] = tmp
  i[2] %= 2

li.sort()

for i in li:
  if done[i[0]-1]==0 and done[i[1]-1]==0:
    if i[2]==1:
      clr[i[1]-1] = clr[i[0]-1]^1
    done[i[0]-1] = 1
    done[i[1]-1] = 1
  elif done[i[0]-1]==1 and done[i[1]-1]==0:
    if i[2]==1:
      clr[i[1]-1] = clr[i[0]-1]^1
    done[i[1]-1] = 1
  elif done[i[0]-1]==0 and done[i[1]-1]==1:
    if i[2]==1:
      clr[i[0]-1] = clr[i[1]-1]^1
    done[i[0]-1] = 1
  else:
    continue

for i in clr:
  print(i)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N=int(input())
ki=[[] for f in range(N)]
for i in range(N-1):
 u,v,w = map(int,input().split())
 ki[u-1].append([v-1,w%2])
 ki[v-1].append([u-1,w%2])
stack = [[0,0]]
check = [0]*N #チェック済みリスト
ans=[2]*N
ans[0]=0
while stack != [] :
 a=stack.pop()
 if check[a[0]]==0:
  check[a[0]]=1
  for i in range(len(ki[a[0]])):
   if check[ki[a[0]][i][0]]==0:
    stack.append([ki[a[0]][i][0],(ki[a[0]][i][1]+ans[i])%2])
    ans[ki[a[0]][i][0]]=(ki[a[0]][i][1]+ans[i])%2
for i in ans:
 print(i)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N=int(input())
ki=[[] for f in range(N)]
for i in range(N-1):
 u,v,w = map(int,input().split())
 ki[u-1].append([v-1,w%2])
 ki[v-1].append([u-1,w%2])
stack = [[0,0]]
check = [0]*N #チェック済みリスト
ans=[2]*N
ans[0]=0
while stack != [] :
 a=stack.pop()
 if check[a[0]]==0:
  check[a[0]]=1
  for i in range(len(ki[a[0]])):
   stack.append([ki[a[0]][i][0],(ki[a[0]][i][1]+ans[i])%2])
   ans[ki[a[0]][i][0]]=(ki[a[0]][i][1]+ans[i])%2
for i in ans:
 print(i)

Submission status: Wrong Answer

Solution code:
from collections import deque
N=int(input())
road=[[]for i in range(N)]
dist=[[0]*N for i in range(N)]
for i in range(N-1):
  u1,v1,w1=map(int,input().split())
  road[u1-1].append(v1-1)
  road[v1-1].append(u1-1)
  dist[u1-1][v1-1]=w1
a=[0]*N
houmon=[0]*N
q=deque([])
q.append(0)
while(q):
  v=q.pop()
  houmon[v]=1
  for x in road[v]:
    if(houmon[x]==0):
      q.append(x)
      a[x]=a[v]+dist[v][x]
      houmon[x]=1
for i in range(N):
  if(a[i]%2==0):
    print(0)
  else:
    print(1)

Submission status: Wrong Answer

Solution code:
import sys
N = int(sys.stdin.readline())
uvw = sorted([list(map(int, sys.stdin.readline().split())) for _ in range(N - 1)])

w_list = []
b_list = []

for i in range(N - 1):
    if uvw[i][2] % 2 == 0:
        if uvw[i][0] in w_list:
            if uvw[i][1] not in w_list:
                w_list.append(uvw[i][1])
        elif uvw[i][0] in b_list:
            if uvw[i][1] not in b_list:
                b_list.append(uvw[i][1])
        elif uvw[i][1] in w_list:
            if uvw[i][0] not in w_list:
                w_list.append(uvw[i][0])
        elif uvw[i][1] in b_list:
            if uvw[i][0] not in b_list:
                b_list.append(uvw[i][0])
        else:
            w_list.append(uvw[i][0])
            w_list.append(uvw[i][1])
    else:
        if uvw[i][0] in w_list:
            if uvw[i][1] not in b_list:
                b_list.append(uvw[i][1])
        elif uvw[i][0] in b_list:
            if uvw[i][1] not in w_list:
                w_list.append(uvw[i][1])
        elif uvw[i][1] in w_list:
            if uvw[i][0] not in b_list:
                b_list.append(uvw[i][0])
        elif uvw[i][1] in b_list:
            if uvw[i][0] not in w_list:
                w_list.append(uvw[i][0])
        else:
            w_list.append(uvw[i][0])
            b_list.append(uvw[i][1])
            
for i in range(1, N + 1):
    if i in w_list:
        print(0)
    else:
        print(1)

Submission status: Wrong Answer

Solution code:
import sys
N = int(sys.stdin.readline())
uvw = sorted([list(map(int, sys.stdin.readline().split())) for _ in range(N - 1)])

w_list = []
b_list = []
ans_dic = {}

for i in range(N - 1):
    if uvw[i][2] % 2 == 0:
        if uvw[i][0] in w_list:
            if str(uvw[i][1]) not in ans_dic.keys():
                ans_dic[str(uvw[i][1])] = 0
                w_list.append(uvw[i][1])
        elif uvw[i][0] in b_list:
            if str(uvw[i][1]) not in ans_dic.keys():
                ans_dic[str(uvw[i][1])] = 1
                b_list.append(uvw[i][1])
        elif uvw[i][1] in w_list:
            if str(uvw[i][0]) not in ans_dic.keys():
                ans_dic[str(uvw[i][0])] = 0
                w_list.append(uvw[i][0])
        elif uvw[i][1] in b_list:
            if str(uvw[i][0]) not in ans_dic.keys():
                ans_dic[str(uvw[i][0])] = 1
                b_list.append(uvw[i][0])
        else:
            ans_dic[str(uvw[i][0])] = 0
            ans_dic[str(uvw[i][1])] = 0
            w_list.append(uvw[i][0])
            w_list.append(uvw[i][1])
    else:
        if uvw[i][0] in w_list:
            if str(uvw[i][1]) not in ans_dic.keys():
                ans_dic[str(uvw[i][1])] = 1
                b_list.append(uvw[i][1])
        elif uvw[i][0] in b_list:
            if str(uvw[i][1]) not in ans_dic.keys():
                ans_dic[str(uvw[i][1])] = 0
                w_list.append(uvw[i][1])
        elif uvw[i][1] in w_list:
            if str(uvw[i][0]) not in ans_dic.keys():
                ans_dic[str(uvw[i][0])] = 1
                b_list.append(uvw[i][0])
        elif uvw[i][1] in b_list:
            if str(uvw[i][0]) not in ans_dic.keys():
                ans_dic[str(uvw[i][0])] = 0
                w_list.append(uvw[i][0])
        else:
            ans_dic[str(uvw[i][0])] = 0
            ans_dic[str(uvw[i][1])] = 1
            w_list.append(uvw[i][0])
            b_list.append(uvw[i][1])
            
ans_dic = sorted(ans_dic.items())
for _, v in ans_dic:
    print(v)

Submission status: Wrong Answer

Solution code:
from collections import deque

N = int(input())

A = []
while True:
    try:
        temp=list(map(int,input().split()))
        A.append(temp)
    except:
        break;
#print(A)
To = [[] for i in range(N)]
Cost = [[] for i in range(N)]
for i in range(len(A)):
  #print(A[i])
  To[A[i][0]-1].append(A[i][1]-1)
  To[A[i][1]-1].append(A[i][0]-1)
  Cost[A[i][0]-1].append(A[i][2])
  Cost[A[i][1]-1].append(A[i][2])
#print(To,Cost)

ans = [-1]*N #到達未到達を判定するために-1を初期値として与える。
ans[0] = 0  #0を根にして、0とパリティが一致する場合には0とする。
Queue = deque([])
Queue.append(0) #0を根にする

while Queue:
  v = Queue.popleft() #vは点
  for i in range(len(To[v])): #vから行けるリストつまりToの中にある点の数だけ回す。
    u = To[v][i] #vから行ける点
    w = Cost[v][i] #その点に行くためのコスト
    if ans[u] != -1: #すでに訪れていたらスキップ
      continue
    ans[u] = (ans[v]+w)%2
    Queue.append(u)
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

N = int(input())

A = []
while True:
    try:
        temp=list(map(int,input().split()))
        A.append(temp)
    except:
        break;
#print(A)
To = [[] for i in range(N)]
Cost = [[] for i in range(N)]
for i in range(len(A)):
  print(A[i])
  To[A[i][0]-1].append(A[i][1]-1)
  To[A[i][1]-1].append(A[i][0]-1)
  Cost[A[i][0]-1].append(A[i][2])
  Cost[A[i][1]-1].append(A[i][2])
print(To,Cost)

ans = [-1]*N #到達未到達を判定するために-1を初期値として与える。
ans[0] = 0  #0を根にして、0とパリティが一致する場合には0とする。
Queue = deque([])
Queue.append(0) #0を根にする

while Queue:
  v = Queue.popleft() #vは点
  for i in range(len(To[v])): #vから行けるリストつまりToの中にある点の数だけ回す。
    u = To[v][i] #vから行ける点
    w = Cost[v][i] #その点に行くためのコスト
    if ans[u] != -1: #すでに訪れていたらスキップ
      continue
    ans[u] = (ans[v]+w)%2
    Queue.append(u)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

A = []
while True:
    try:
        temp=list(map(int,input().split()))
        A.append(temp)
    except:
        break;

E = set([])
O = set([])

for i in range(len(A)):
  if A[i][2]%2 ==0:
    E.add(A[i][0])
    E.add(A[i][1])
  else:
    if A[i][0] not in E and A[i][0] not in O:
      if A[i][1] in E:
        O.add(A[i][0])
      elif A[i][1] in O:
        E.add(A[i][0])
      else:
        E.add(A[i][0])
        O.add(A[i][1])
    elif A[i][1] not in E and A[i][1] not in O:
      if A[i][0] in E:
        O.add(A[i][1])
      elif A[i][0] in O:
        E.add(A[i][1])

#print(E,O)
for i in range(N):
  if i+1 in E:
    print("1")
  else:
    print("0")

Submission status: Wrong Answer

Solution code:
N = int(input())
edg = [list(map(int,input().split())) for _ in range(N-1)]
vtx = [[] for _ in range(N)]
for e in edg:
    v,c,w = e
    vtx[v-1].append([c-1,w])

colors = [1 for _ in range(N)]
def dfs(vtx,i=0): # vertex and its parent
    for c,w in vtx[i]:
        colors[c] = 1 - colors[i] if w%2 != 0 else colors[i]
        dfs(vtx,c)
dfs(vtx)

for v in colors:
    print(v)

Submission status: Wrong Answer

Solution code:
N = int(input())
edg = [list(map(int,input().split())) for _ in range(N-1)]
vtx = [[] for _ in range(N)]
for e in edg:
    v,c,w = e
    vtx[v-1].append([c-1,w])

colors = [1 for _ in range(N)]
def dfs(vtx,i=0): # vertex and its parent
    for c,w in vtx[i]:
        if w % 2 != 0:
            colors[c] = abs(colors[i] - 1)
        dfs(vtx,c)
dfs(vtx)

for v in colors:
    print(v)

Submission status: Wrong Answer

Solution code:
N = int(input())
edg = [list(map(int,input().split())) for _ in range(N-1)]
vtx = [[] for _ in range(N)]
for e in edg:
    v,c,w = e
    vtx[v-1].append([c-1,w])

colors = [1 for _ in range(N)]
def dfs(vtx,i=0): # vertex and its parent
    v = vtx[i]
    if len(v) == 0:
        return
    for c,w in v:
        if w % 2 != 0:
            colors[c] = abs(colors[i] - 1)
        dfs(vtx,c)
dfs(vtx)

for v in colors:
    print(v)

Submission status: Wrong Answer

Solution code:
N = int(input())
w = [0] * (N-1)
for i in range(N-1):
    w[i] = [int(i) for i in input().split()]

for i in range(N-1):
    if w[i][2] % 2 == 0:
        print("0")
    else:
        print("1")

Submission status: Wrong Answer

Solution code:
from collections import deque
n=int(input())
v=tuple(tuple(map(lambda x: int(x)-1,input().split())) for _ in range(n-1))

node=[{} for _ in range(n)]
d=deque()
ans= [-1] * n
ans[0]=0
d.append(0)
for pair in v:
    node[pair[0]][pair[1]]=pair[2]
    node[pair[1]][pair[0]]=pair[2]
    
while True:
    if not d:
        break
    i=d.pop()
    for k in node[i].keys():
        if ans[k]!=-1:
          continue
        d.append(k)
        if node[i][k]%2==1:
            ans[k]=ans[i]
        else:
            ans[k]=1-ans[i]
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
node=[-1 for i in range(n)]
def onezero(x,w):
  if w%2==0:
    return node[x]
  else:
    return abs(node[x]-1)
for i in range(n-1):
  u,v,w=map(int,input().split())
  if node[u-1]==-1 and node[v-1]==-1:
    if w%2==0:
      node[u-1]=0
      node[v-1]=0
    else:
      node[u-1]=0
      node[v-1]=1
  elif node[u-1]!=1:
    node[v-1]=onezero(u-1,w)
  else:
    node[u-1]=onezero(v-1,w)
for i in range(n):
  if node[i]==-1:
    node[i]=1
for i in node:
  print(i)

Submission status: Wrong Answer

Solution code:

import sys
sys.setrecursionlimit(10**6)
n = int(input())
tree = [[] for i in range(n)]
for i 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))
vis = [False]*n
color = [0]*n

def dfs_rec(prev,now):
    for node,w in tree[now]:
        if not vis[node]:
            vis[node] = True
            if w%2==0:
                color[node] = color[prev]
            else:
                color[node] = 1-color[prev]
            dfs_rec(now,node)
    return
vis[0] = True
dfs_rec(0,0)
for c in color:
    print(c)

Submission status: Wrong Answer

Solution code:
n = int(input())

li = [0] * n

for i in range(n-1):
    u, v, w = map(int, input().split())
    if w % 2 == 0:
        li[u - 1] += 1
        li[v - 1] += 1

for i in li:
    if i == 0:
        print(1)
    else:
        print(0)

Submission status: Wrong Answer

Solution code:
n=int(input())
l=[]

for i in range(n-1):
    l.append(list(map(int,input().split())))

ll=[1]*n

for j in l:
    if j[2]%2==0:
        ll[j[0]-1]=0
        ll[j[1]-1]=0

for k in ll:
    print(k)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6)
n=int(input())
g=[set() for _ in range(n)]
for _ in range(n-1):
  u,v,w=map(int,input().split())
  g[u-1].add((v-1,w%2))
  g[v-1].add((u-1,w%2))
l=[-1]*n
def dfs(x,p=-1,s=0):
  l[x]=s
  for c,d in g[x]:
    if p==c: continue
    s^=d
    dfs(c,x,s)
dfs(0)
print(*l,sep='\n')

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6)
n=int(input())
g=[set() for _ in range(n)]
for _ in range(n-1):
  u,v,w=map(int,input().split())
  g[u-1].add((v-1,w%2))
  g[v-1].add((u-1,w%2))
l=[-1]*n
def dfs(v,p=-1,s=0):
  l[v]=s
  for c,d in g[v]:
    if p==c: continue
    s^=d
    dfs(c,v,s)
dfs(0)
print(*l,sep='\n')

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6)
n=int(input())
g=[set() for _ in range(n)]
for _ in range(n-1):
  u,v,w=map(int,input().split())
  g[u-1].add((v-1,w%2))
  g[v-1].add((u-1,w%2))
l=[0]+[-1]*(n-1)
def dfs(v,p=-1,s=0):
  for c,d in g[v]:
    if p==c: continue
    s^=d
    l[c]=s
    dfs(c,v,s)
dfs(0)
print(*l,sep='\n')

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6)
n=int(input())
g=[set() for _ in range(n)]
for i in range(n-1):
  u,v,w=map(int,input().split())
  g[u-1].add((v-1,w%2))
  g[v-1].add((u-1,w%2))
l=[0]+[-1]*(n-1)
def dfs(v,p=-1,s=0):
  for c,d in g[v]:
    if l[c]>=0: continue
    s^=d
    l[c]=s
    dfs(c,v,s)
dfs(0)
print(*l,sep='\n')

Submission status: Wrong Answer

Solution code:
import heapq
import sys

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


def dijkstra(edges, n_nodes, start=0):
    dist = [float("inf")] * n_nodes
    dist[start] = 0

    # 優先度キュー
    q = [(dist[start], start)]
    heapq.heapify(q)

    while q:
        d, i = heapq.heappop(q)

        if dist[i] < d:
            continue

        for weight, j in edges[i]:
            tmp = dist[i] + weight
            if dist[j] > tmp:
                dist[j] = tmp
                heapq.heappush(q, (dist[j], j))

    return dist


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

    dist = dijkstra(edges, N)
    print(dist)

    for d in dist:
        if d % 2 == 0:
            print(0)
        else:
            print(1)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque

N = int(input())
ifm = [list(map(int, input().split())) for i in range(N-1)]

tree = [[None]*N for _ in range(N)]
for i in range(N-1):
    s = ifm[i]
    tree[s[0]-1][s[1]-1] = s[2]%2
    tree[s[1]-1][s[0]-1] = s[2]%2
    
ans = [None]*N    
ans[0] = 0

s = deque([])

for i in range(N):
    if tree[0][i] != None and not i in s:
        s.append(i)

bfr = 0

while len(s) != 0:
    nxt = s.pop()
    if ans[nxt] != None:
        continue
    ans[nxt] = 0
    for i in range(N):
        if tree[nxt][i] != None and not i in s:
            s.append(i)
    bfr = nxt

for i in range(N):
    print(ans[i])

Submission status: Wrong Answer

Solution code:
N = int( input() )
d = [ [] for _ in range( N )]

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

s = [( 1, 0 )]
l = [ -1 ] * N
while s != []:
  a, w = s.pop()
  l[ a - 1 ] = w % 2
  for i, j in d[ a - 1 ]:
    if l[ i ] == -1:
      s.append( ( i + 1 , j ) )
for i in l:
  print( i )

Submission status: Wrong Answer

Solution code:
N = int( input() )
path = [[] for _ in range( N + 1 ) ]
for _ in range( N - 1 ):
  v, u, w = map( int, input().split() )
  if w % 2 == 0:
    path[v].append(u)
    path[u].append(v)

team_1 = [1]
team_one = []
while team_1 != []:
  a = team_1.pop()
  if not ( a in team_one ):
    team_one += [ a ]
    team_1 += path[ a ]

for i in range(1, N+1):
  if i in team_one:
    print(0)
  else:
    print(1)

Submission status: Wrong Answer

Solution code:
import sys
import math  # noqa
import bisect  # noqa
import queue  # noqa


def input():
    return sys.stdin.readline().rstrip()


def main():
    N = int(input())
    node = [-1 for _ in range(N)]
    for _ in range(N - 1):
        u, v, w = map(int, input().split())
        u -= 1
        v -= 1
        if w % 2 == 0:
            if (node[u] == -1) and (node[v] == -1):
                node[u] = 0
                node[v] = 0
            elif (node[u] != -1) and (node[v] == -1):
                node[v] = node[u]
            elif (node[u] == -1) and (node[v] != -1):
                node[u] = node[v]
        else:
            if (node[u] == -1) and (node[v] == -1):
                node[u] = 0
                node[v] = 1
            elif (node[u] != -1) and (node[v] == -1):
                node[v] = 1 - node[u]
            elif (node[u] == -1) and (node[v] != -1):
                node[u] = 1 - node[v]
    
    for n in node:
        print(n)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n = int(input())
    ans = [-1 for i in range(n)]
    ans[0]=0
    for i in range(n-1):
        u,v,w = map(int,input().split())
        if ans[u-1]==-1:
            ans[u-1]=0
        if w%2==0:
            ans[v-1]=ans[u-1]
        else:
            ans[v-1]=(ans[u-1]+1)%2
    for i in range(n):
        print(ans[i])

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())
same = []
dif = []

for i in range(1,N):
  u, v, w = map(int, input().split())
  if w % 2 == 0:
    if v in dif:
      dif.append(u)
    elif u in dif:
      dif.append(v)
    else:
      same.append(u)
      same.append(v)
  else:
    if u in same:
      dif.append(v)
    elif v in same:
      dif.append(u)
    else:
      same.append(u)
      dif.append(v)
      
for j in range(1,N+1):
  if j in same:
    print(0)
  else:
    print(1)

Submission status: Wrong Answer

Solution code:
N = int(input())
same = []
dif = []

for i in range(1,N):
  u, v, w = map(int, input().split())
  if w % 2 == 0:
    same.append(u)
    same.append(v)
  else:
    if u in same:
      dif.append(v)
    elif v in same:
      dif.append(u)
    else:
      same.append(u)
      dif.append(v)
      
for j in range(1,N+1):
  if j in same:
    print(0)
  else:
    print(1)

Submission status: Wrong Answer

Solution code:
import sys
import math
import fractions
from collections import deque
from collections import defaultdict
sys.setrecursionlimit(10**7)

N = int(input())
graph = [[] for _ in range(N)]

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

ans = [0] * N


def dfs(cur, prev, isEven):
    if isEven:
        ans[cur] = 1
    else:
        ans[cur] = 0
    for n, d in graph[cur]:
        if n != prev:
            isEven = not (isEven ^ (d % 2 == 0))
            dfs(n, cur, isEven)


dfs(0, -1, True)
for a in ans:
    print(a)

Submission status: Wrong Answer

Solution code:
from collections import deque

n=int(input())
d=[[-1 for j in range(n)] for i in range(n)]
g=[[] for i in range(n)]
color=[-1 for i in range(n)]
color[0]=0
for i in range(n):
  d[i][i]=0

for i in range(n-1):
  u,v,w=map(int,input().split())
  u-=1
  v-=1
  d[u][v]=w
  d[v][u]=w
  g[u].append(v)
  g[v].append(u)

que=deque([0])

while que:
  #print("que",que)
  now=que.popleft()
  for i in g[now]:
    if color[i]!=-1:
      continue
    if d[now][i]%2==0:
      if color[now]==0:
        color[i]=0
      else:
        color[i]=1
    else:
      if color[now]==0:
        color[i]=1
      else:
        color[i]=0
    if color[i]==-1:
      que.append(i)

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

Submission status: Wrong Answer

Solution code:
from collections import deque

n=int(input())
d=[[-1 for j in range(n)] for i in range(n)]
g=[[] for i in range(n)]
color=[-1 for i in range(n)]
color[0]=0
for i in range(n):
  d[i][i]=0

for i in range(n-1):
  u,v,w=map(int,input().split())
  u-=1
  v-=1
  d[u][v]=w
  d[v][u]=w
  g[u].append(v)
  g[v].append(u)

que=deque([0])

while que:
  now=que.popleft()
  for i in g[now]:
    if color[i]!=-1:
      continue
    if d[now][i]%2==0:
      color[i]=0
    else:
      color[i]=1
    que.append(i)

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

Submission status: Wrong Answer

Solution code:
N=int(input())
E=[]
for i in range(N-1):
  u,v,w=map(int,input().split())
  E.append([u,v,w])
par=[0]*(N+1)
dist=[0]*(N+1)
for u,v,w in E:
  if par[u]==0 and par[v]==0:
    par[u]=-1
    par[v]=u
    dist[v]+=w
  elif par[u]==-1:
    par[v]=u
    dist[v]+=w
  elif par[v]==-1:
    par[u]=v
    dist[u]+=w
  elif par[u]==0:
    par[u]=v
    dist[u]+=dist[v]+w
  elif par[v]==0:
    par[v]=u
    dist[v]+=dist[u]+w
for i in range(1,N+1):
  print(dist[i]%2)

Submission status: Wrong Answer

Solution code:
N=int(input())
E=[]
for i in range(N-1):
  u,v,w=map(int,input().split())
  E.append([u,v,w])
par=[0]*(N+1)
dist=[0]*(N+1)
for u,v,w in E:
  if par[u]==0 and par[v]==0:
    par[u]=-1
    par[v]=u
    dist[v]+=w
  elif par[u]==-1:
    par[v]=u
    dist[v]+=w
  elif par[v]==-1:
    par[u]=v
    dist[u]+=w
  elif par[u]==0:
    par[u]=v
    dist[u]+=dist[v]+w
  elif par[v]==0:
    par[v]=u
    dist[v]+=dist[u]+w
for i in range(1,N+1):
  print(dist[i])

Submission status: Wrong Answer

Solution code:
N=int(input())
E=[]
for i in range(N-1):
  u,v,w=map(int,input().split())
  E.append([u,v,w])
par=[0]*(N+1)
dist=[0]*(N+1)
for u,v,w in E:
  if par[u]==0 and par[v]==0:
    par[u]=-1
    par[v]=u
    dist[v]+=w
  elif par[u]==-1:
    par[v]=u
    dist[v]+=w
  elif par[v]==-1:
    par[u]=v
    dist[u]+=w
  elif par[u]==0:
    par[u]=v
    dist[u]+=dist[par[u]]+w
  elif par[v]==0:
    par[v]=u
    dist[v]+=dist[par[v]]+w
for i in range(1,N+1):
  print(dist[i]%2)

Submission status: Wrong Answer

Solution code:
N=int(input())
E=[]
for i in range(N-1):
  u,v,w=map(int,input().split())
  E.append([u,v,w%2])
par=[0]*(N+1)
dist=[0]*(N+1)
for u,v,w in E:
  if par[u]==0 and par[v]==0:
    par[u]=-1
    par[v]=u
    dist[v]^=w
  elif par[u]==-1:
    par[v]=u
    dist[v]^=w
  elif par[v]==-1:
    par[u]=v
    dist[u]^=w
  elif par[u]==0:
    par[u]=v
    dist[u]^=dist[par[u]]^w
  elif par[v]==0:
    par[v]=u
    dist[v]^=dist[par[v]]^w
for i in range(1,N+1):
  print(dist[i])

Submission status: Wrong Answer

Solution code:
N=int(input())
A=[0]*N
for i in range(1,N):
    l=list(map(int,input().split()))
    if l[2]%2==0:
        A[l[1]-1]=A[l[0]-1]
    else:
        A[l[1]-1]=(A[l[0]-1]+1)%2
for i in A:
    print(i)

Submission status: Wrong Answer

Solution code:
n = int(input())
# n, k = list(map(int, input().split()))
# r = list(map(int, input().split()))
u = []
v = []
w = []
for i in range(n-1):
    x, y, z = [int(j) for j in input().split()]
    u.append(x)
    v.append(y)
    w.append(z)

ans = [0]*n
check = [0]*n

for i in range(n-1):
    if w[i] % 2 == 0:
        check[u[i]-1] += 1
        check[v[i]-1] += 1
    else:
        if ans[u[i]-1] == ans[v[i]-1]:
            if check[u[i]-1] == 0:
                ans[u[i]-1] = 1
                check[u[i]-1] += 1
            else:
                ans[v[i]-1] = 1
                check[u[i]-1] += 1

for i in ans:
    print(i)