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 collections import defaultdict

class Tree:
  def __init__(self):
    self.edges = defaultdict(set)
  
  def add(self, x, y, w):
    self.edges[x].add((y, w))
    self.edges[y].add((x, w))
  
  def traverse(self, start):
    return self._traverse(start, 0, set([start]), [])
  
  def _traverse(self, start, dist, already_visited, result):
    out_nodes = self.edges[start]
    for out_node, w in out_nodes:
      if out_node in already_visited:
        continue
      new_dist = dist + w
      result.append((out_node, new_dist))
      break
      self._traverse(out_node, new_dist, already_visited | set([out_node]), result)
    return result
      
n = int(input())
tree = Tree()
for _ in range(n-1):
  xyw = list(map(int, input().split()))
  tree.add(*xyw)

node_dists = list(tree.traverse(1))
node_dists.append((1, 0))
node_dists = sorted(node_dists, key=lambda x: x[0])

for node, dist in node_dists:
  print(1 if dist % 2 == 0 else 0)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

class Tree:
  def __init__(self):
    self.edges = defaultdict(set)
  
  def add(self, x, y, w):
    self.edges[x].add((y, w))
    self.edges[y].add((x, w))
  
  def traverse(self, start):
    return self._traverse(start, 0, set([start]), [])
  
  def _traverse(self, start, dist, already_visited, result):
    out_nodes = self.edges[start]
    for out_node, w in out_nodes:
      if out_node in already_visited:
        continue
      new_dist = dist + w
      result.append((out_node, new_dist))
      self._traverse(out_node, new_dist, already_visited | set([out_node]), result)
    return result
      
n = int(input())
tree = Tree()
for _ in range(n-1):
  xyw = list(map(int, input().split()))
  tree.add(*xyw)

print(0)
"""
node_dists = list(tree.traverse(1))
node_dists.append((1, 0))
node_dists = sorted(node_dists, key=lambda x: x[0])

for node, dist in node_dists:
  print(1 if dist % 2 == 0 else 0)
"""

Submission status: Wrong Answer

Solution code:
n = int(input())
d = {}
for i in range(n - 1):
  u, v, w = map(int, input().split())
  if u in d:
    d[u].append([v, w])
  else:
    d[u] = [[v, w]]
  if v in d:
    d[v] = [[u, w]]
  else:
    d[v] = [[u, w]]

start = list(d.keys())[0]
ans = [0 for _ in range(n)]

def walk(poj):
  ans[poj - 1] = 1
  for i, way in enumerate(d[poj]):
    if way[1] % 2 == 0:
      d[poj][i][1] = 1
      walk(way[0])
      
walk(start)
      
for res in ans:
  print(res)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10000000)
N = int(input())
JU = [-1] * N

LI = [[] for _ in range(N)]
for _ in range(N-1):
    u,v,w = map(int,input().split())
    W = w%2
    LI[u-1].append([v-1,W])
    LI[v-1].append([u-1,W])
def dfs(x):
    for i in range(len(LI[x])):
        j = LI[x][i][0]
        k = LI[x][i][1]
        if JU[j] == -1:
            if JU[i] == 0:
                if k == 0:
                    JU[j] = 0
                    dfs(j)
                else:
                    JU[j] = 1
                    dfs(j)
            else:
                if k == 0:
                    JU[j] = 1
                    dfs(j)
                else:
                    JU[j] = 0
                    dfs(j)

for i in range(N):
    if JU[i] == -1:
        JU[i] = 0
        dfs(i)
for i in range(N):
    print(JU[i])

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(1000000)
N = int(input())
e = [ [] for i in range(N+1)]
for i in range(N-1):
    u, v, w = map(int, input().split())
    e[u].append((v,w))
    e[v].append((u,w))

col = [ -1 for i in range(N+1)]

def dfs(x,c):
    col[x] = c
    for v,w in e[x]:
        if col[v] == -1:
            dfs(v , c+w)

dfs(1,0)

print(col)

for i in range(1,N+1):
    print(col[i] % 2)

