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:
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations,permutations,accumulate, product # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict,Counter
import decimal
import re
import math
import bisect
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#
# my_round_int = lambda x:np.round((x*2 + 1)//2)
# 四捨五入g
#
# インデックス系
# int min_y = max(0, i - 2), max_y = min(h - 1, i + 2);
# int min_x = max(0, j - 2), max_x = min(w - 1, j + 2);
#
#
import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
#mod = 998244353
from sys import stdin
readline = stdin.readline
def readInts():
  return list(map(int,readline().split()))
def readTuples():
    return tuple(map(int,readline().split()))
def I():
  return int(readline())
n = I()
# UVW = [tuple(map(int,input().split())) for _ in range(n-1)]
G = [[] for _ in range(n)]
for _ in range(n-1):
    u,v,w = readInts()
    u-=1;v-=1;
    G[u].append((v,w))
    G[v].append((u,w))
# 初期化
ans = [-1] * n
ans[0] = 0
if n == 3:
    print('^p^')
    exit()
def dfs(v, p = -1):
    for to,d in G[v]:
        if to == p:
            continue
        if d%2: #距離が奇数なら、色を異なるものにする必要がある
            ans[to] = 1 - ans[v]
        else: # 距離が奇数なら、同じ色でいい。同じだもの
            ans[to] = ans[v]
        # こっからまだやらないと駄目でしょ
        dfs(to,v)
dfs(0)
print(*ans,sep='\n')

Submission status: Wrong Answer

Solution code:
N = int(input())

d = [[] for _ in range(N)]

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

dist = [0] * N
for i in range(1,N):
  dist[i] = d[i-1][-1][1] + dist[i-1]

#print(dist)

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

Submission status: Wrong Answer

Solution code:
from collections import deque

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=[-1]*N
ans[0]=0
d=deque([0])
done=[0]
while d:
  p=deque.popleft(d)
  color=ans[p]
  print(p,color,tree[p])
  for v,w in tree[p]:
    if not v in done:
      deque.append(d,v)
      done.append(v)
      ans[v]=color^(w%2)
print(*ans,sep='\n')

Submission status: Wrong Answer

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

d = [0]*n

for i in range(n-1):
  if data[i][2]%2==0:
    continue
  else:
    d[data[i][0]-1] += 1
    d[data[i][1]-1] += 1
    
for i in d:
  if i != 0:
    print(1)
  else:
    print(0)

Submission status: Wrong Answer

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

d = [0]*n

for i in range(n-1):
  if data[i][2]%2==0:
    continue
  else:
    d[data[i][0]-1] += 1
    d[data[i][1]-1] += 1
    
for i in d:
  print(i)

Submission status: Wrong Answer

Solution code:
A=int(input())
for i in range(A):
   print(0)

Submission status: Wrong Answer

Solution code:
n = int(input())
c = [None] * (n + 1)
pp = []
for i in range(1, n):
    u, v, w = tuple(map(int, input().split()))
    pp.append((min(u, v), max(u, v), w))
pp = sorted(pp)

for u, v, w in pp:
    if w % 2 == 0:
        if c[u] is None and c[v] is None:
            c[u] = 0
            c[v] = 0
        elif c[u] is not None:
            c[v] = c[u]
        elif c[v] is not None:
            c[u] = c[v]
    else:
        if c[u] is None and c[v] is None:
            c[u] = 0
            c[v] = 1
        elif c[u] is not None:
            c[v] = 1 - c[u]
        elif c[v] is not None:
            c[u] = 1 - c[v]

for i in range(1, n + 1):
    if c[i] is None:
        print(0)
    else:
        print(c[i])

Submission status: Wrong Answer

Solution code:
n = int(input())
c = [None] * (n + 1)
pp = []
for i in range(1, n):
    p = tuple(map(int, input().split()))
    pp.append(p)
pp = sorted(pp)

