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())
UVW=[]
for i in range(N-1):
  u,v,w=map(int,input().split())
  if w%2==0:
    W=0
  else:
    W=1
  UVW.append([u,v,W])
f=[-1]*(N+1)
for i in range(N-1):
  if f[UVW[i][0]]==-1:
    f[UVW[i][0]]=0
    if UVW[i][2]==0:
      f[UVW[i][1]]=f[UVW[i][0]]
    else:
      f[UVW[i][1]]=1-f[UVW[i][0]]
  else:
    if UVW[i][2]==0:
      f[UVW[i][1]]=f[UVW[i][0]]
    else:
      f[UVW[i][1]]=1-f[UVW[i][0]]
for i in range(1,N+1):
  if f[i]==-1:
    print(0)
  else:
    print(f[i])

Submission status: Wrong Answer

Solution code:
# D - Even Relation

from collections import deque

n = int(input())

# adjacency list
alist = [[] for _ in range(n)]

for _ in range(n-1):
    u, v, w = map(int, input().split())
    u -= 1
    v -= 1
    alist[u].append((v, w%2))
    #alist[v].append((u, w%2))

#print(alist)

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

q = deque()
q.append(0)

while len(q)>0:
    node = q.popleft()
    for i, j in alist[node]:
        if dlist[i]<0:
            q.append(i)
            dlist[i] = (dlist[node] + j)%2
        else:
            continue

'''
def dfs(node, par, dist):
    dlist[node] = dist%2
    for i, j in alist[node]:
        if i==par:
            continue
        else:
            dfs(i, node, dist+j)
dfs(0,0,0)
'''

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

Submission status: Wrong Answer

Solution code:
# D - Even Relation

from collections import deque

n = int(input())

# adjacency list
alist = [[] for _ in range(n)]

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

#print(alist)

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

q = deque()
q.append(0)

while len(q)>0:
    node = q.popleft()
    for i, j in alist[node]:
        if dlist[i]<0:
            q.append(i)
            dlist[i] = j
        else:
            continue

'''
def dfs(node, par, dist):
    dlist[node] = dist%2
    for i, j in alist[node]:
        if i==par:
            continue
        else:
            dfs(i, node, dist+j)
dfs(0,0,0)
'''

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

Submission status: Wrong Answer

Solution code:
import heapq

n = int(input())
l = [list(map(int,input().split())) for _ in range(n-1)]
li = [[0 for i in range(n)]for j in range(n)]

for items in l:
    u,v,w = items
    li[u-1][v-1] = w
    li[v-1][u-1] = w

inf = 10**10

def dijkstra(start, W):
    distance_list = [inf] * len(W)
    distance_list[start] = 0
    done_list = []
    wait_heap = []
    for i, d in enumerate(distance_list):
        heapq.heappush(wait_heap, (d, i))
    while wait_heap:
        p = heapq.heappop(wait_heap)
        i = p[1]
        if i in done_list:
            continue
        done_list.append(i)
        for j, x in enumerate(W[i]):
            if x != 0 and j not in done_list:
                d = min(distance_list[j], distance_list[i]+x)
                distance_list[j] = d
                heapq.heappush(wait_heap, (d, j))
    return distance_list

for i,j in enumerate(dijkstra(0,li)):
    if j%2 == 1:
        print(1)
    else:
        print(0)

Submission status: Wrong Answer

Solution code:
N = int(input())
a = []
b = {}
for i in range(N-1):
    c = list(map(int, input().split()))
    a.append(c)

