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:
N=int(input())
tree_dict={i:0 for i in range(N)}
tree_map=[[] for i in range(N)]
start=set([i for i in range(N)])

edge=[]

#数字の若いほうから見ていくとする
for i in range(N-1):
    u,v,w = map(int, input().split())
    edge.append(v-1)
    tree_map[u-1].append([v-1,w])

# start = 0
start = (start - set(edge)).pop()

def fill(now_tree, now_color):
    for v,w in now_tree:
        if w%2==0:
            tree_dict[v] = now_color
        else:
            now_color *= -1
            tree_dict[v] = now_color
        fill(tree_map[v], now_color)


now_color=-1
tree_dict[start]=now_color
fill(tree_map[start],now_color)

for i in tree_dict.values():
    if i==-1:
        print(0)
    else:
        print(1)

Submission status: Wrong Answer

Solution code:
#21:30
n = int(input())
peer = [[] for _ in range(n)]
for _ in range(n-1):
  a,b,x = map(int,input().split())
  a -= 1
  b -= 1
  peer[a].append([b,x])
  peer[b].append([a,x])
ans = [-1 for _ in range(n)]
now = [0]
while now != []:
  last = now
  now = []
  for x in last:
    for yz in peer[x]:
      y = yz[0]
      z = yz[1]
      if ans[y] == -1:
        ans[y] = (ans[x] + z) % 2
        now.append(y)
  #print(now)
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
eda = [[0]* n for _ in range(n)]
mini = n
for i in range(n-1):
  x,y,z = map(int,input().split())
  if x < mini:
    mini =x
  eda[x-1][y-1] = z
chouten = [0]*n
chouten[mini] = 0
count = 0
while count < n-1:
  for i in range(n):
    for j in range(i+1,n):
      if eda[i][j]>0 and eda[i][j]%2==0:
        chouten[j] = chouten[i]
        count += 1
      else:
        chouten[j] = abs(chouten[i]-1)
for i in range(n):
  print(chouten[i])

Submission status: Wrong Answer

Solution code:
n = int(input())
eda = [[0]* n for _ in range(n)]
mini = n
for i in range(n-1):
  x,y,z = map(int,input().split())
  if x < mini:
    mini =x
  eda[x-1][y-1] = z
chouten = [0]*n
chouten[mini] = 0
for i in range(n):
  for j in range(i+1,n):
    if eda[i][j]>0 and eda[i][j]%2==0:
      chouten[j] = chouten[i]
    else:
      chouten[j] = abs(chouten[i]-1)
for i in range(n):
  print(chouten[i])

Submission status: Wrong Answer

Solution code:
N=int(input())
l1=[]
l2=[]
for i in range(N-1):
  l1.append(list(map(int,input().split())))
ans=[0]*N
for l in l1:
  if l[2]%2 == 1:
    if l[0] not in l2:
      ans[l[0]]=1
      l2.append(l[0])
    elif l[1] not in l2:
      l2.append(l[1])
for i in range(N):
  print(ans[i])

Submission status: Wrong Answer

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

ans=[0]*n

for i in range(n-1):
    if uvw[i][2]%2==1:
        if ans[uvw[i][0]-1]==0 and ans[uvw[i][1]-1]==0:
            ans[uvw[i][1]-1]=1
for i in range(n):
    print(ans[i])

Submission status: Wrong Answer

Solution code:
from operator import itemgetter

n = int(input())
ee = [list(map(int, input().split())) for _ in range(n-1)]
ee.sort(key=itemgetter(1))
ee.sort(key=itemgetter(0))

ans = [0 for _ in range(n+1)]
sude = set([1])
for u, v, w in ee :
    if v not in sude :
        if w&1 : ans[v] = ans[u]^1
        else : ans[v] = ans[u]
        sude.add(v)
    else :
        if w&1 : ans[u] = ans[v]^1
        else : ans[u] = ans[v]
        sude.add(u)

print(*ans[1:], sep="\n")

Submission status: Wrong Answer

Solution code:
from operator import itemgetter

n = int(input())
ee = [list(map(int, input().split())) for _ in range(n-1)]
ee.sort(key=itemgetter(1))
ee.sort(key=itemgetter(0))

ans = [0 for _ in range(n+1)]
sude = set()
for u, v, w in ee :
    if v not in sude :
        if w&1 : ans[v] = ans[u]^1
        else : ans[v] = ans[u]
        sude.add(v)
    else :
        if w&1 : ans[u] = ans[v]^1
        else : ans[u] = ans[v]
        sude.add(u)