for u, v, w in pp:
    if w % 2 == 0:
        if c[u] is None and c[v] is None:
            c[u] = 0
            c[v] = 0
        elif c[u] is not None:
            c[v] = c[u]
        elif c[v] is not None:
            c[u] = c[v]
    else:
        if c[u] is None and c[v] is None:
            c[u] = 0
            c[v] = 1
        elif c[u] is not None:
            c[v] = 1 - c[u]
        elif c[v] is not None:
            c[u] = 1 - c[v]

for i in range(1, n + 1):
    if c[i] is None:
        print(0)
    else:
        print(c[i])

Submission status: Wrong Answer

Solution code:
n = int(input())
c = [None] * (n + 1)
for i in range(1, n):
    u, v, w = map(int, input().split())
    if w % 2 == 0:
        if c[u] is None and c[v] is None:
            c[u] = 0
            c[v] = 0
        elif c[u] is not None:
            c[v] = c[u]
        elif c[v] is not None:
            c[u] = c[v]
    else:
        if c[u] is None and c[v] is None:
            c[u] = 0
            c[v] = 1
        elif c[u] is not None:
            c[v] = 1 - c[u]
        elif c[v] is not None:
            c[u] = 1 - c[v]

for i in range(1, n + 1):
    if c[i] is None:
        print(0)
    else:
        print(c[i])

Submission status: Wrong Answer

Solution code:
n = int(input())
c = [None] * (n + 1)
for i in range(1, n):
    u, v, w = map(int, input().split())
    if w % 2 == 0:
        if c[u] is None and c[v] is None:
            c[u] = 0
            c[v] = 0
        elif c[u] is not None:
            c[v] = c[u]
        elif c[v] is not None:
            c[u] = c[v]
    else:
        if c[u] is None and c[v] is None:
            c[u] = 0
            c[v] = 1
        elif c[u] is not None:
            c[v] = 1 - c[u]
        elif c[v] is not None:
            c[u] = 1 - c[v]

for i in range(1, n + 1):
    print(c[i])

Submission status: Wrong Answer

Solution code:
N = int(input())
dots = [tuple(map(int, input().split())) for _ in range(N-1)]
memo = [0] * (N + 1)

for dot in dots:
    u, v, w = dot
    if memo[u] == 0 and memo[v] == 0:
        if w % 2 == 0:
            memo[u] = 1
            memo[v] = 1
        else:
            memo[u] = 1
            memo[v] = -1
    elif memo[u] == 0 and memo[v] != 0:
        if w % 2 == 0:
            memo[u] = memo[v]
        else:
            memo[u] = memo[v] * -1
    elif memo[u] != 0 and memo[v] == 0:
        if w % 2 == 0:
            memo[v] = memo[u]
        else:
            memo[v] = memo[u] * -1
    else:
        continue

for i in range(1, N+1):
    if memo[i] == -1:
        print(0)
    else:
        print(1)

Submission status: Wrong Answer

Solution code:
n=int(input())

#隣接リスト表現
es=[[] for i in range(n)]
for i in range(n-1):
  u,v,w=list(map(int,input().split()))
  es[u-1].append([v-1,w])
  es[v-1].append([u-1,w])
  
# 再帰の深さが1000を超えそうなときはこれをやっておく
import sys
sys.setrecursionlimit(10**7)

#頂点vから初めて白なら１、黒なら−１、未探索なら０
colors=[0 for i in range(n)]
def search(v,color):
  colors[v]=color
  #vから行ける辺について考える
  for to in es[v]:
    if colors[to[0]]==0 and to[1]%2==0:
      search(to[0],color)
    elif colors[to[0]]==0 and to[1]%2==1:
      search(to[0],-color)
  return colors


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

Submission status: Wrong Answer

Solution code:
n=int(input())

#隣接リスト表現
es=[[] for i in range(n)]
for i in range(n-1):
  u,v,w=list(map(int,input().split()))
  es[u-1].append([v-1,w])