for l in a:
    if l[0] not in b and l[1] not in b:
        if l[2]%2==0:
            b[l[0]] = 0
            b[l[1]] = 0
        else:
            b[l[0]] = 1
            b[l[1]] = 1
    else:
        if l[0] not in b:
            if l[2]%2==0 and b[l[1]]==0: 
                b[l[0]] = 0                
            elif l[2]%2!=0 and b[l[1]]==0:
                b[l[0]] = 1               
            elif l[2]%2==0 and b[l[1]]!=0:
                b[l[0]] = 1
            else:
                b[l[0]] = 0
                
        elif l[1] not in b:
            if l[2]%2==0 and b[l[0]]==0: 
                b[l[1]] = 0                
            elif l[2]%2!=0 and b[l[0]]==0:
                b[l[1]] = 1               
            elif l[2]%2==0 and b[l[0]]!=0:
                b[l[1]] = 1
            else:
                b[l[1]] = 0

for j in range(1,N+1):
    print(b[j])

Submission status: Wrong Answer

Solution code:
N = int(input())
a = []
b = {}
for i in range(N-1):
    c = list(map(int, input().split()))
    a.append(c)

for l in a:
    if l[0] not in b and l[1] not in b:
        if l[2]%2==0:
            b[l[0]] = 0
            b[l[1]] = 0
        else:
            b[l[0]] = 1
            b[l[1]] = 1
    else:
        if l[0] not in b:
            if (b[l[1]]+l[2])%2==0: 
                b[l[0]] = b[l[1]]
            else:
                b[l[0]] = 1 + b[l[1]]
        elif l[1] not in b:
            if (b[l[0]]+l[2])%2==0: 
                b[l[1]] = b[l[0]]               
            else:
                b[l[1]] = 1 + b[l[0]]
                
for j in range(1,N+1):
    print(b[j]%2)

Submission status: Wrong Answer

Solution code:
N = int(input())
a = []
b = {}
for i in range(N-1):
    c = list(map(int, input().split()))
    a.append(c)

for l in a:
    if l[0] not in b and l[1] not in b:
        if l[2]%2==0:
            b[l[0]] = 0
            b[l[1]] = 0
        else:
            b[l[0]] = 1
            b[l[1]] = 1
    else:
        if l[0] not in b:
            if (b[l[1]]+l[2])%2==0: 
                b[l[0]] = 0
            else:
                b[l[0]] = 1
        elif l[1] not in b:
            if (b[l[0]]+l[2])%2==0: 
                b[l[1]] = 0
                
            else:
                b[l[1]] = 1

for j in range(1,N+1):
    print(b[j])

Submission status: Wrong Answer

Solution code:
N = int(input())
a = []
b = {}
for i in range(N-1):
    c = list(map(int, input().split()))
    a.append(c)

for l in a:
    if l[2]%2==0:
        if l[0] not in b:
            b[l[0]] = 0
        if l[1] not in b:
            b[l[1]] = 0

    else:
        if l[0] not in b:
            b[l[0]] = 1
        if l[1] not in b:
            b[l[1]] = 1

for j in range(1,N+1):
    print(b[j])

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
import queue
N = int(input())

d = defaultdict(lambda: defaultdict(int))
for i in range(N-1):
    u, v, w = list(map(int, input().split()))
    d[u-1][v-1] = w
    d[v-1][u-1] = w
for i in range(N):
    if len(d[i]) == 1:
        start = i
        break

visited = [False for _ in range(N)]
visited[start] = True
all_length =  [0 for _  in range(N)]
q = queue.Queue()
q.put(start)
while not q.empty():
    i = q.get()
    for v, w in d[i].items():
        if not visited[v]:
            q.put(v)
            visited[v] = True
            all_length[v] = all_length[i] + w
print(all_length)
for length in all_length:
    print(length % 2)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(1000000)


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


n = int(input())
e = [[] for _ in range(n)]
for _ in range(n - 1):
    a, b, c = [int(i) - 1 for i in input().split()]
    c += 1
    e[a].append([b, c])
    e[b].append([a, c])

c = [0] * n

def dfs(i=0, r=-1, co=0):
    c[i] = co
    for j, k in e[i]:
        if j != r:
            dfs(j, i, (co+k) % 2)


print("\n".join(map(str, c)))

Submission status: Wrong Answer