print(*ans[1:], sep="\n")

Submission status: Wrong Answer

Solution code:
n = int(input())

# 木構造の定義
links = [[] for _ in range(n+1)]
for _ in range(n-1):
    u,v,w = map(int, input().split())
    links[u].append([v, w])
    links[v].append([u, w])

# DFS の実行
# w が偶数なら黒色に塗る
ans = [1] * (n+1)
q = [(1, -1)]
while q:
    parent, done = q.pop(-1)
    for child, w in links[parent]:
        
        if child == done:
            continue
            
        if w % 2 == 0:
            ans[child] = ans[parent]
        else:
            ans[child] = ans[parent] - 1
            
            
        q.append((child, parent))
        
for i in ans[1:]:
    print(i)

Submission status: Wrong Answer

Solution code:
n = int(input())

# 木構造の定義
links = [[] for _ in range(n+1)]
for _ in range(n-1):
    u,v,w = map(int, input().split())
    links[u].append([v, w])
    links[v].append([u, w])

# DFS の実行
# w が偶数なら黒色に塗る
ans = [1] * (n+1)
q = [(1, -1)]
while q:
    parent, done = q.pop(-1)
    for child, w in links[parent]:
        
        if child == done:
            continue
            
        if w % 2 == 0:
            ans[child] = ans[parent]
        else:
            ans[child] = ans[parent] - 1
            
            
        q.append((child, parent))
        
print(*ans[1:])

Submission status: Wrong Answer

Solution code:
n = int(input())

# 木構造の定義
links = [[] for _ in range(n+1)]
for _ in range(n-1):
    u,v,w = map(int, input().split())
    links[u].append([v, w])
    links[v].append([u, w])

# DFS の実行
# w が偶数なら黒色に塗る
ans = [0] * (n+1)
q = [(1, -1)]
while q:
    me, parent = q.pop()
    for child, w in links[me]:
        
        if child == parent:
            continue
        if w % 2 ==0:
            ans[me] = 1
            ans[child] = 1
            
        q.append((child, me))
        
print(*ans[1:])

Submission status: Wrong Answer

Solution code:
n = int(input())

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

for i in ans:
    print(i)

Submission status: Wrong Answer

Solution code:
N = int(input().strip())
colours = []
for i in range(N):
    colours.append(0)
uvw = []
for i in range(N-1):
    uvw.append(list(map(int, input().strip().split())))
uvw.sort(key=lambda x: x[0])
for branch in uvw:
    A = colours[branch[0]-1]
    B = colours[branch[1]-1]
    if A == B:  # try changing oddeven and A==B
        if branch[2] % 2 == 1:
            if B == 1:
                colours[branch[1]-1] = 0
            if B == 0:
                colours[branch[1]-1] = 1
        else:
            pass
    else:
        pass

for i in colours:
    print(i)

Submission status: Wrong Answer

Solution code:
from collections import deque

n=int(input())
graph =[[] for _ in range(n)]
colors = [0 for _ in range(n)]

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

for i, ws in enumerate(graph):
  flag_all_odd_dis = True
  for w in ws:
    if w%2 == 0:
      flag_all_odd_dis = False
      break
  if flag_all_odd_dis is True:
    colors[i] = 1
  
for color in colors:
  print(color)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

sys.setrecursionlimit(1000000)

def main(): 

    N=int(input())
    to=[[] for _ in range(N+1)]
    ans=[0]*(N+1)

    for i in range(N-1):
        u,v,w=map(int,input().split())
        print(u,v,w)
        to[u].append([v,w])
        to[v].append([u,w])
    
    def dfs(chil, par):
        for v,l in to[chil]:
            if v == par: continue
            if (ans[chil] == 1 and l%2==0) or (ans[chil] == 0 and l%2==1):
                ans[v]=1
            dfs(v,chil)
    
    #ans[1] = 1
    dfs(1,-1)
    for i in ans[1:]:
        print(i)
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

sys.setrecursionlimit(1000000)