# 再帰の深さが1000を超えそうなときはこれをやっておく
import sys
sys.setrecursionlimit(10**7)

#頂点vから初めて白なら１、黒なら−１、未探索なら０
colors=[0 for i in range(n)]
def search(v,color):
  colors[v]=color
  #vから行ける辺について考える
  for to in es[v]:
    if colors[to[0]]==0 and to[1]%2==0:
      search(to[0],color)
    elif colors[to[0]]==0 and to[1]%2==1:
      search(to[0],-color)
  return colors

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

Submission status: Wrong Answer

Solution code:
N = int(input())
data = [[] for _ in range(N)]
for _ in range(N-1):
    p, q, w = map(int, input().split())
    p -= 1
    q -= 1
    data[p].append((q,w))

colors = [0]*N
visited = [0]*N
start = 0
next_set = [start]
while next_set:
    p = next_set.pop()
    visited[p] = 1
    color = colors[p]
    for q, w in data[p]:
        if visited[q]:
            continue
        colors[q] = (color + w%2)%2
        next_set.append(q)
print(*colors, sep="\n")

Submission status: Wrong Answer

Solution code:
N = int(input())
data = [[] for _ in range(N)]
for _ in range(N-1):
    p, q, w = map(int, input().split())
    p -= 1
    q -= 1
    data[p].append((q,w))

colors = [0]*N
visited = [0]*N
start = 0
next_set = [start]
while next_set:
    p = next_set.pop()
    visited[p] = 1
    color = colors[p]
    for q, w in data[p]:
        if visited[q]:
            continue
        colors[q] = (color + w)%2
        next_set.append(q)
print(*colors, sep="\n")

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()


def main():
    n = int(input())
    graph = []
    for _ in range(n-1):
        graph.append(list(map(int, input().split())))
    graph.sort(key=lambda x: x[2]%2)
    uf = UnionFind(n+1)
    ans = [0]*(n+1)
    for node in graph:
        if node[2]%2 == 0:
            uf.union(node[0], node[1])
        else:
            if ans[uf.find(node[0])] == ans[uf.find(node[1])]:
                for mem in uf.members(node[1]):
                    ans[mem] = -ans[mem]+1
    for anss in ans[1:]:
        print(anss)
    #print(graph)


class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()


def main():
    n = int(input())
    graph = []
    for _ in range(n-1):
        graph.append(list(map(int, input().split())))
    graph.sort(key=lambda x: x[2]%2)
    uf = UnionFind(n+1)
    ans = [0]*(n+1)
    for node in graph:
        if node[2]%2 == 0:
            uf.union(node[0], node[1])
        else:
            if ans[uf.find(node[0])] == ans[uf.find(node[1])]:
                for mem in uf.members(node[1]):
                    ans[mem] = -ans[mem]+1
    for anss in ans[1:]:
        print(anss)
    print(graph)


class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()