Solution code:
n = int(input())
uvw = [list(map(int, input().split())) for i in range(n-1)]
color = [0]*n
visited = [0]*n
visited[0] = 1
for i in uvw:
  if visited[i[0]-1] == 0:
    if i[2] % 2 == 0:
      color[i[0]-1] = color[i[1]-1]
    else:
      color[i[0]-1] = 1 - color[i[1]-1]
    visited[i[0]-1] = 1
  if visited[i[1]-1] == 0:
    if i[2] % 2 == 0:
      color[i[1]-1] = color[i[0]-1]
    else:
      color[i[1]-1] = 1 - color[i[0]-1]
    visited[i[1]-1] = 1
for i in color:
  print(i)

Submission status: Wrong Answer

Solution code:
n = int(input())
uvw = [list(map(int, input().split())) for i in range(n-1)]
color = [0]*n
visited = [0]*n
visited[0] = 1
for i in uvw:
  if visited[i[0]-1] == 0:
    if i[2] % 2 == 0:
      color[i[0]-1] = color[i[1]-1]
    else:
      color[i[0]-1] = 1 - color[i[1]-1]
  if visited[i[1]-1] == 0:
    if i[2] % 2 == 0:
      color[i[1]-1] = color[i[0]-1]
    else:
      color[i[1]-1] = 1 - color[i[0]-1]
  visited[i[1]-1] = 1
for i in color:
  print(i)

Submission status: Wrong Answer

Solution code:
n = int(input())
uvw = [list(map(int, input().split())) for i in range(n-1)]
color = [0]*n
visited = [0]*n
for i in uvw:
  if visited[i[0]-1] == 0:
    if i[2] % 2 == 0:
      color[i[0]-1] = color[i[1]-1]
    else:
      color[i[0]-1] = 1 - color[i[1]-1]
  if visited[i[1]-1] == 0:
    if i[2] % 2 == 0:
      color[i[1]-1] = color[i[0]-1]
    else:
      color[i[1]-1] = 1 - color[i[0]-1]
  visited[i[0]-1] = 1
  visited[i[1]-1] = 1
for i in color:
  print(i)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
import sys
sys.setrecursionlimit(1000000)
n = int(input())

g = [[] * n 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))

chk = [0]*n
chk[0] = 1

def dfs(now, prev):
    global chk
    # 今いる頂点から行ける頂点を順に next に入れてループ
    for next in g[now]:
        if next[0] != prev:
          if next[1]%2 == 0:
            chk[next[0]] = chk[now]
          elif next[1]%2 == 1:
            chk[next[0]] = (chk[now]*-1)
          
          dfs(next[0], now)
        
dfs(0,-1)
print(chk)
chk = tuple(chk)
for i in chk:
  if i == 1:
    print(1)
  else:
    print(0)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n = int(input())

def find(x):
    if par[x] < 0:
        return x
    else:
        px = find(par[x])
        wei[x] += wei[par[x]]
        par[x] = px
        return px

def weight(x):
    find(x)
    return wei[x]

#w[y]=w[x]+wとなるようにxとyを併合
def unite(x,y,w):
    w += wei[x]-wei[y]
    x = find(x)
    y = find(y)
    
    if x == y:
        return False
    else:
        #sizeの大きいほうがx
        if par[x] > par[y]:
            x,y = y,x
            w = -w
        par[x] += par[y]
        par[y] = x
        wei[y] = w
        return True

def diff(x,y):
    return weight(y)-weight(x)

par = [-1]*n
wei = [0]*n
chk = []
for i in range(n-1):
  u,v,w = [int(i) for i in input().split()]
  chk_append = chk.append
  chk_append((u,v,w))
  
from operator import itemgetter
chk = sorted(chk,key=itemgetter(0))

for i in chk:
  unite(i[0]-1,i[1]-1,i[2])    

m = find(0)
for i in range(n):
  #print(diff(m,i),i+1)
  if diff(m,i)%2 == 0:
    print(0)
  else:
    print(1)