def main(): 

    N=int(input())
    to=[[] for _ in range(N+1)]
    ans=[0]*(N+1)

    for i in range(N-1):
        u,v,w=map(int,input().split())
        print(u,v,w)
        to[u].append([v,w])
        to[v].append([u,w])
    
    def dfs(chil, par):
        for v,l in to[chil]:
            if v == par: continue
            if (ans[chil] == 1 and l%2==0) or (ans[chil] == 0 and l%2==1):
                ans[v]=1
            dfs(v,chil)
    
    ans[1] = 1
    dfs(1,-1)
    for i in ans[1:]:
        print(i)
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque
def main():
    n = int(input())
    ls = [[float("inf")]*(n+1)]*(n+1)
    for _ in range(n-1):
        a,b,c = map(int,input().split())
        ls[a][b] = c
        ls[b][a] = c
    que = deque([1])
    # 調べたか判定
    visited = [0]*(n+1)
    visited[0] = 1
    visited[1] = 1

    ans = [1]*(n+1)
    while len(que)>0:
        now = que.popleft()
        for next_,value in enumerate(ls[now]):
            if value == float("inf"):
                pass
            else:
                if visited[next_]==1:
                    pass
                else:
                    que.append(next_)
                    visited[next_] = 1
                    if value%2 == 0:
                        ans[next_] = ans[now]
                    else:
                        ans[next_] *= -ans[now]
                    
                    
    for i in range(1,n+1):
        if ans[i]==1:
            print(1)
        else:
            print(0)
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque
def main():
    n = int(input())
    ls = [[float("inf")]*(n+1)]*(n+1)
    for _ in range(n-1):
        a,b,c = map(int,input().split())
        ls[a][b] = c
        ls[b][a] = c
    que = deque([1])
    # 調べたか判定
    visited = [0]*(n+1)
    visited[0] = 1
    visited[1] = 1

    ans = [1]*(n+1)
    while len(que)>0:
        now = que.popleft()
        for next_,value in enumerate(ls[now]):
            if value == float("inf"):
                pass
            else:
                if visited[next_]==1:
                    pass
                else:
                    que.append(next_)
                    visited[next_] = 1
                    if value%2 == 0:
                        pass
                    else:
                        ans[next_] *= -1
                    
                    
    for i in range(1,n+1):
        if ans[i]==1:
            print(1)
        else:
            print(0)
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

bw = [2] * N

for i in range(N-1):
    if w[i] % 2 == 1:
        if bw[u[i]-1] == 2 and bw[v[i]-1] == 2:
            bw[u[i]-1] = 0
            bw[v[i]-1] = 1
        elif bw[u[i]-1] != 2 and bw[v[i]-1] == 2:
            bw[v[i]-1] = (bw[u[i]-1] + 1) % 2
        elif bw[u[i]-1] == 2 and bw[v[i]-1] != 2:
            bw[u[i]-1] = (bw[v[i]-1] + 1) % 2
            
for i in range(N):
    if bw[i] == 2:
        bw[i] = 0

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

Submission status: Wrong Answer

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

bw = [0] * N

for i in range(N-1):
    if w[i] % 2 == 1:
        bw[v[i]-1] = (bw[u[i]-1] + 1) % 2

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

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**9)
 
def dfs(now, prev):
    # 今いる頂点から行ける頂点を順に next に入れてループ
    for next, w in g[now]:
        if ans[next] == -1:
            ans[next]=0
            if  w % 2 != 0:
                if ans[prev]==0:
                    ans[next]=1
                    dfs(next,now)
                else:
                    ans[next]=0
                    dfs(next,now)
            else:
                ans[next]=ans[prev]
                dfs(next,now)
    
 
#木構造の取り込み 
n=int(input())
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
ans[0]=0
dfs(0,0)
for i in range(n):
    print(ans[i])

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**9)
 
def dfs(now, prev):
    # 今いる頂点から行ける頂点を順に next に入れてループ
    for next, w in g[now]:
        if ans[next] == -1:
            ans[next]=0
            if  w % 2 != 0:
                if ans[prev]==0:
                    ans[next]=1
                    dfs(next,now)
                else:
                    ans[next]=0
                    dfs(next,now)
            else:
                ans[next]=ans[prev]
                dfs(next,now)
    
 
#木構造の取り込み 
n=int(input())
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

dfs(0,0)
for i in range(n):
    print(ans[i])

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**9)
 
def dfs(now, prev):
    # 今いる頂点から行ける頂点を順に next に入れてループ
    for next, w in g[now]:
        if ans[next] == -1:
            if  w % 2 != 0:
                if ans[prev]==0:
                    ans[next]=1
                    dfs(next,now)
                else:
                    ans[next]=0
                    dfs(next,now)
            else:
                ans[next]=0
                dfs(next,now)
    
 