Submission status: Wrong Answer

Solution code:
n = int(input())

### WeightedUnionFind
# 親のノード番号を格納
par = [i for i in range(n+1)]
rank = [0] * (n+1)

# 根までの総重りを格納
weight = [0] * (n+1)

def find(x):
  if par[x] == x:
    return x
  else:
    y = find(par[x])
    weight[x] += weight[par[x]]
    par[x] = y
    return y

def union(x, y, w):
  rx = find(x)
  ry = find(y)
  if rank[rx] < rank[ry]:
    par[rx] = ry
    weight[rx] = w - weight[x] + weight[y]
  else:
    par[ry] = rx
    weight[ry] = -w - weight[y] + weight[x]
    if rank[rx] == rank[ry]:
      rank[rx] += 1

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

# x, y間の重り
def diff(x, y):
  return weight[x] - weight[y]

### WUF end


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

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

# 72

Submission status: Wrong Answer

Solution code:
n = int(input())

# 重り付きUnion Find
par = [i for i in range(n+1)]
rank = [0] * (n+1)
weight = [0] * (n+1)

def find(x):
  if par[x] == x:
    return x
  else:
    parent = par[x]
    return find(parent)

def dis(x):
  if par[x] == x:
    return 0
  else:
    a = dis(par[x]) + weight[x]
    return a

def union(x, y, w):
  if rank[par[x]] > rank[par[y]]:
    par[y] = x
    weight[y] = w
  else:
    par[x] = y
    weight[x] = w
    if rank[x] == rank[y]:
      rank[y] += 1
# end

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

for i in range(n):
  if dis(i+1) % 2 == 0:
    print('0')
  else:
    print('1')

Submission status: Wrong Answer

Solution code:
n = int(input())

# 重り付きUnion Find
par = [i for i in range(n+1)]
rank = [0] * (n+1)
weight = [0] * (n+1)

def find(x):
  if par[x] == x:
    return x
  else:
    parent = par[x]
    return find(parent)

def dis(x):
  if par[x] == x:
    return 0
  else:
    a = dis(par[x]) + weight[x]
    return a

def union(x, y, w):
  if rank[x] > rank[y]:
    par[y] = x
    weight[y] = w
  else:
    par[x] = y
    weight[x] = w
    if rank[x] == rank[y]:
      rank[y] += 1
# end

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

for i in range(n):
  if dis(i+1) % 2 == 0:
    print('0')
  else:
    print('1')

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(100000000)

class UnionFind():
    def __init__(self,size):
        self.table = [-1 for _ in range(size)]
    def find(self,x):
        if self.table[x] < 0:
            return x
        else:
            self.table[x] = self.find(self.table[x])
            return self.table[x]
    def union(self,a,b):
        a1 = self.find(a)
        a2 = self.find(b)
        if a1!= a2:
            if self.table[a1] < self.table[a2]:
                self.table[a1] = a2
            elif self.table[a2] < self.table[a1]:
                self.table[a2] = a1
            else:
                self.table[a1] = a2
                self.table[a2] -= 1
        return
N = int(input())
uf = UnionFind(N)
ou = []
ov = []
for _ in range(N-1):
    u,v,w = map(int,input().split())
    if w %2 == 0:
        uf.union(u-1,v-1)
    else:
        ou.append(u-1)
        ov.append(v-1)
M = len(ou)
oj = [0] * N
for i in range(M):
    if (oj[uf.find(ou[i])] == 0)&(oj[uf.find(ov[i])] == 0):
        oj[uf.find(ou[i])] = 1
        oj[uf.find(ov[i])] = 2
    elif (oj[uf.find(ou[i])] == 0)&(oj[uf.find(ov[i])] == 1):
        oj[uf.find(ou[i])] = 2
    elif (oj[uf.find(ou[i])] == 0)&(oj[uf.find(ov[i])] == 2):
        oj[uf.find(ou[i])] = 1
    elif (oj[uf.find(ou[i])] == 1)&(oj[uf.find(ov[i])] == 0):
        oj[uf.find(ov[i])] = 2
    elif (oj[uf.find(ou[i])] == 2)&(oj[uf.find(ov[i])] == 0):
        oj[uf.find(ov[i])] = 1