Submission status: Wrong Answer

Solution code:
import sys, copy, math, heapq, bisect
from itertools import accumulate
from collections import deque, defaultdict, Counter
input = sys.stdin.readline # 文字列の入力のとき'/n'まで受け取るので注意!!!
sys.setrecursionlimit(500000)

N = int(input())
T = [defaultdict(int) for i in range(N)]
for i in range(N-1):
    u,v,w = map(int,input().split())
    T[u-1][v-1] = w
    T[v-1][u-1] = w
visited = [0]*N
ans = [0]*N
def bfs(now,col):
    visited[now] = 1
    for i in T[now].keys():
        if visited[i] == 1:
            continue
        if T[now][i]%2==1:
            ans[i] = (col+1)%2
            col = (col+1)%2
        else:
            ans[i] = col
        bfs(i,col)
bfs(0,0)
for i in range(N):
    print(ans[i])

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n = int(input())

def find(x):
    if par[x] < 0:
        return x
    else:
        px = find(par[x])
        wei[x] += wei[par[x]]
        par[x] = px
        return px

def weight(x):
    find(x)
    return wei[x]

#w[y]=w[x]+wとなるようにxとyを併合
def unite(x,y,w):
    w += wei[x]-wei[y]
    x = find(x)
    y = find(y)
    
    if x == y:
        return False
    else:
        #sizeの大きいほうがx
        if par[x] > par[y]:
            x,y = y,x
            w = -w
        par[x] += par[y]
        par[y] = x
        wei[y] = w
        return True

def diff(x,y):
    return weight(y)-weight(x)

par = [-1]*n
wei = [0]*n

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

m = find(0)
for i in range(n):
  if diff(m,i)%2 == 0:
    print(0)
  else:
    print(1)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n = int(input())

def find(x):
    if par[x] < 0:
        return x
    else:
        px = find(par[x])
        wei[x] += wei[par[x]]
        par[x] = px
        return px

def weight(x):
    find(x)
    return wei[x]

#w[y]=w[x]+wとなるようにxとyを併合
def unite(x,y,w):
    w += wei[x]-wei[y]
    x = find(x)
    y = find(y)
    
    if x == y:
        return False
    else:
        #sizeの大きいほうがx
        if par[x] > par[y]:
            x,y = y,x
            w = -w
        par[x] += par[y]
        par[y] = x
        wei[y] = w
        return True

def diff(x,y):
    return weight(y)-weight(x)

par = [-1]*n
wei = [0]*n

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

print(0)
for i in range(1,n):
  if diff(0,i)%2 == 0:
    print(0)
  else:
    print(1)

Submission status: Wrong Answer

Solution code:
n = int(input())

def find(x):
    if par[x] < 0:
        return x
    else:
        par[x] = find(par[x])
        return par[x]
      
def unite(x,y):
    x = find(x)
    y = find(y)
    
    if x == y:
        return False
    else:
        #sizeの大きいほうがx
        if par[x] > par[y]:
            x,y = y,x
        par[x] += par[y]
        par[y] = x
        return True

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

par = [-1]*n
for i in range(n-1):
  u,v,w = [int(i) for i in input().split()]
  if w%2 == 0:
    unite(u-1,v-1)
    
for i in range(n):
  if i == 0:
    print(0)
  else:
    if same(0,i):
      print(0)
    else:
      print(1)

Submission status: Wrong Answer