def main():
    n = int(input())
    graph = []
    for _ in range(n-1):
        graph.append(list(map(int, input().split())))
    graph.sort(key=lambda x: (x[2]+1)//2)
    uf = UnionFind(n+1)
    ans = [0]*(n+1)
    for node in graph:
        if node[2]%2 == 0:
            uf.union(node[0], node[1])
        else:
            if ans[uf.find(node[0])] == ans[uf.find(node[1])]:
                for mem in uf.members(node[1]):
                    ans[mem] = -ans[mem]+1
    for anss in ans[1:]:
        print(anss)
    #print(graph)


class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N=int(input())
l=[0]*N
com_l=[[] for i in range(N)]
for i in range(N-1):
    X,Y,Z=list(map(int,input().split()))
    com_l[X-1].append((Y-1,Z%2))
for i in range(N-1):
    if com_l[i] != []:
        if l[i] == 0:
            for j,k in com_l[i]:
                if k ==1:
                    l[j]=1
        else:
            for j,k in com_l[i]:
                if k==0:
                    l[j]=1
print(*l,sep="\n")

Submission status: Wrong Answer

Solution code:
N=int(input())
l=[0]*N
com_l=[[] for i in range(N)]
for i in range(N-1):
    X,Y,Z=list(map(int,input().split()))
    com_l[X-1].append((Y-1,Z%2))
for i in range(N-1):
    if com_l[i] != []:
        if l[i] == 0:
            for j,k in com_l[i]:
                if k ==1:
                    l[j]=1
        else:
            for j,k in com_l[i]:
                if k==0:
                    l[j]=1
print(l)

Submission status: Wrong Answer

Solution code:
N = int(input())
G = [{} for _ in range(N)]
results = ['0' for _ in range(N)]

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

for i, adj in enumerate(G):
    for key, val in adj.items():
        if val % 2 == 1:
            if results[i] == '0':
                results[key] = '1'
            else:
                results[key] = '0'
        else:
            results[key] = results[i]

for result in results:
    print(result)

Submission status: Wrong Answer

Solution code:
N = int(input())
G = [{} for _ in range(N)]
results = ['0' for _ in range(N)]

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

for i, adj in enumerate(G):
    for key, val in adj.items():
        if val % 2 == 0:
            continue

        if results[i] == '0':
            results[key] = '1'
        else:
            results[key] = '0'

for result in results:
    print(result)

Submission status: Wrong Answer

Solution code:
N = int(input())
u, v, w = [0]*(N-1), [0]*(N-1), [0]*(N-1)
n = [-1] * N

for i in range(N-1):
    u[i], v[i], w[i] = list(map(int, input().split()))
    if n[u[i]-1] != -1:
        n[v[i]-1] = (-(n[u[i]-1]*2 - 1) * (w[i]%2) + 1)//2
    elif n[v[i]-1] != -1:
        n[u[i]-1] = (-(n[v[i]-1]*2 - 1) * (w[i]%2) + 1)//2
    else:
        n[u[i]-1] = n[v[i]-1] = 0

for i in n:
    print(i)

Submission status: Wrong Answer

Solution code:
N = int(input())
dist_from_1 = [0 for i in range(N+1)]

paths = [dict() for i in range(N+1)]
for i in range(N-1):
    a, b, dist = map(int, input().split())
    paths[a][b] = dist
    paths[b][a] = dist

queue = [(0, 1, 0)]
while len(queue) != 0:
    prev, now, dist = queue.pop()
    dist_from_1[now] = dist
    for next in paths[now]:
        if next == prev:
            continue
        queue.append((now, next, dist + paths[now][next]))

print(dist_from_1)

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

Submission status: Wrong Answer

Solution code:
N = int(input())
dist_from_1 = [0 for i in range(N+1)]

paths = [dict() for i in range(N+1)]
for i in range(N-1):
    a, b, dist = map(int, input().split())
    paths[a][b] = dist
    paths[b][a] = dist

queue = [(0, 1, 0)]
while len(queue) != 0:
    prev, now, dist = queue.pop()
    dist_from_1[now] = dist
    for next in paths[now]:
        if next == prev:
            continue
        queue.append((now, next, paths[now][next]))

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

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10000000)
n=int(input())
path=dict()
for i in range(n-1):
  u,v,w=map(int,input().split())
  if u-1 in path:
    path[u-1].append([v-1,w])
  else:
    path[u-1] = [[v-1,w]]
  if v-1 in path:
    path[v-1].append([u-1,w])
  else:
    path[v-1] = [[u-1,w]]

color = [-1 for i in range(n)]
def dfs(bef,pos,v):
  if color[pos]>-1:
    return
  if v%2:
    color[pos] = 0 if bef else 1
  else:
    color[pos] = bef
  for nex, dist in path[pos]:
    dfs(color[pos],nex,v+dist)
  return

init=-1
for i in range(n):
  if len(path[i]) == 1:
    init = i
    break