#木構造の取り込み 
n=int(input())
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

dfs(0,0)
for i in range(n):
    print(ans[i])

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**9)
 
def dfs(now, prev):
    # 今いる頂点から行ける頂点を順に next に入れてループ
    for next, w in g[now]:
        if ans[next] == -1:
            if  w % 2 != 0:
                if ans[prev]==1:
                    ans[next]=0
                    dfs(next,now)
                else:
                    ans[next]=1
                    dfs(next,now)
            else:
                ans[next]=0
                dfs(next,now)
    
 
#木構造の取り込み 
n=int(input())
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

dfs(0,0)
for i in range(n):
    print(ans[i])

Submission status: Wrong Answer

Solution code:

def main():
  n=int(input())
  a=[0]*(n-1)
  c=[2]*(n)
  for x in range(n-1):
    a[x]=list(map(int,input().split()))
    a[x][0],a[x][1]=min(a[x][:2]),max(a[x][:2])
  a=sorted(a,key=lambda x:x[1])
  a=sorted(a)
  for i in range(n-1):
    if c[a[i][0]-1]==c[a[i][1]-1]==2:
      if a[i][2]%2==0:
        c[a[i][0]-1],c[a[i][1]-1]=0,0
      else:
        c[a[i][0]-1],c[a[i][1]-1]=0,1
    elif c[a[i][1]-1]==2:
      if a[i][2]%2==0:
        c[a[i][1]-1]=c[a[i][0]-1]
      else:
        c[a[i][1]-1]=-(c[a[i][0]-1]-1)
    elif c[a[i][0]-1]==2:
      if a[i][2]%2==0:
        c[a[i][0]-1]=c[a[i][1]-1]
      else:
        c[a[i][0]-1]=-(c[a[i][1]-1]-1)
#  print(a)
  for i in range(n):
    print(c[i])   
                
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:

def main():
  n=int(input())
  a=[0]*(n-1)
  c=[2]*(n)
  for x in range(n-1):
    a[x]=list(map(int,input().split()))
  a=sorted(a,key=lambda x:x[1])
  a=sorted(a)
  for i in range(n-1):
    if c[a[i][0]-1]==c[a[i][1]-1]==2:
      if a[i][2]%2==0:
        c[a[i][0]-1],c[a[i][1]-1]=0,0
      else:
        c[a[i][0]-1],c[a[i][1]-1]=0,1
    elif c[a[i][1]-1]==2:
      if a[i][2]%2==0:
        c[a[i][1]-1]=c[a[i][0]-1]
      else:
        c[a[i][1]-1]=-(c[a[i][0]-1]-1)
    elif c[a[i][0]-1]==2:
      if a[i][2]%2==0:
        c[a[i][0]-1]=c[a[i][1]-1]
      else:
        c[a[i][0]-1]=-(c[a[i][1]-1]-1)
#  print(a)
  for i in range(n):
    print(c[i])   
                
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:

def main():
  n=int(input())
  a=[0]*(n-1)
  c=[2]*(n)
  for x in range(n-1):
    a[x]=list(map(int,input().split()))
  a=sorted(a,key=lambda x:x[1])
  a=sorted(a)
  for i in range(n-1):
    if c[a[i][0]-1]==c[a[i][1]-1]==2:
      if a[i][2]%2==0:
        c[a[i][0]-1],c[a[i][1]-1]=0,0
      else:
        c[a[i][0]-1],c[a[i][1]-1]=0,1
    elif c[a[i][1]-1]==2:
      if a[i][2]%2==0:
        c[a[i][1]-1]=c[a[i][0]-1]
      else:
        c[a[i][1]-1]=-(c[a[i][0]-1]-1)
  for i in range(n):
    print(c[i])   
                
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())

colors = [-1]*N
odd = []
even = []

for i in range(N-1):
  u,v,w = map(int, input().split())
  if w%2==0:
    even.append([u-1,v-1])
  else:
    odd.append([u-1,v-1])
    
for u,v in sorted(odd):
  if colors[u]==-1:
    colors[u]=0
    colors[v]=1
  elif colors[u]==0:
    colors[v]=1
  else:
    colors[v]=0

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

Submission status: Wrong Answer