Solution code:
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents =[-1 for i in range(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 unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return False
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        self.parents[x] += self.parents[y]
        self.parents[y] = x
        return True

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


n = int(input())
u = [0 for _ in range(n-1)]
v = [0 for _ in range(n-1)]
w = [0 for _ in range(n-1)]
uf = UnionFind(n)
for i in range(n-1):
    u[i], v[i], w[i] = map(int, input().split())
    if w[i]%2 == 0:
        uf.unite(u[i]-1, v[i]-1)
white = -1
for x in uf.parents:
    if white == -1:
        white = uf.find(x)
    if white == uf.find(x):
        print(0)
    else:
        print(1)

Submission status: Wrong Answer

Solution code:
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents =[-1 for i in range(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 unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return False
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        self.parents[x] += self.parents[y]
        self.parents[y] = x
        return True

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


n = int(input())
u = [0 for _ in range(n-1)]
v = [0 for _ in range(n-1)]
w = [0 for _ in range(n-1)]
uf = UnionFind(n)
for i in range(n-1):
    u[i], v[i], w[i] = map(int, input().split())
    if w[i]%2 == 0:
        uf.unite(u[i]-1, v[i]-1)
white = -1
for x in uf.parents:
    if white == -1:
        white = uf.find(x)
    if white == uf.find(x):
        print(0)

Submission status: Wrong Answer

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

n = int(input())
g = [[] for _ in range(n)]
color = [-1] * n
for _ 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])


def dfs(u, c):
    color[u] = c
    for v, w in g[u]:
        if color[v] != -1:
            continue
        if w % 2 == 0:
            dfs(v, c)
        else:
            c = 1 - c
            dfs(v, c)


dfs(0, 0)
for ans in color:
    print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
g = [[] for _ in range(n)]
color = [-1] * n


def dfs(u, c):
    global n, g, color
    color[u] = c
    for v, w in g[u]:
        if color[v] != -1:
            continue
        if w % 2 == 0:
            dfs(v, c)
        else:
            c = 1 - c
            dfs(v, c)


def main():
    import sys
    sys.setrecursionlimit(10**6)
    global n, g, color
    for _ 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])
    dfs(0, 0)
    for ans in color:
        print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def dfs(u, c):
    color[u] = c
    for v, w in g[u]:
        if color[v] != -1:
            continue
        if w % 2 == 0:
            dfs(v, c)
        else:
            c = 1 - c
            dfs(v, c)


def main():
    import sys
    sys.setrecursionlimit(10**6)
    global g, color
    n = int(input())
    g = [[] for _ in range(n)]
    for _ 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])
    color = [-1] * n
    dfs(0, 0)
    for ans in color:
        print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
a=int(input())
for i in range(a-1):
    List=[list(map(int,input().split()))]
lst=[]
for i in range(a):
    lst+=[0]
for i in List:
    if i[2]%2==1:
        if lst[i[0]-1]==0:
            lst[i[1]-1]=1
for i in lst:
    print(i)

Submission status: Wrong Answer

Solution code:
from collections import deque
N = int(input())
Graph = [[] for i in range(N)]
for i in range(N-1):
    u,v,w = map(int, input().split())
    Graph[u-1].append((v-1, w))
    Graph[v-1].append((u-1, w))
 
used = [0]*N
Q = deque([0])
used[0] = 1
ans = [0]*N
while Q:
  i = Q.popleft()
  j = ans[i]
  for uv, w in Graph[i]:
    if used[uv]:
      continue
    if w%2 != 0:
      ans[uv] = 1
    else:
      ans[uv] = j
    Q.append(uv)
    used[uv] = 1
 