dfs(0,init,0)
for i in range(n):
  print(color[i])

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10000000)
n=int(input())
path=dict()
for i in range(n-1):
  u,v,w=map(int,input().split())
  if u-1 in path:
    path[u-1].append([v-1,w])
  else:
    path[u-1] = [[v-1,w]]
  if v-1 in path:
    path[v-1].append([u-1,w])
  else:
    path[v-1] = [[u-1,w]]

color = [-1 for i in range(n)]
def dfs(bef,pos,v):
  if color[pos]>-1:
    return
  if v:
    color[pos] = 0 if bef else 1
  else:
    color[pos] = bef
  for nex, dist in path[pos]:
    dfs(color[pos],nex,(v+dist)%2)
  return
    
dfs(0,0,0)
for i in range(n):
  print(color[i])

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10000000)
n=int(input())
path=dict()
for i in range(n-1):
  u,v,w=map(int,input().split())
  if u-1 in path:
    path[u-1].append([v-1,w])
  else:
    path[u-1] = [[v-1,w]]
  if v-1 in path:
    path[v-1].append([u-1,w])
  else:
    path[v-1] = [[u-1,w]]

color = [-1 for i in range(n)]
def dfs(bef,pos,v):
  if color[pos]>-1:
    return
  color[pos] = bef ^ v%2
  #print(pos,color[pos])
  for nex, dist in path[pos]:
    dfs(color[pos],nex,v+dist)
  return
    
dfs(0,0,0)
for i in range(n):
  print(color[i])

Submission status: Wrong Answer

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

N=int(input().rstrip('\n'))
d = {x:0 for x in range(1,N+1)}
bs=[]
bd=[]
for _ in range(N-1):
  v1,v2,w = (int(x) for x in input().rstrip('\n').split())
  if w % 2 == 1:
    if v1<v2:
      d[v2] = (d[v1]+1)%2
    else:
      d[v1] = (d[v2]+1)%2
  else:
    if v1<v2:
      d[v2] = d[v1]
    else:
      d[v1] = d[v2]
for i in d.values():
  print(i)

Submission status: Wrong Answer

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

N=int(input().rstrip('\n'))
d = {x:0 for x in range(1,N+1)}
bs=[]
bd=[]
for _ in range(N-1):
  v1,v2,w = (int(x) for x in input().rstrip('\n').split())
  if w % 2 == 1:
    d[v2] = (d[v1]+1)%2
for i in d.values():
  print(i)

Submission status: Wrong Answer

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

N=int(input().rstrip('\n'))
d = {1:0}
bs=[]
bd=[]
for _ in range(N-1):
  v1,v2,w = (int(x) for x in input().rstrip('\n').split())
  if w % 2 == 0:
    bs.append((v1,v2))
  else:
    bd.append((v1,v2))
hit = [1]
for _ in range(1,N+1):
  if len(hit) == 0:
    break
  for i in hit:
    nh = []
    xs = [x for x in bs if i in x]
    xd = [x for x in bd if i in x]
    if len(xs)>0:
      for x in xs:
        a = x[0]
        b = x[1]
        if i == a:
          d[b] = d[a]
          nh.append(b)
        else:
          d[a] = d[b]
          nh.append(a)
        bs.remove(x)
    if len(xd)>0:
      for x in xd:
        a = x[0]
        b = x[1]
        if i ==a:
          d[b] = (d[a]+1)%2
          nh.append(b)
        else:
          d[a] = (d[b]+1)%2
          nh.append(a)
        bd.remove(x)        
    hit = nh
for i in d.values():
  print(i)

Submission status: Wrong Answer

Solution code:
n = int(input())

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

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

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/abc126/tasks/abc126_d

# ｄfsでやっていき、最初の点を黒か白かで固定する。偶数ならどんどん加えていく。
n = int(input())
tree = [[] for _ in range(n + 1)]
for _ in range(n - 1):
    u, v, w = map(int, input().split())
    u -= 1
    v -= 1
    tree[u].append((v, w))
    tree[v].append((u, w))