Solution code:
N=int(input())
g=[[] for _ in range(N)]
for n in range(N-1):
    u, v, w = map(int, input().split())
    u, v, w = u - 1, v - 1, w % 2
    g[u].append((v, w))
    g[v].append((u, w))
    rs, visited, que = [0] * N, [False] * N, [0]
    while que:
        c = que.pop(0)
        if visited[c]:continue
        visited[c] = True
        for n, w in g[c]:
            if visited[n]: continue
            rs[n] = (rs[c] + w) % 2
            que.append(n)
    for r in rs:
        print(r)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
# https://atcoder.jp/contests/abc126/tasks/abc126_d

import sys

def dfs(i):
  print(i)
  print(color)
  for v,w in V.get(i, []):
    if (w % 2) == 0:
      if color[v] == -1:
        color[v] = color[i]
      elif color[v] == color[i]:
        continue
      elif color[v] != color[i]:
        return False
    else:
      if color[v] == -1:
        color[v] = 0 if color[i] else 1
      elif color[v] == color[i]:
        return False
      elif color[v] != color[i]:
        continue
    dfs(v)
  return True

if __name__ == '__main__':
  n = int(input())
  V = {} # key:vertex, value:[[vertex opposited, wight]]
  color = {} # 0 or 1 or -1
  for i in range(1, n):
    u,v,w = map(int, input().split())
    if not V.get(u, False):
      V[u] = [[v,w]]
    else:
      V[u].append([v,w])

    if not V.get(v, False):
      V[v] = [[u,w]]
    else:
      V[v].append([u,w])
    # 同じ頂点間で複数の辺はないと仮定
  for i in range(1, n+1):
    color[i] = -1

  print(V)  

  i = 1
  color[i] = 0
  while dfs(i):
    print(color)
    for j in range(1, n+1):
      if color[j] == -1:
        i = j
        color[j] = 0
        break
    for j in range(1, n+1):
      print(color[j])
    sys.exit()
  print('No')

Submission status: Wrong Answer

Solution code:
n = int(input())
T = []
for _ in range(n-1):
    u, v, w = map(int, input().split())
    T += [[u-1, v-1, w%2]]
T.sort()
print(T)
P = list(range(n))
print(P)
for u, v, w in T:
    if w: continue
    while P[u] > u:
        u = P[u]
    while P[v] > u:
        v = P[v]
        P[v] = u
for p in P: print(1 if p else 0)

Submission status: Wrong Answer

Solution code:
n = int(input())
T = []
for _ in range(n-1):
    T += [list(map(int, input().split()))]
T.sort()
P = [0]+[-1]*(n-1)
b = n-1
while b > 0:
    for u, v, w in T:
        pu, pv = P[u-1], P[v-1]
        if pu >= 0:
            P[v-1] = (pu+(w%2))%2
            b -= 1
        if pv >= 0:
            P[u-1] = (pv+(w%2))%2
            b -= 1
for p in P: print(p)

Submission status: Wrong Answer

Solution code:
Data1 = []

def add( node, DATA, first,same, diff):
    for i in range( first, N-1):
        data = DATA[i]
        u,v,w = map( int, data )
        if u == node:
            if w % 2 == 0:
                same.append( v )
                add(v,DATA,first+1,same,diff)
            else:
                diff.append( v )
                add(v,DATA,first+1,same,diff)

def add2( node, DATA, first,same, diff):
    for i in range(first,-1,-1):
        data = DATA[i]
        u,v,w = map( int, data )
        if v == node:
            if w % 2 == 0:
                same.append( u )
                add2(u,DATA,first-1,same,diff)
            else:
                diff.append( u )
                add2(u,DATA,first-1,same,diff)

N = int(input())
for i in range(N-1):
    u,v,w = map(int,input().split())
    Data1.append([u,v,w])
Data1.sort(key=lambda x:x[0])

same = []
diff = []
for i in range(N-1):
    if Data1[i][2] % 2 == 1:
        same = []
        diff = []
        same.append( Data1[i][0])
        diff.append( Data1[i][1])
        if i < N -1:
            add ( Data1[i][0],Data1,i+1,same,diff)
        if i > 0:
            add2( Data1[i][0],Data1,i-1,same,diff)
        break

if len(diff) == 0:
    for i in range(1,N+1):
        print( '0')
else:
    for i in range(1,N+1):
        if i in same:
            print('0')
        else:
            print('1')