for i in range(N):
    if oj[uf.find(i)] == 1:
        print(1)
    else:
        print(0)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(100000000)

class UnionFind():
    def __init__(self,size):
        self.table = [-1 for _ in range(size)]
    def find(self,x):
        if self.table[x] < 0:
            return x
        else:
            self.table[x] = self.find(self.table[x])
            return self.table[x]
    def union(self,a,b):
        a1 = self.find(a)
        a2 = self.find(b)
        if a1!= a2:
            if self.table[a1] < self.table[a2]:
                self.table[a1] = a2
            elif self.table[a2] < self.table[a1]:
                self.table[a2] = a1
            else:
                self.table[a1] = a2
                self.table[a2] -= 1
        return
N = int(input())
uf = UnionFind(N)
ou = []
ov = []
for _ in range(N-1):
    u,v,w = map(int,input().split())
    if w %2 == 0:
        uf.union(u-1,v-1)
    else:
        ou.append(u-1)
        ov.append(v-1)
M = len(ou)
oj = [0] * N
for i in range(N):
    print(uf.find(i))
for i in range(M):
    if (oj[uf.find(ou[i])] == 0)&(oj[uf.find(ov[i])] == 0):
        print(uf.find(ou[i]))
        print(uf.find(ov[i]))
        oj[uf.find(ou[i])] = 1
        oj[uf.find(ov[i])] = 2
    elif (oj[uf.find(ou[i])] == 0)&(oj[uf.find(ov[i])] == 1):
        oj[uf.find(ou[i])] = 2
    elif (oj[uf.find(ou[i])] == 0)&(oj[uf.find(ov[i])] == 2):
        oj[uf.find(ou[i])] = 1
    elif (oj[uf.find(ou[i])] == 1)&(oj[uf.find(ov[i])] == 0):
        oj[uf.find(ov[i])] = 2
    elif (oj[uf.find(ou[i])] == 2)&(oj[uf.find(ov[i])] == 0):
        oj[uf.find(ov[i])] = 1
    print(oj)
for i in range(N):
    if oj[uf.find(i)] == 1:
        print(1)
    else:
        print(0)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(100000000)

class UnionFind():
    def __init__(self,size):
        self.table = [-1 for _ in range(size)]
    def find(self,x):
        if self.table[x] < 0:
            return x
        else:
            self.table[x] = self.find(self.table[x])
            return self.table[x]
    def union(self,a,b):
        a1 = self.find(a)
        a2 = self.find(b)
        if a1!= a2:
            if self.table[a1] < self.table[a2]:
                self.table[a1] = a2
            elif self.table[a2] < self.table[a1]:
                self.table[a2] = a1
            else:
                self.table[a1] = a2
                self.table[a2] -= 1
        return
N = int(input())
uf = UnionFind(N)
ou = []
ov = []
for _ in range(N-1):
    u,v,w = map(int,input().split())
    if w %2 == 0:
        uf.union(u-1,v-1)
    else:
        ou.append(u-1)
        ov.append(v-1)
M = len(ou)
oj = [0] * N
for i in range(M):
    if (oj[uf.find(ou[i])] == 0)&(oj[uf.find(ov[i])] == 0):
        oj[uf.find(ou[i])] = 1
        oj[uf.find(oj[i])] = 2
    elif (oj[uf.find(ou[i])] == 0)&(oj[uf.find(ov[i])] == 1):
        oj[uf.find(ou[i])] = 2
    elif (oj[uf.find(ou[i])] == 0)&(oj[uf.find(ov[i])] == 2):
        oj[uf.find(ou[i])] = 1
    elif (oj[uf.find(ou[i])] == 1)&(oj[uf.find(ov[i])] == 0):
        oj[uf.find(ov[i])] = 2
    elif (oj[uf.find(ou[i])] == 2)&(oj[uf.find(ov[i])] == 0):
        oj[uf.find(ov[i])] = 1