print(*ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
N = int(input())
Graph = [[] for i in range(N)]
for i in range(N-1):
    u,v,w = map(int, input().split())
    Graph[u-1].append((v-1, w))
    Graph[v-1].append((u-1, w))

used = [0]*N
Q = deque([0])
used[0] = 1
ans = [0]*N
while Q:
  i = Q.popleft()
  j = ans[i]
  for uv, w in Graph[i]:
    print(uv,w)
    if used[uv]:
      continue
    if w%2 != 0:
      ans[uv] = j^1
    else:
      ans[uv] = j
    Q.append(uv)
    used[uv] = 1

print(*ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
node_manager = {i:0 for i in range(1, n+1)}

UVW = sorted([list(map(int, input().split())) for _ in range(n-1)])

for uvw in UVW:
  u,v,w = uvw
  if w%2==0:
    if node_manager[u] != node_manager[v]:
      node_manager[v] = node_manager[u]
    
  else:
    if node_manager[u] == node_manager[v]:
      node_manager[v] = 1-node_manager[u]
      
for v in node_manager.values():
  print(v)

Submission status: Wrong Answer

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

edges = [[] for _ in range(n)]
for u, v, w in uvw:
    u-=1
    v-=1
    edges[u].append([w, v])
    edges[v].append([w, u])

dist = [float('inf')] * n
color = [-1]*n
used = [False]*n
color[0] = '0'
q = []

dist[0] = 0
for e in edges[0]:
    heapq.heappush(q, e)
used[0] = True

while q:
    minedge = heapq.heappop(q)
    w, u = minedge
    if used[u]:
        continue
    dist[u] = w    
    used[u] = True
    for e in edges[v]:
        w, v = e
        if not used[v]:
            heapq.heappush(q, [dist[u]+w, v])
for i in range(n):
    if dist[i]%2 == 0:
        color[i] = '0'
    else:
        color[i] = '1'
print('\n'.join(color))

Submission status: Wrong Answer

Solution code:
n = int(input())
node_manager = {i:0 for i in range(1, n+1)}

UVW = sorted([list(map(int, input().split())) for _ in range(n-1)])

for uvw in UVW:
  u,v,w = uvw
  if w%2==0:
    node_manager[v] = node_manager[u]
  else:
    node_manager[v] = 1-node_manager[u]
    
for v in node_manager.values():
  print(v)

Submission status: Wrong Answer

Solution code:
n = int(input())
node_manager = {i:0 for i in range(1, n+1)}

UVW = sorted([list(map(int, input().split())) for _ in range(n-1)])

for uvw in UVW:
  u,v,w = uvw
  if w%2==0:
    node_manager[v] = node_manager[u]
  else:
    node_manager[v] = 1-node_manager[u]
    
print(node_manager)

Submission status: Wrong Answer

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

edges = [[] for _ in range(n)]
for u, v, w in uvw:
    u-=1
    v-=1
    edges[u].append([w, v])
    edges[v].append([w, u])

dist = [float('inf')] * n
color = [-1]*n
used = [False]*n
color[0] = '0'
q = []

dist[0] = 0
for e in edges[0]:
    heapq.heappush(q, e)
used[0] = True

while q:
    minedge = heapq.heappop(q)
    w, v = minedge
    if used[v]:
        continue
    dist[v] = w    
    used[v] = True
    for e in edges[v]:
        w, v = e
        if not used[v]:
            heapq.heappush(q, [dist[v]+w, v])
for i in range(n):
    if dist[i]%2 == 0:
        color[i] = '0'
    else:
        color[i] = '1'
print('\n'.join(color))

Submission status: Wrong Answer

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

def solve(N: int, u: "List[int]", v: "List[int]", w: "List[int]"):

    nodes = [1]*N
    e = sorted([(ui, vi, wi) for ui, vi, wi in zip(u, v, w)])
    for eu, ev, ew in e:
        if ew%2 == 1:
            nodes[ev-1] = nodes[eu-1]*-1
        else:
            nodes[ev-1] = nodes[eu-1]
            
    for n in nodes:
        print((n==1)*1)

    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    u = [int()] * (N - 1)  # type: "List[int]"
    v = [int()] * (N - 1)  # type: "List[int]"
    w = [int()] * (N - 1)  # type: "List[int]"
    for i in range(N - 1):
        u[i] = int(next(tokens))
        v[i] = int(next(tokens))
        w[i] = int(next(tokens))
    solve(N, u, v, w)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque

N = int(input())
lines = [[] for _ in range(N+1)]
for _ in range(N-1):
    u, v, w = map(int, input().split())
    lines[u].append([v, w%2])
    lines[v].append([u, w%2])
q = deque([1])
color = [0]*(N+1)
d = [0]*(N+1)
parent = [0]*(N+1)
while q:
    x = q.popleft()
    dis = d[x]
    for y, w in lines[x]:
        if y == parent[x]:
            continue
        if (dis+w)%2 == 1:
            color[y] = 1
        parent[y] = x
        d[y] = w%2
        q.append(y)
for i in range(1, N+1):
    print (color[i])

Submission status: Wrong Answer

Solution code:
n = int( input())

l = [[[] for _ in range( 2 )] for _ in range( n )]

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

ret = [1] + [0] * ( n - 1 )

def check( i ):
  k = l[ i ][ 0 ]
  for j in k:
    ret[ j ] = 1
    check( j )

def check2( i ):
  for j in l[ i ][ 1 ]:
    for m in l[ j ][ 1 ]:
      ret[ m ] = 1
      check2( m )
      
check( 0 )

for i in ret:
  print( i )

Submission status: Wrong Answer

Solution code:
n = int( input())

l = [[[] for _ in range( 2 )] for _ in range( n )]

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

ret = [1] + [0] * ( n - 1 )

def check( i ):
  k = l[ i ][ 0 ]
  for j in k:
    ret[ j ] = 1
    check( j )

check( 0 )

for i in ret:
  print( i )

Submission status: Wrong Answer

Solution code:
N=int(input())
table=[list(map(int,input().split())) for _ in range(N-1)]
x=sorted(table,key=lambda x:x[1])
x.sort(key=lambda x:x[0])
#print(x)
colist=[-1]*N
for i in range(N-1):
    if (colist[x[i][0]-1]==-1)and(colist[x[i][1]-1]==-1):
        if x[i][2]%2==0:
            colist[x[i][0] - 1]=0
            colist[x[i][1] - 1]=0
        else:
            colist[x[i][0] - 1]=0
            colist[x[i][1] - 1]=1
    elif colist[x[i][0]-1]==-1:#colist[x[i][1]-1]==1or0
        if x[i][2]%2==0:
            colist[x[i][0] - 1] = colist[x[i][1] - 1]
        else:
            colist[x[i][0] - 1] = (colist[x[i][1] - 1]+1)%2
    elif colist[x[i][1]-1]==-1:
        if x[i][2]%2==0:
            colist[x[i][1] - 1] = colist[x[i][0] - 1]
        else:
            colist[x[i][1] - 1] = (colist[x[i][0] - 1]+1)%2
    #else:
        #if colist[x[i][0] - 1] != colist[x[i][1] - 1]:
            #print("?")
    #print(colist)
    #input()
for i in range(N):
    print(colist[i])

Submission status: Wrong Answer

Solution code:
n = int(input()) 
s = []
for i in range(n-1):
    array = list(map(int, input().split()))
    s.append(array)
result = [0] * n
for i in s:
    if i[2] % 2 == 0:
        result[i[0] - 1],result[i[1] - 1] = 1,1
for i in result:
    print(i)

Submission status: Wrong Answer

Solution code:
class UnionFind:
    def __init__(self,n):
        super().__init__()
        self.par = [-1]*n
        self.rank = [0]*n
        self.tsize = [1]*n
    
    def root(self,x):
        if self.par[x] == -1:
            return x 
        else:
            self.par[x] = self.root(self.par[x])
            return self.par[x]

    def unite(self, x, y):
        x_r = self.root(x)
        y_r = self.root(y)
        
        if self.rank[x_r]>self.rank[y_r]:
            self.par[y_r] = x_r

        elif self.rank[x_r]<self.rank[y_r]:
            self.par[x_r] = y_r

        elif x_r != y_r:
            self.par[y_r] = x_r
            self.rank[x_r] += 1

        if x_r != y_r:
            size = self.tsize[x_r]+self.tsize[y_r]
            self.tsize[x_r] = size
            self.tsize[y_r] = size

    def isSame(self,x,y):
        return self.root(x) == self.root(y)

    def size(self,x):
        return self.tsize[self.root(x)]

def main():

    n= int(input())
    uf = UnionFind(2*(n+1))
    
    for _ in range(n-1):
      x,y,w = tuple([int(t)for t in input().split()])
      if w%2==0:
        uf.unite(2*x,2*y)
        uf.unite(2*x+1,2*y+1)
      else:
        uf.unite(2*x+1,2*y)
        uf.unite(2*x,2*y+1)

    w = uf.root(2)
    print(uf.par)
    for i in range(1,n+1):
      if w == uf.root(2*i):
        print(1)
      else:
        print(0)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N=int(input())
import sys
sys.setrecursionlimit(10**6)
#頂点の色
colors=[0]*(N)
L=[[] for _ in range(N)]
for i in range(N-1):
  #u,vを長さwでつなぐ
  u,v,w=map(int, input().split())
  L[u-1].append((v-1,w,i))
  L[v-1].append((u-1,w,i))
  
def dfs(start_node, pre_node,is_even, ):
  for next_node,l,i in L[start_node]:
    if next_node==pre_node:
      continue
    if is_even:
      if l%2==0:
        colors[next_node]=0
        is_even_next=True
      else:
        colors[next_node]=1
        is_even_next=False
    else:
      if l%2==0:
        colors[next_node]=1
        is_even_next=False
      else:
        colors[next_node]=0
        is_even_next=True
    dfs(next_node, start_node, is_even)
    
dfs(0,-1,True)
for color in colors:
  print(color)

Submission status: Wrong Answer

Solution code:
N=int(input())
import sys
sys.setrecursionlimit(10**6)
#頂点の色
colors=[0]*(N)
L=[[] for _ in range(N)]
for i in range(N-1):
  #u,vを長さwでつなぐ
  u,v,w=map(int, input().split())
  L[u-1].append((v-1,w,i))
  L[v-1].append((u-1,w,i))
  
def dfs(start_node, pre_node,is_even, ):
  for next_node,l,i in L[start_node]:
    if next_node==pre_node:
      continue
    if is_even:
      if l%2==0:
        colors[next_node]=0
      else:
        colors[next_node]=1
        is_even=False
    else:
      if l%2==0:
        colors[next_node]=1
      else:
        colors[next_node]=0
        is_even=True
    dfs(next_node, start_node, is_even)
    
dfs(0,-1,True)
for color in colors:
  print(color)

Submission status: Wrong Answer

Solution code:
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  n=I()
  ans=[-1]*n

  for _ in range(n-1):
    a,b,c=LI()
    a-=1
    b-=1

    if ans[a]!=-1 and ans[b]!=-1:
      continue

    if ans[a]==-1 and ans[b]==-1:
      if c%2==0:
        ans[a]=0
        ans[b]=0
      else:
        ans[a]=0
        ans[b]=1
    if ans[a]==0:
      if c%2==0:
        ans[b]=0
      else:
        ans[b]=1
    if ans[a]==1:
      if c%2==0:
        ans[b]=1
      else:
        ans[b]=0
    if ans[b]==0:
      if c%2==0:
        ans[a]=0
      else:
        ans[a]=1
    if ans[b]==1:
      if c%2==0:
        ans[a]=1
      else:
        ans[a]=0

  for x in ans:
    print(x)

main()
# print(main())

Submission status: Wrong Answer

Solution code:
from heapq import heappop, heappush

n = int(input())

color = [-1] * (n + 1)

edges = [[] for _ in range(n + 1)]

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


for i in range(1, n + 1):
    hq = []
    distance = [float("inf")] * (n + 1)

    if color[i] == -1:
        heappush(hq, [0, i, i])  # [distance, frm, to]
        distance[i] = 0
        color[i] = 0

    while len(hq) > 0:
        d, frm, to = heappop(hq)

        color[to] = color[frm] if d % 2 == 0 else 1 ^ (color[frm])

        for next_to, w in edges[to]:
            if color[next_to] == -1:
                heappush(hq, [distance[to] + w, to, next_to])

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