def dfs(tree, n):
    visited = [False] * n
    dis = [0] * n
    even = set([0])
    stack = [0]
    while stack:
        v = stack.pop()
        cur_dis = dis[v]
        for next_v, d in tree[v]:
            if not visited[next_v]:
                visited[next_v] = True
                if (cur_dis + d) % 2 == 0:
                    even.add(next_v)
                    stack.append(next_v)
                    dis[next_v] = cur_dis + d
    return even

even = dfs(tree, n)
for i in range(n):
    if i in even:
        print(0)
    else:
        print(1)

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/abc126/tasks/abc126_d

# ｄfsでやっていき、最初の点を黒か白かで固定する。偶数ならどんどん加えていく。
n = int(input())
tree = [[] for _ in range(n + 1)]
for _ in range(n - 1):
    u, v, w = map(int, input().split())
    u -= 1
    v -= 1
    tree[u].append((v, w))
    tree[v].append((u, w))


def dfs(tree, n):
    visited = [False] * n
    dis = [0] * n
    even = set([0])
    stack = [0]
    while stack:
        v = stack.pop()
        cur_dis = dis[v]
        for next_v, d in tree[v]:
            if not visited[v]:
                visited[v] = True
                if (cur_dis + d) % 2 == 0:
                    even.add(next_v)
                    stack.append(next_v)
                    dis[next_v] = cur_dis + d
    return even

even = dfs(tree, n)
for i in range(n):
    if i in even:
        print(0)
    else:
        print(1)

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/abc126/tasks/abc126_d

# ｄfsでやっていき、最初の点を黒か白かで固定する。偶数ならどんどん加えていく。
n = int(input())
tree = [[] for _ in range(n + 1)]
for _ in range(n - 1):
    u, v, w = map(int, input().split())
    u -= 1
    v -= 1
    tree[u].append((v, w))
    tree[v].append((u, w))


def dfs(tree, n):
    visited = [False] * n
    dis = [0] * n
    even = set([0])
    stack = [0]
    while stack:
        v = stack.pop()
        cur_dis = dis[v]
        for next_v, d in tree[v]:
            if not visited[v]:
                visited[v] = True
                stack.append(next_v)
                dis[next_v] = cur_dis + d
                if (cur_dis + d) % 2 == 0:
                    even.add(next_v)
    return even

even = dfs(tree, n)
for i in range(n):
    if i in even:
        print(0)
    else:
        print(1)

Submission status: Wrong Answer

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

q.sort()

ans=[0 for i in range(n+1)]

for que in q:
    ans[que[1]]=ans[que[0]]^que[2]
    
print("\n".join([str(c) for c in ans[1:]]))

Submission status: Wrong Answer

Solution code:
import math
#N = int(input())
#S = str(input())
#A, B = map(int, input().split())
#C = list(map(int, input().split()))

N = int(input())
ans = [0] * N
root = [0] * (N - 1)
for i in range(N - 1):
  u, v, w = map(int, input().split())
  if u > v:
    u, v = v, u
  root[i] = [u - 1, v - 1, w]
root = sorted(root)
#print(root)

for i in range(N - 1):
  now = root[i]
  if now[2] % 2 == 0:
    ans[now[1]] = ans[now[0]]
  else:
    ans[now[1]] = abs(1 - ans[now[0]])

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

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10**7)


def dfs(s, to, color, distance=0):
    if distance % 2 == 0:
        color[s] = 0
    else:
        color[s] = 1

    for next_v, step in to[s]:
        if color[next_v] == -1:
            distance += step
            dfs(next_v, to, color, distance)


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

    color = [-1] * N #-1: 未探索、0: 白、1: 黒
    dfs(0, to, color)

    print(*color, sep='\n')


if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10**7)