for i in range(N):
    if oj[uf.find(i)] == 1:
        print(1)
    else:
        print(0)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(100000000)

class UnionFind():
    def __init__(self,size):
        self.table = [-1 for _ in range(size)]
    def find(self,x):
        if self.table[x] < 0:
            return x
        else:
            self.table[x] = self.find(self.table[x])
            return self.table[x]
    def union(self,a,b):
        a1 = self.find(a)
        a2 = self.find(b)
        if a1!= a2:
            if self.table[a1] < self.table[a2]:
                self.table[a1] = a2
            elif self.table[a2] < self.table[a1]:
                self.table[a2] = a1
            else:
                self.table[a1] = a2
                self.table[a2] -= 1
        return
N = int(input())
uf = UnionFind(N)
ou = []
ov = []
for _ in range(N-1):
    u,v,w = map(int,input().split())
    if w %2 == 0:
        uf.union(u-1,v-1)
    else:
        ou.append(u-1)
        ov.append(v-1)
M = len(ou)
oj = [0] * N
for i in range(M):
    if (oj[uf.find(ou[i])] == 0)&(oj[uf.find(ov[i])] == 0):
        oj[uf.find(ou[i])] = 1
    elif (oj[uf.find(ou[i])] == 0)&(oj[uf.find(ov[i])] == 1):
        oj[uf.find(ou[i])] = 2
    elif (oj[uf.find(ou[i])] == 0)&(oj[uf.find(ov[i])] == 2):
        oj[uf.find(ou[i])] = 1
    elif (oj[uf.find(ou[i])] == 1)&(oj[uf.find(ov[i])] == 0):
        oj[uf.find(ov[i])] = 2
    elif (oj[uf.find(ou[i])] == 2)&(oj[uf.find(ov[i])] == 0):
        oj[uf.find(ov[i])] = 1
        
for i in range(N):
    if oj[uf.find(i)] == 1:
        print(1)
    else:
        print(0)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Tue May 21 22:42:19 2019