Submission status: Wrong Answer

Solution code:
Data1 = []
Data2 = []

def add( node, DATA, first,same, diff):
    for i in range( first, N-1) :
        data = DATA[i]
        u,v,w = map( int, data )
        if u == node:
            if w % 2 == 0:
                same.append( v )
                add(v,DATA,first+1,same,diff)
            else:
                diff.append( v )
                add(v,DATA,first+1,same,diff)
            
N = int(input())
for i in range(N-1):
    u,v,w = map(int,input().split())
    Data1.append([u,v,w])
    Data2.append([v,u,w])
Data1.sort(key=lambda x:x[0])
Data2.sort(key=lambda x:x[0],reverse=True)

same = []
diff = []
for i in range(N-1):
    if Data1[i][2] % 2 == 1:
        same = []
        diff = []
        same.append( Data1[i][0])
        diff.append( Data1[i][1])
        if i < N -1:
            add( Data1[i][0],Data1,i+1,same,diff)
        if i > 0:
            add( Data1[i][0],Data2,i-1,same,diff)
        break

if len(diff) == 0:
    for i in range(1,N+1):
        print( '0')
else:
    for i in range(1,N+1):
        if i in same:
            print('0')
        else:
            print('1')

Submission status: Wrong Answer

Solution code:
Data1 = []
Data2 = []

def add( node, DATA, same, diff):
    for data in DATA:
        u,v,w = map( int, data )
        if u == node:
            if w % 2 == 0:
                same.append( v )
                add(v,DATA,same,diff)
            else:
                diff.append( v )
                add(v,DATA,same,diff)
            
N = int(input())
for i in range(N-1):
    u,v,w = map(int,input().split())
    Data1.append([u,v,w])
    Data2.append([v,u,w])
Data1.sort(key=lambda x:x[0])
Data2.sort(key=lambda x:x[0])

same = []
diff = []
for i in range(N-1):
    if Data1[i][2] % 2 == 1:
        same = []
        diff = []
        same.append( Data1[i][0])
        add( Data1[i][0],Data1,same,diff)
        add( Data1[i][0],Data2,same,diff)
        break

if len(diff) == 0:
    for i in range(N):
        print( '0')
else:
    for i in range(1,N+1):
        if i in same:
            print('0')
        else:
            print('1')

Submission status: Wrong Answer

Solution code:
n = int(input())
T = []
for _ in range(n-1):
    T += [list(map(int, input().split()))]
T.sort()
P = [0]*n
for u, v, w in T:
    P[v-1] = (P[u-1]+w%2)%2
for p in P: print(p)

Submission status: Wrong Answer

Solution code:

import collections
import itertools

def getint(): return int(input())
def getints(): return list(map(int, input().split()))

##* graph
##pre graph
m,lst,nxt,to=0,[],[],[]

def init_graph(node_count, edge_count):
    global lst,nxt,to
    lst = [-1 for _ in range(node_count)]
    nxt = [0  for _ in range(edge_count)]
    to  = [0  for _ in range(edge_count)]

# bidirection
def add_edge(u, v):
    global m
    nxt[m], lst[u], to[m], m = lst[u], m, v, m + 1
    nxt[m], lst[v], to[m], m = lst[v], m, u, m + 1

# bidirection weight
def add_edge(u, v, w):
    global m
    nxt[m], lst[u], to[m], m = lst[u], m, (v, w), m + 1
    nxt[m], lst[v], to[m], m = lst[v], m, (u, w), m + 1

def traverse(u):
    m = lst[u]
    while m != -1:
        yield m
        m = nxt[m]


n = getint()
init_graph(n, 2 * n)

for _ in range(n - 1):
    u, v, w = getints()
    add_edge(u - 1, v - 1, w)

def get_distance(u, dist):
    dist[u] = 0
    stk = [(u, 0)]
    while stk:
        v, d = stk[-1]
        stk.pop()
        for m in traverse(v):
            vv, w = to[m]
            if dist[vv] >= 0:
                continue
            dist[vv] = d + w


dist = [-1]*n
get_distance(0, dist)


for i in range(n):
    print(dist[i] % 2)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

q = deque()
now = 0
for e in E[now]:
    q.append(e)
while q:
    (next, w) = q.pop()
    if ans[next] < 0:
        if w % 2 == 0:
            ans[next] = ans[now]
        else:
            ans[next] = 1 if ans[now] == 0 else 0
        now = next
        for e in E[now]:
            q.append(e)