def dfs(s, to, color, distance=0):
    for next_v, step in to[s]:
        if color[next_v] == -1:
            distance += step
            if distance % 2 == 0:
                color[next_v] = 0
            else:
                color[next_v] = 1
            dfs(next_v, to, color, distance)


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

    color = [-1] * N #-1: 未探索、0: 白、1: 黒
    color[0] = 0
    dfs(0, to, color)

    print(*color, sep='\n')


if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
n = int(input())
a = []
b = [-1]*(n+1)
for _ in range(n-1):
    u, v, w = map(int, input().split())
    if b[u] == -1 and b[v] == -1:
        if w%2 == 0:
            a.append([u, v])
            b[u] = len(a)-1
            b[v] = len(a)-1
        else:
            a.append([u])
            b[u] = len(a)-1
            a.append([v])
            b[v] = len(a)-1
    elif b[u] == -1:
        if w%2 == 0:
            a[b[v]].append(u)
            b[u] = b[v]
        else:
            a.append([u])
            b[u] = len(a)-1
    elif b[v] == -1:
        if w%2 == 0:
            a[b[u]].append(v)
            b[v] = b[u]
        else:
            a.append([v])
            b[v] = len(a)-1
    else:
        if w%2 == 0:
            if len(a[b[u]]) < len(a[b[v]]):
                for i in a[b[u]]:
                    a[b[v]].append(i)
                    b[i] = b[v]
            else:
                for i in a[b[v]]:
                    a[b[u]].append(i)
                    b[i] = b[u]
for i in range(n):
    if b[i+1] == b[1]:
        print(0)
    else:
        print(1)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = []
b = [-1]*(n+1)
for _ in range(n-1):
    u, v, w = map(int, input().split())
    if b[u] == -1 and b[v] == -1:
        if w%2 == 0:
            a.append([u, v])
            b[u] = len(a)-1
            b[v] = len(a)-1
        else:
            a.append([u])
            b[u] = len(a)-1
            a.append([v])
            b[v] = len(a)-1
    elif b[u] == -1:
        if w%2 == 0:
            a[b[v]].append(u)
            b[u] = b[v]
        else:
            a.append([u])
            b[u] = len(a)-1
    elif b[v] == -1:
        if w%2 == 0:
            a[b[u]].append(v)
            b[v] = b[u]
        else:
            a.append([v])
            b[v] = len(a)-1
    else:
        if len(a[b[u]]) < len(a[b[v]]):
            for i in a[b[u]]:
                a[b[v]].append(i)
                b[i] = b[v]
        else:
            for i in a[b[v]]:
                a[b[u]].append(i)
                b[i] = b[u]
for i in range(n):
    if b[i+1] == b[1]:
        print(0)
    else:
        print(1)

Submission status: Wrong Answer

Solution code:
a = int(input())
vertices = [i for i in range(1, a + 1)]
color = {i: 0 for i in vertices}
for i in range(a - 1):
    c = [int(s) for s in input().split()]
    if c[2] % 2 == 0: color[c[0]], color[c[1]] = 1, 1
for i in vertices: print(color[i])

Submission status: Wrong Answer

Solution code:
a = int(input())
for i in range(a - 1): c = [int(s) for s in input().split()]
for i in range(a): print(0)

Submission status: Wrong Answer

Solution code:
import sys
N = int(input())
G = [[] for _ in range(N)]
for _ in range(N - 1):
    u, v, w = map(int, sys.stdin.readline().split())
    u -= 1
    v -= 1
    G[u].append((v, w))
    G[v].append((u, w))

done = {}
todo = [(0, True, 0)]
while todo:
    i, c, d = todo.pop()
    for ni, dd in G[i]:
        if ni in done:
            continue
        nd = d + dd
        if nd % 2 == 0:
            nc = c
        else:
            nc = not c
        todo.append((ni, nc, nd))
    done[i] = c

for i in range(N):
    if done[i] == True:
        print("0")
    else:
        print("1")