@author: Yamazaki Kenichi
"""

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

M = [[] for i in range(N+1)]
for c in uvw:
    M[c[0]].append([c[1],c[2]])
    M[c[1]].append([c[0],c[2]])
M[0] = [[1,0]]

class Dijkstra(object):
    
    def __init__(self, size, INFTY=10**9+1):
        self.color = [-1 for i in range(size)]
        self.d = [INFTY for i in range(size)]
        self.p = [-1 for i in range(size)]
        self.d[0] = 0 
        self.p[0] = -1
    
    def add(self, M, INFTY=10**9+1):
        while True:
            mincost = INFTY
            for i in range(len(self.d)):
                if self.color[i] != 1 and self.d[i] < mincost:
                    mincost = self.d[i]
                    u = i
#            print(u,self.color,self.d)
            if mincost == INFTY:
                break
            self.color[u] = 1    
            for v in range(len(self.d)):
                for k in range(len(M[u])):
                    if self.color[v] != 1 and M[u][k][0] == v:
                        if self.d[v] > self.d[u] + M[u][k][1]:
                            self.d[v] = self.d[u] + M[u][k][1]
                            self.p[v] = u
                            self.color[v] = 0
        return self.d

dij = Dijkstra(N+1)
dij.add(M)
for i in range(1,N+1):
    ans = 0 if dij.d[i] % 2 == 0 else 1
    print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())

array = []
vArray = [-1] * n

for _ in range(n):
    array.append([-1] * n)

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

def check(checkV, color):
    if vArray[checkV] >= 0:
        return
    vArray[checkV] = color
    for i in range(n):
        if array[checkV][i] >= 0:
            if array[checkV][i] % 2 == 0:
                check(i, color)
            else:
                check(i, not color)


check(0, True)

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

Submission status: Wrong Answer

Solution code:
import queue

N = int(input())

adj = dict([(i+1, []) for i in range(N)])
color = [-1 for i in range(N)]

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

q = queue.Queue()
q.put((1, 0))
while(not q.empty()):
    v, w = q.get()
    color[v-1] = (color[v-1]+w)%2
    for nv, nw in adj[v]:
        if(color[nv-1]!=-1):
            continue
        q.put((nv, nw))

for c in color:
    print(c)

Submission status: Wrong Answer

Solution code:
N = int(input())

res = []
for i in range(N-1):
    x, y, w = (int(i) for i in input().split())
    res.append((x, y, w))
res = sorted(res)
print(res)

result = [-1] * N

for tup in res:
    if result[tup[0]-1] == -1:
        if result[tup[1]-1] == 0 and tup[2] % 2 == 0:
            result[tup[0]-1] = 0
        if result[tup[1]-1] == 0 and tup[2] % 2 == 1:
            result[tup[0]-1] = 1
        if result[tup[1]-1] == 1 and tup[2] % 2 == 0:
            result[tup[0]-1] = 1
        if result[tup[1]-1] == 1 and tup[2] % 2 == 1:
            result[tup[0]-1] = 0
        if result[tup[1]-1] == -1:
            result[tup[0]-1] = 0            
            if tup[2] % 2 == 0:
                result[tup[1]-1] = 0
            else:
                result[tup[1]-1] = 1
    elif result[tup[0]-1] == 0:
        if tup[2] % 2 == 0:
            result[tup[1]-1] = 0
        else:
            result[tup[1]-1] = 1
    elif result[tup[0]-1] == 1:
        if tup[2] % 2 == 0:
            result[tup[1]-1] = 1
        else:
            result[tup[1]-1] = 0
    
for hoge in result:
    print(hoge)

Submission status: Wrong Answer

Solution code:
def rec(edges, nodes, cp, cdistance):
    nodes[cp] = (cdistance % 2 == 0)
    if edges[cp]:
        for v, w in edges[cp]:
            rec(edges, nodes, v, cdistance + w)


def main():
    N = int(input())
    nodes = [False] * N
    edges = [[] for _ in range(N)]

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

    rec(edges, nodes, 0, 0)

    for n in nodes:
        print(0 if n else 1)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def rec(edges, nodes, cp):
    if edges[cp]:
        for v, w in edges[cp]:
            nodes[v] = not nodes[cp] if w % 2 else nodes[cp]
            rec(edges, nodes, v)


def main():
    N = int(input())
    nodes = [False] * N
    edges = [[] for _ in range(N)]

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

    rec(edges, nodes, 0)

    for n in nodes:
        print(0 if n else 1)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# coding: utf-8

n = int(input())

inputs = []
for i in range(n-1):
    inputs.append(tuple(map(int, input().split())))

class Node(object):

    def __init__(self, number):
        self.number = number
        self.others = []
        self.color = -1

nodes = dict()
for u, v, w in inputs:
    nodes.setdefault(u, Node(u)).others.append((v, w))
    nodes.setdefault(v, Node(v)).others.append((u, w))

color = 0
queue = []
for key, node in nodes.items():
    if node.color != -1:
        continue
    queue.append(node)
    while queue:
        n = queue.pop()
        n.color = color
        for other in n.others:
            if other[1] % 2 == 0:
                if nodes[other[0]].color == -1:
                    queue.append(nodes[other[0]])
    color = int(not color)

for _, node in sorted(nodes.items()):
    print(node.color)

Submission status: Wrong Answer

Solution code:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math

mod = 10**9+7

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

# v は今見ている頂点，p はｖの親
def dfs(node,p,color): # Depth First Serch
    res[node] = color
    index_num = [n for n, v in enumerate(u) if v == node+1]
    for index in index_num:
        new_node = u[index]
        if new_node == p:
            continue
        if w[index] % 2 == 1:dfs(new_node,node,1-color)
        else:dfs(new_node,node,color)



#tree 探索　
# vは頂点，ｐは親
dfs(0,-1,1)

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

Submission status: Wrong Answer

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

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

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

color_list = {1: "1"}

for e in t:
    if e[2] % 2 == 0:
        if color_list.get(e[0]) == "1" or color_list.get(e[1]) == "1":
            color_list[e[0]] = "1"
            color_list[e[1]] = "1"
        else:
            color_list[e[0]] = "0"
            color_list[e[1]] = "0"
    else:
        if color_list.get(e[0]) == "1" or color_list.get(e[1]) == "0":
            color_list[e[0]] = "1"
            color_list[e[1]] = "0"
        else:
            color_list[e[0]] = "0"
            color_list[e[1]] = "1"

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

Submission status: Wrong Answer

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

point = [-1] * n

for t in t_list:
    u = t[0] - 1
    v = t[1] - 1
    w = t[2]

    if w % 2 == 0:
        if point[u] != -1:
            point[v] = point[u]
        elif point[v] != -1:
            point[u] = point[v]
        else:
            point[v] = point[u] = 0
    else:
        if point[u] == -1:
            point[u] = 0

        if point[u] == 0:
            point[v] = 1
        else:
            point[v] = 0

for p in point:
    print(p)

Submission status: Wrong Answer

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

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

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

white_list = [1]
black_list = []

for e in t:
    if e[2] % 2 == 0:
        if e[0] in white_list or e[1] in white_list:
            white_list.append(e[0])
            white_list.append(e[1])
        else:
            black_list.append(e[0])
            black_list.append(e[1])
    else:
        if e[0] in white_list or e[1] in black_list:
            white_list.append(e[0])
            black_list.append(e[1])
        else:
            white_list.append(e[1])
            black_list.append(e[0])

for i in range(n):
    if i in white_list:
        print(0)
    else:
        print(1)

Submission status: Wrong Answer

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

point = [-1] * n

for t in t_list:
    u = t[0] - 1
    v = t[1] - 1
    w = t[2]

    if w % 2 == 0:
        if point[u] == -1 and point[v] == -1:
            point[u] = point[v] = 1
        elif point[u] == -1 and point[v] != -1:
            point[u] = point[v]
        else:
            point[v] = point[u]
    else:
        if point[u] == 0:
            point[v] = 1
        else:
            point[v] = 0

for p in point:
    print(p)

Submission status: Wrong Answer

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

point = [-1] * n

for t in t_list:
    u = t[0] - 1
    v = t[1] - 1
    w = t[2]

    if w % 2 == 0:
        if point[u] == -1:
            point[u] = 1
        point[v] = point[u]
    else:
        if point[u] == 0:
            point[v] = 1
        else:
            point[v] = 0

for p in point:
    print(p)

Submission status: Wrong Answer

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

point = [-1] * n
dp = [[-1] * n for i in range(n)]

for t in t_list:
    u = t[0] - 1
    v = t[1] - 1
    w = t[2]

    if w % 2 == 0:
        dp[u][v] = 1
    else:
        dp[u][v] = 0

for i in range(0, n):
    if point[i] == -1:
        point[i] = 1
    for j in range(i + 1, n):
        if dp[i][j] == 1:
            point[j] = point[i]
        elif dp[i][j] == 0:
            if point[i] == 1:
                point[j] = 0
            else:
                point[j] = 1

for p in point:
    print(p)

Submission status: Wrong Answer

Solution code:
def coloring(A, n, colors, color):
  for (i,w) in enumerate(A[n]):
    if w != 0:
    #   print(i)
      A[i][n] = 0
      if w % 2 == 1:
        color = str(1-int(color))
      colors[i] = color
    #   print(colors)
      coloring(A, i, colors, color)


if __name__ == '__main__':
  a = int(input())
  A = [[0 for i in range(a)] for j in range(a)]

  colors = ['0' for i in range(a)]
  for _ in range(a-1):
    u,v,w = tuple([int(x) for x in input().split()])
    # print(w)
    A[u-1][v-1] = w
    A[v-1][u-1] = w
    # print(A)
#   print(A)
  coloring(A, 0, colors, '0')
  for c in colors:
    print(c)

Submission status: Wrong Answer

Solution code:
dp = [-1] * (10 ** 5)

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

for t in tree:
    if t[2] % 2 == 0:
        if dp[t[0] - 1] != -1:
            dp[t[1] - 1] = dp[t[0] - 1]
        elif dp[t[1] - 1] != -1:
            dp[t[0] - 1] = dp[t[1] - 1]
        else:
            dp[t[0] - 1] = dp[t[1] - 1] = 0
    else:
        if dp[t[0] - 1] ==  0 and dp[t[1] - 1] == -1:
            dp[t[1] - 1] = 1
        elif dp[t[0] - 1] == 1 and dp[t[1] - 1] == -1:
            dp[t[1] - 1] = 0
        elif dp[t[1] - 1] ==  0 and dp[t[0] - 1] == -1:
            dp[t[0] - 1] = 1
        elif dp[t[1] - 1] == 1 and dp[t[0] - 1] == -1:
            dp[t[0] - 1] = 0
        else:
            dp[t[0] - 1] = 0
            dp[t[1] - 1] = 1

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

Submission status: Wrong Answer

Solution code:
n=int(input())
kyori=sorted([list(map(int,input().split())) for i in range(n-1)])
ans=[2]*n
ans[0]=0

for i in range(n-1):
  if ans[kyori[i][0]-1]!=2:
    C=ans[kyori[i][0]-1]
    d=0
  else:
    C=ans[kyori[i][1]-1]
    d=1
    
  if kyori[i][2]%2==0:
    ans[kyori[i][abs(1-d)]-1]=C
  else:
    ans[kyori[i][abs(1-d)]-1]=abs(1-C)
    
for i in range(n):
  print(ans[i])

Submission status: Wrong Answer

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

def search(UF,loc):
    if UF[loc][0] == loc:
        return UF[loc]
    else:
        UF[loc][0],temp = search(UF,UF[loc][0])
        UF[loc][1] += temp
        return UF[loc]
for i in range(1,n+1):
    parent = search(UF,i)
    if parent[1]%2 == 0:
        print(0)
    else:
        print(1)

Submission status: Wrong Answer

Solution code:
n=int(input())
kyori=sorted([list(map(int,input().split())) for i in range(n-1)])
ans=[2]*n
ans[0]=0

for i in range(n-1):
  if ans[kyori[i-1][0]-1]!=2:
    C=ans[kyori[i-1][0]-1]
    d=0
  else:
    C=ans[kyori[i-1][1]-1]
    d=1
    
  if kyori[i-1][2]%2==0:
    ans[kyori[i-1][abs(1-d)]-1]=C
  else:
    ans[kyori[i-1][abs(1-d)]-1]=abs(1-C)
    
for i in range(n):
  print(ans[i])

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Tue May 21 22:42:19 2019

@author: Yamazaki Kenichi
"""

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