print(*ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

N = int(input())
E1 = [[] for _ in range(N)]
E2 = [[] for _ in range(N)]
for _ in range(N-1):
    u, v, w = map(int, input().split())
    E1[u-1].append((v-1, w))
    E2[v-1].append((u-1, w))
ans = [-1]*N
ans[0] = 0

q = deque()
now = 0
for e in E1[now]:
    q.append(e)
while q:
    (next, w) = q.pop()
    if ans[next] < 0:
        if w % 2 == 0:
            ans[next] = ans[now]
        else:
            ans[next] = 1 if ans[now] == 0 else 0
    
    for e2 in E2[now]:
        if ans[e2[0]] < 0:
            q.append(e2)
    now = next
    for e in E1[now]:
        q.append(e)

print(*ans)

Submission status: Wrong Answer

Solution code:
from collections import deque


def dfs(start, edges, verts):
    q = deque()
    now = start
    for e in edges[now]:
        q.append(e)
    while q:
        (next, w) = q.pop()
        if verts[next] < 0:
            if w % 2 == 0:
                verts[next] = verts[now]
            else:
                verts[next] = 1 if verts[now] == 0 else 0
        else:
            if w % 2 == 0 and verts[next] != verts[now]:
                return False
            elif w % 2 == 1 and verts[next] == verts[now]:
                return False
            else:
                continue
        now = next
        for e in edges[now]:
            q.append(e)
    return True


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

for i in range(N):
    if ans[i] == -1:
        ans[0] = 0
        dfs(0, E, ans)
print(*ans)

Submission status: Wrong Answer

Solution code:
from collections import deque


def dfs(start, edges, verts):
    q = deque()
    now = start
    for e in edges[now]:
        q.append(e)
    while q:
        (next, w) = q.popleft()
        if verts[next] < 0:
            if w % 2 == 0:
                verts[next] = verts[now]
            else:
                verts[next] = 1 if verts[now] == 0 else 0
        else:
            if w % 2 == 0 and verts[next] != verts[now]:
                return False
            elif w % 2 == 1 and verts[next] == verts[now]:
                return False
            else:
                continue
        now = next
        for e in edges[now]:
            q.append(e)
    return True


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

for i in range(N):
    if ans[i] == -1:
        ans[i] = 0
        dfs(i, E, ans)
print(*ans)

Submission status: Wrong Answer

Solution code:
N = int(input().strip())
colours = []
for i in range(N):
    colours.append(0)
uvw = []
uvw.sort(key=lambda x: x[0])
for i in range(N-1):
    uvw.append(list(map(int, input().strip().split())))
for branch in uvw:
    A = colours[branch[0]-1]
    B = colours[branch[1]-1]
    if A == B:
        if branch[2] % 2 == 1:
            if B == 1:
                colours[branch[1]-1] = 0
            if B == 0:
                colours[branch[1]-1] = 1
        else:
            pass
    else:
        pass

for i in colours:
    print(i)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from collections import deque
n = int(input())

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

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

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

queue = deque([0])

while queue:
  x = queue.popleft()
  for x_, w_ in g[x]:
    if c[x_] == -1:
      if w_%2 == 0:
        c[x_] = c[x]
      else:
        c[x_] = 1-c[x]
      queue.append(x_)
    else:
      continue
print(c)

Submission status: Wrong Answer

Solution code:
n = int(input())
uvw = []
for i in range(n-1):
    uvw.append(list(map(int, input().split(' '))))
result = [0]*(n+1)
result[uvw[0][1]] = uvw[0][2]
s = uvw[0][0]
n_visit = []
for i in range(1, len(uvw)):
    if result[uvw[i][0]] == 0 and result[uvw[i][1]] != 0 and uvw[i][0] != s:
        result[uvw[i][0]] =  result[uvw[i][1]] + uvw[i][2]
    elif result[uvw[i][0]] != 0 and result[uvw[i][1]] == 0 and uvw[i][1] != s:
        result[uvw[i][1]] =  result[uvw[i][0]] + uvw[i][2]
    else:
        if uvw[i][1] != s:
            result[uvw[i][1]] = uvw[i][2]
        else:
            result[uvw[i][0]] = uvw[i][2]

for i in range(1,n+1):
    if result[i]%2 == 0:
        print(0)
    else:
        print(1)