M = [[] for i in range(N+1)]
for c in uvw:
    M[c[0]].append([c[1],c[2]])
    M[c[1]].append([c[0],c[2]])
M[0] = [[1,0]]

class Dijkstra(object):
    
    def __init__(self, size, INFTY=10**6):
        self.color = [-1 for i in range(size)]
        self.d = [INFTY for i in range(size)]
        self.p = [-1 for i in range(size)]
        self.d[0] = 0 
        self.p[0] = -1
    
    def add(self, M, INFTY=10**6):
        while True:
            mincost = INFTY
            for i in range(len(self.d)):
                if self.color[i] != 1 and self.d[i] < mincost:
                    mincost = self.d[i]
                    u = i
#            print(u,self.color,self.d)
            if mincost == INFTY:
                break
            self.color[u] = 1    
            for v in range(len(self.d)):
                for k in range(len(M[u])):
                    if self.color[v] != 1 and M[u][k][0] == v:
                        if self.d[v] > self.d[u] + M[u][k][1]:
                            self.d[v] = self.d[u] + M[u][k][1]
                            self.p[v] = u
                            self.color[v] = 0
        return self.d

dij = Dijkstra(N+1)
dij.add(M)
for i in range(1,N+1):
    ans = 0 if dij.d[i] % 2 == 0 else 1
    print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
kyori=sorted([list(map(int,input().split())) for i in range(n-1)])
ans=[0]*n

for i in range(n-1):
  C=ans[kyori[i-1][0]-1]
  if kyori[i-1][2]%2==0:
    ans[kyori[i-1][1]-1]=C
  else:
    ans[kyori[i-1][1]-1]=abs(1-C)
    
for i in range(n):
  print(ans[i])

Submission status: Wrong Answer

Solution code:
n=int(input())
kyori=sorted([list(map(int,input().split())) for i in range(n-1)])
ans=[0]*n

for i in range(n-1):
  C=ans[kyori[i-1][0]]
  if kyori[i-1][2]%2==0:
    ans[kyori[i-1][1]-1]=C
  else:
    ans[kyori[i-1][1]-1]=abs(1-C)
    
for i in range(n):
  print(ans[i])

Submission status: Wrong Answer

Solution code:
n=int(input())
kyori=sorted([list(map(int,input().split())) for i in range(n-1)])
ans=[0]*n

for i in range(n):
  C=kyori[i-1][0]
  if kyori[i-1][2]%2==0:
    ans[kyori[i-1][1]-1]=C
  else:
    ans[kyori[i-1][1]-1]=abs(1-C)
    
for i in range(n):
  print(ans[i])

Submission status: Wrong Answer

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

def search(UF,loc):
    if UF[loc][0] == loc:
        return UF[loc]
    else:
        UF[loc][0],temp = search(UF,UF[loc][0])
        UF[loc][1] += temp
        return UF[loc]
for i in range(1,n+1):
    parent = search(UF,i)
    if parent[1]%2 == UF[parent[0]][1]%2:
        print(0)
    else:
        print(1)

Submission status: Wrong Answer

Solution code:
n=int(input())
kyori=sorted([list(map(int,input().split())) for i in range(n-1)])
ans=[0]*n

for i in range(n):
  C=kyori[i-1][0]
  if kyori[i-1][2]%2==0:
    ans[kyori[i-1][1]-1]=C
  else:
    ans[kyori[i-1][1]-1]=1-C
    
for i in range(n):
  print(ans[i])

Submission status: Wrong Answer

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

def search(UF,loc):
    if UF[loc][0] == loc:
        return UF[loc]
    else:
        UF[loc][0],temp = search(UF,UF[loc][0])
        UF[loc][1] += temp
        return UF[loc]
for i in range(1,n+1):
    if search(UF,i)[1]%2 == 1:
        print(1)
    else:
        print(0)

Submission status: Wrong Answer

Solution code:
class Node:
  def __init__(self):
    self.parent = None
    self.distance = None
  def set_parent(self,p,d):
    self.parent = p
    self.distance = d
  def dist_to_root(self):
    total_distance = 0
    cur = self
    while(True):
      if cur.parent == None:
        break
      else:
        total_distance += cur.distance
        cur = cur.parent
    return total_distance

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

for _ in range(N - 1):
  u,v,w = map(int,input().split())
  nodes[v-1].set_parent(nodes[u-1],w)
  
for node in nodes:
  if node.dist_to_root() % 2 == 0:
    print(1)
  else:
    print(0)

Submission status: Wrong Answer

Solution code:
class Node:
  def __init__(self):
    self.parent = None
    self.distance = None
  def set_parent(self,p,d):
    self.parent = p
    self.distance = d
  def dist_to_root(self):
    total_distance = 0
    cur = self
    while(True):
      if cur.parent == None:
        break
      else:
        total_distance += cur.distance
        cur = cur.parent
    return total_distance

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

for _ in range(N - 1):
  u,v,w = map(int,input().split())
  nodes[v-1].set_parent(nodes[u-1],w)
  
for node in nodes:
  if node.dist_to_root() % 2 == 1:
    print(0)
  else:
    print(1)