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:
import collections
def resolve():
    N = int(input())
    VVW = [list(map(int, input().split())) for _ in range(N-1)]
    adjs = [[] for _ in range(N)]
    for vvw in VVW:
        adjs[vvw[0]-1].append([vvw[1]-1, vvw[2]])
        adjs[vvw[1]-1].append([vvw[0]-1, vvw[2]])

    result = [None for _ in range(N)]
    deq = collections.deque([[0, 0]])
    
    while deq:
        node, cost = deq.popleft()
        for adj in adjs[node]:
            nextnode, nextcost = adj[0], adj[1]
            if result[nextnode] is not None:
                continue
            if nextcost%2:
                result[nextnode] = "0"
            else:
                result[nextnode] = "1"
            deq.append([nextnode, nextcost])
    
    [print(r) for r in result]



if '__main__' == __name__:
    resolve()

Submission status: Wrong Answer

Solution code:
import collections
def resolve():
    N = int(input())
    VVW = [list(map(int, input().split())) for _ in range(N-1)]
    adjs = [[] for _ in range(N)]
    for vvw in VVW:
        adjs[vvw[0]-1].append([vvw[1]-1, vvw[2]])
        adjs[vvw[1]-1].append([vvw[0]-1, vvw[2]])

    result = [None for _ in range(N)]
    deq = collections.deque([[0, 0]])
    result[0] = "0"
    
    while deq:
        node, cost = deq.popleft()
        for adj in adjs[node]:
            nextnode, nextcost = adj[0], adj[1]
            if result[nextnode] is not None:
                continue
            if cost%2 == nextcost%2:
                result[nextnode] = "0"
            else:
                result[nextnode] = "1"
            deq.append([nextnode, nextcost])
    
    [print(r) for r in result]



if '__main__' == __name__:
    resolve()

Submission status: Wrong Answer

Solution code:
n=int(input())
o=set()
e=set()
for i in range(n-1):
  a,b,c=map(int,input().split())
  if c%2==1:
    o.add((a,b))
  else:
    e.add((a,b))
o1=set()
o2=set()
for x in o:
  a,b=x
  if a in o1 or b in o2:
    o1.add(a)
    o2.add(b)
  elif a in o2 or b in o1:
    o1.add(b)
    o2.add(a)
  else:
    o1.add(b)
    o2.add(a)
for x in e:
  a,b=x
  if a in o1:
    o1.add(b)
  elif b in o1:
    o1.add(a)
  elif a in o2:
    o2.add(b)
  elif b in o2:
    o2.add(a)
l=[0]*n
for x in o2:
  l[x-1]=1
[print(x) for x in l]

Submission status: Wrong Answer

Solution code:
n=int(input())
e=set()
o=set()
for i in range(n-1):
  a,b,c=map(int,input().split())
  if c%2==1:
    if a in e or b in o:
      o.add(b)
      e.add(a)
    elif a in o or b in e:
      o.add(a)
      e.add(b)
    else: 
      o.add(a)
      e.add(b)
  else:
    if a not in e and a not in o and b not in e and b not in o:
      e.add(a)
      e.add(b)
l=[0]*n
for x in o:
  l[x-1]=1
[print(x) for x in l]

Submission status: Wrong Answer

Solution code:
from collections import deque

N = int(input())

edge = [[int(s) - 1 for s in input().split()] for _ in range(N-1)]
graph = [[] for _ in range(N)]
weight_odd = {}

for i, j, w in edge:
    graph[i].append(j)
    graph[j].append(i)
    weight_odd[(i, j)] = (w % 2 == 0)
    weight_odd[(j, i)] = (w % 2 == 0)

dq = deque([0])
visited = [False] * N
color = [0] * N
while(dq):
    search_obj = dq.pop()
    col1 = color[search_obj]
    col2 = 0 if col1 == 1 else 1
    for candidate_obj in graph[search_obj]:
        if not visited[candidate_obj]:
            if weight_odd[(candidate_obj, search_obj)]:
                color[candidate_obj] = col1
            else:
                color[candidate_obj] = col2
            dq.appendleft(candidate_obj)
            visited[candidate_obj] = True

for c in color:
    print(c)

Submission status: Wrong Answer

Solution code:
def main():
    from collections import deque, defaultdict
    n, *uvw = map(int, open(0).read().split())
    l = defaultdict(dict)
    g = [[] for _ in range(n + 1)]

    for u, v, w in zip(uvw[::3], uvw[1::3], uvw[2::3]):
        g[u].append(v)
        g[v].append(u)
        l[u].update({v: w % 2})

    l[0].update({1: 0})

    vis = [False] * (n + 1)
    col = [0] * (n + 1)

    q = deque()
    q.append([0, 1, 0])

    while q:
        i, j, k = q.popleft()
        if vis[j]:
            continue
        vis[j] = True
        c = k if l[min(i, j)][max(i, j)] == 0 else 1 - k
        col[j] = c

        for x in g[j]:
            q.appendleft([j, x, c])

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    from collections import defaultdict, deque

    n, *uvw = map(int, open(0).read().split())
    g = [[] for _ in range(n + 1)]
    d = defaultdict(dict)
    for u, v, w in zip(uvw[::3], uvw[1::3], uvw[2::3]):
        g[u].append(v)
        g[v].append(u)
        d[u].update({v: w % 2})

    d[0].update({1: 0})

    q = deque()
    q.append([0, 1, 0])

    vis = [False] * (n + 1)
    col = [0] * (n + 1)
    while q:
        i, j, k = q.popleft()
        if vis[j]:
            continue
        vis[j] = True
        c = k if d[min(i, j)][max(i, j)] == 0 else 1 - k
        col[j] = c

        for x in g[j]:
            q.appendleft([j, x, c])

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n, *uvw = map(int, open(0).read().split())
    c = [0] * (n + 1)
    fix = [False] * (n + 1)
    for u, v, w in zip(uvw[::3], uvw[1::3], uvw[2::3]):
        k = w % 2
        p, q = fix[u], fix[v]
        if k == 1:
            if not p and not q:
                c[v] = 1
            if p and not q:
                c[v] = 1 - c[u]
            if not p and q:
                c[u] = 1 - c[v]

        fix[u] = fix[v] = True

    ans = '\n'.join(map(str, c[1:]))
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    from collections import deque, defaultdict
    n, *uvw = map(int, open(0).read().split())
    l = defaultdict(dict)
    g = [[] for _ in range(n + 1)]

    for u, v, w in zip(uvw[::3], uvw[1::3], uvw[2::3]):
        g[u].append(v)
        l[u].update({v: w % 2})

    l[0].update({1: 0})

    vis = [False] * (n + 1)
    col = [0] * (n + 1)

    q = deque()
    q.append([0, 1, 0])

    while q:
        i, j, k = q.popleft()
        if vis[j]:
            continue
        vis[j] = True
        c = k if l[i][j] == 0 else 1 - k
        col[j] = c

        for x in g[j]:
            q.append([j, x, c])

    ans = '\n'.join(map(str, col[1:]))
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    from collections import deque, defaultdict
    n, *uvw = map(int, open(0).read().split())
    l = defaultdict(dict)
    g = [[] for _ in range(n + 1)]

    for u, v, w in zip(uvw[::3], uvw[1::3], uvw[2::3]):
        g[u].append(v)
        g[v].append(u)
        if u > v:
            u, v = v, u
        l[u].update({v: w})

    l[0].update({1: 0})

    vis = [False] * (n + 1)
    col = [0] * (n + 1)

    q = deque()
    q.append([0, 1])

    while q:
        i, j = q.popleft()
        if vis[j]:
            continue
        vis[j] = True
        col[j] = l[i][j] % 2

        for x in g[j]:
            q.append([min(j, x), max(j, x)])

    ans = '\n'.join(map(str, col[1:]))
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
class UnionFind():
    def __init__(self, n):
        self.n=n
        self.parents=[-1]*n # 親(uf.find()で経路圧縮して根)の番号。根の場合は-(そのグループの要素数)
    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 members(self, x):
    #   root = self.find(x)
    #   return [i for i in range(self.n) if self.find(i) == root]
    def unite(self,x,y):
        #要素x,yのグループを併合
        x,y=self.find(x),self.find(y)
        if x==y:return
        if self.parents[x]>self.parents[y]:#要素数の大きい方をxに
            x,y=y,x
        self.parents[x]+=self.parents[y]
        self.parents[y]=x #要素数が大きい方に併合
    def roots(self):
        #全ての根の要素のリスト
        return [i for i, x in enumerate(self.parents) if x<0]
    def group_count(self):
        #グループの数
        return len(self.roots())
    #def __str__(self):
    #   return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())
import sys
from functools import lru_cache
 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

def read():
  return int(readline())
def reads():
  return map(int, readline().split())
n=read()
uf=UnionFind(n)
for i in range(n-1):
  x,y,w=reads()
  if not w%2:
    uf.unite(x-1,y-1)
  else:
    try:
      uf.unite(hoge,y-1)
    except:pass
    hoge=x-1
ls=uf.roots()
for i in range(n):
  if uf.find(i) == ls[0]:
    print("0")
  else:
    print("1")

Submission status: Wrong Answer

Solution code:
class UnionFind():
    def __init__(self, n):
        self.n=n
        self.parents=[-1]*n # 親(uf.find()で経路圧縮して根)の番号。根の場合は-(そのグループの要素数)
    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 members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]
    def unite(self,x,y):
        #要素x,yのグループを併合
        x,y=self.find(x),self.find(y)
        if x==y:return
        if self.parents[x]>self.parents[y]:#要素数の大きい方をxに
            x,y=y,x
        self.parents[x]+=self.parents[y]
        self.parents[y]=x #要素数が大きい方に併合
    def roots(self):
        #全ての根の要素のリスト
        return [i for i, x in enumerate(self.parents) if x<0]
    def group_count(self):
        #グループの数
        return len(self.roots())
    #def __str__(self):
    #   return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())
import sys
from functools import lru_cache
 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

def read():
  return int(readline())
def reads():
  return map(int, readline().split())
n=read()
uf=UnionFind(n)
for i in range(n-1):
  x,y,w=reads()
  if not w%2:
    uf.unite(x-1,y-1)
  else:
    try:
      uf.unite(hoge,y-1)
    except:pass
    hoge=x-1
for i in range(n):
  print("{}".format(1 if i in uf.members(1) else 0))

Submission status: Wrong Answer

Solution code:
class UnionFind():
    def __init__(self, n):
        self.n=n
        self.parents=[-1]*n # 親(uf.find()で経路圧縮して根)の番号。根の場合は-(そのグループの要素数)
    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 members(self, x):
    #    root = self.find(x)
    #    return [i for i in range(self.n) if self.find(i) == root]
    def unite(self,x,y):
        #要素x,yのグループを併合
        x,y=self.find(x),self.find(y)
        if x==y:return
        if self.parents[x]>self.parents[y]:#要素数の大きい方をxに
            x,y=y,x
        self.parents[x]+=self.parents[y]
        self.parents[y]=x #要素数が大きい方に併合
    def roots(self):
        #全ての根の要素のリスト
        return [i for i, x in enumerate(self.parents) if x<0]
    def group_count(self):
        #グループの数
        return len(self.roots())
    #def __str__(self):
    #   return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

import sys
from functools import lru_cache
 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

def read():
  return int(readline())
def reads():
  return map(int, readline().split())
n=read()
uf=UnionFind(n)
dic=[0 for i in range(n)]
for i in range(n-1):
  u,v,w=reads()
  if  w%2:
    dic[v-1]=1-dic[u-1]
  else:
    dic[v-1]=dic[u-1]
for i in range(n):
  print("{}".format(dic[i]))

Submission status: Wrong Answer

Solution code:
from collections import deque
from sys import stdin
nii=lambda:map(int,stdin.readline().split())

n=int(input())

tree=[[] for i in range(n)]
for i in range(n-1):
  u,v,w=nii()
  u-=1
  v-=1
  tree[u].append([v,w])
  tree[v].append([u,w])

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

que=deque()
que.append(0)

while que:
  x=que.popleft()
  for i in tree[x]:
    nx=i[0]
    nw=i[1]
    if ans[nx]==-1:
      que.append(nx)
      if nw%2==0:
        ans[nx]=0
      else:
        ans[nx]=1

for i in ans:
  print(i)

Submission status: Wrong Answer

Solution code:
import sys

def tree_path():
    def walk(i,c):
        if d[i] == -1:
            d[i] = c
            for node, dist in path[i]:
                walk(node, c+dist)
    sys.setrecursionlimit(100007)
    n = int(input())
    path = [[] for _ in range(n+1)]
    for _ in range(n-1):
        u , v , w = map(int,input().split())
        path[u].append([v,w])
        path[v].append([v,w])
    
    d = [-1] * (n+1)
    walk(1,0)
    for item in d[1:]:
        print(item % 2)
tree_path()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from collections import deque
def bfs(now):
    plan = deque()
    plan.append(now)
    while plan:
        now = plan.popleft()
        color = COLOR[now]
        for bady, l in LINK[now]:
            if COLOR[bady] != -1:
                continue
            plan.append(bady)
            if l%2:
                COLOR[bady] = color - 1
            else:
                COLOR[bady] = color

if __name__ == '__main__':
    N = int(input())
    LINK = [[]*(N+1) for i in range(N+1)]
    # LINK = [deque()*(N+1) for i in range(N+1)]
    for i in range(N-1):
        a, b, l = map(int, input().split())
        LINK[a].append([b, l])
        LINK[b].append([a, l])
    # print(LINK)
    # print(LINK[1])
    # print(LINK[1][0])
    COLOR= [-1]*(N+1)
    COLOR[1] = 1
    bfs(1)
    for i in range(1,N+1):
        print(COLOR[i])

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from collections import deque
def bfs(now):
    plan = deque()
    plan.append(now)
    while plan:
        now = plan.popleft()
        for bady, l in LINK[now]:
            if COLOR[bady] != -1:
                continue
            plan.append(bady)
            if l%2:
                COLOR[bady] = COLOR[now]-1
            else:
                COLOR[bady] = COLOR[now]

if __name__ == '__main__':
    N = int(input())
    LINK = [[]*(N+1) for i in range(N+1)]
    # LINK = [deque()*(N+1) for i in range(N+1)]
    for i in range(N-1):
        a, b, l = map(int, input().split())
        LINK[a].append([b, l])
        LINK[b].append([a, l])
    # print(LINK)
    # print(LINK[1])
    # print(LINK[1][0])
    COLOR= [-1]*(N+1)
    COLOR[1] = 1
    bfs(1)
    for i in range(1,N+1):
        print(COLOR[i])

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from collections import deque
def bfs(now):
    plan = deque()
    plan.append(now)
    while plan:
        now = plan.popleft()
        for bady, l in LINK[now]:
            if COLOR[bady] != -1:
                continue
            plan.append(bady)
            if l%2:
                COLOR[bady] = 1
            else:
                COLOR[bady] = 0

if __name__ == '__main__':
    N = int(input())
    LINK = [[]*(N+1) for i in range(N+1)]
    # LINK = [deque()*(N+1) for i in range(N+1)]
    for i in range(N-1):
        a, b, l = map(int, input().split())
        LINK[a].append([b, l])
        LINK[b].append([a, l])
    # print(LINK)
    # print(LINK[1])
    # print(LINK[1][0])
    COLOR= [-1]*(N+1)
    bfs(1)
    for i in range(1,N+1):
        print(COLOR[i])

Submission status: Wrong Answer

Solution code:
def d_even_relation_bfs():
    from collections import deque
    N = int(input())
    Edges = [[int(i) - 1 for i in input().split()] for j in range(N - 1)]  # 0-indexed

    graph = [[] for _ in range(N)]
    for u, v, w in Edges:
        graph[u].append((v, w))
        graph[v].append((u, w))
    distance_from_root = [0] * N  # 根は頂点 0 とする

    queue = deque([(-1, 0, 0)])  # 親と現在の頂点番号，根から親までの距離
    while queue:
        parent, current, distance = queue.pop()
        distance_from_root[current] = distance
        for next_vertex, dist in graph[current]:
            if parent != next_vertex:  # 木を「逆戻り」しない頂点だけ調べる
                queue.appendleft((current, next_vertex, distance + dist))
    ans = (1 if d % 2 == 0 else 0 for d in distance_from_root)
    return ' '.join(map(str, ans))

print(d_even_relation_bfs())

Submission status: Wrong Answer

Solution code:
import sys, re
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees#, log2
from collections import deque, defaultdict, Counter
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop, heapify
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
 
N = INT()
uvw = [LIST() for _ in range(N-1)]

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

for u, v, w in uvw:
	graph[u-1].append((v-1, w))
	graph[v-1].append((u-1, w))

#print(graph)

color = [-1]*N
color[0] = 0

def DFS(node):
	for i, w in graph[node]:
		if color[i] == -1:
			if w%2:
				color[i] = color[node]
			else:
				color[i] = color[node]^1
			DFS(i)

DFS(0)

for x in color:
	print(x)

Submission status: Wrong Answer

Solution code:
import sys, re
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees#, log2
from collections import deque, defaultdict, Counter
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop, heapify
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
 
N = INT()
uvw = [LIST() for _ in range(N-1)]

graph = defaultdict(list)

for u, v, w in uvw:
	graph[u-1].append((v-1, w))
	graph[v-1].append((u-1, w))

#print(graph)

colors=[0]*N
colors[0]=1

def DFS(node):
	for x, w in graph[node]:
		if not colors[x]:
			if w%2:
				colors[x] = colors[node]
			else:
				colors[x] = colors[node]*(-1)
			DFS(x)

DFS(0)

#print(colors)

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

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
#%% for atcoder uniittest use
import sys
input= lambda: sys.stdin.readline().rstrip()
sys.setrecursionlimit(10**9)
def pin(type=int):return map(type,input().split())
def tupin(t=int):return tuple(pin(t))
def lispin(t=int):return list(pin(t))
#%%code
def resolve():
    N,=pin()
    graph={i+1:dict() for i in range(N)}
    print(graph)
    for i in range(N-1):
        u,v,w=pin()
        graph[u][v]=w%2
        graph[v][u]=w%2
    print(graph)

    ans={i+1:0 for i in range(N)}
    seen={i+1:0 for i in range(N)}
    #ans[1]=0 (always)
    stack=[(1,0)]#pos,color
    while (stack!=[]):
        p,c=stack.pop()
        if seen[p]==1:continue

        seen[p]=1
        ans[p]=c
        for k,v in graph[p].items():
            stack.append((k,(c+v)%2))
    print(ans)
    print(*ans.values(),sep="\n")
resolve()

Submission status: Wrong Answer

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

N = int(input())
G = [[] for i in range(N)]
for i in range(N - 1):
    a, b, w = map(int, input().split())
    G[a - 1].append([b, w])
    G[b - 1].append([a, w])
S = [-1] * N

def func(x, w, c):
    if w % 2 != 0:
        c = 1 - c
    S[x] = c
    for i in range(len(G[x])):
        x1 = G[x][i][0] - 1
        if S[x1] != -1:
            continue
        w1 = G[x][i][1]
        func(x1, w1, c)

func(G[0][0][0] - 1, G[0][0][1], 0)
print(S)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 6)
N = int(input())
uvw = [list(map(int, input().split())) for i in range(N - 1)]
R = [-1] * N
Q = [[] for i in range(N)]
for i in range(N - 1):
    Q[uvw[i][0] - 1].append([uvw[i][1], uvw[i][2]])
    Q[uvw[i][1] - 1].append([uvw[i][0], uvw[i][2]])

def func(i, s, d, l):
    
    if R[i] != -1:
        return

    if R[i] == -1:
        if l %2 == 0:
            R[i] = s
        else:
            R[i] = d
            s = d
            d = s
    for j in range(len(Q[i])):
        func(Q[i][j][0] - 1, s, d, Q[i][j][1])

func(uvw[0][0] - 1, 0, 1, 0)
for i in range(len(R)):
    print(R[i])

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(1000000)

n = int(input())
t = [[] for _ in range(n)]
color = [None for _ in range(n)]
visited = [False for _ in range(n)]


def dfs(n_num):
    visited[n_num] = True
    for i in t[n_num]:
        if visited[i[0]]:
            continue
        if i[1] % 2 == 0:
            color[i[0]] = color[n_num]
        else:
            color[i[0]] = abs(color[n_num] - 1)
        dfs(i[0])


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

print(t)

color[0] = 0
dfs(0)
for i in color:
    print(i)

Submission status: Wrong Answer

Solution code:
N = int(input())
g = [[] for _ in range(N+1)]
for _ in range(N-1):
    u,v,w = map(int,input().split())
    g[u].append((v,w))
from collections import deque
import numpy as np
queue = deque([1])
d = np.array([0]*(N+1))
d[1] = 1
#print(d)
#print(g[v])
while queue:
    v = queue.popleft()
    for i in g[v]:
        if not d[i[0]]:
            if i[1]%2==0:
                d[i[0]]=1
                queue.append(i[0])
                
if np.all(d[1:]>0):
    print(0)
    print(0)
    for i in range(3,N+1):
        print(1)
else:
    for i in range(1,N+1):
        print(d[i])

Submission status: Wrong Answer

Solution code:
N = int(input())
g = [[] for _ in range(N+1)]
for _ in range(N-1):
    u,v,w = map(int,input().split())
    g[u].append((v,w))
from collections import deque
import numpy as np
queue = deque([1])
d = np.array([0]*(N+1))
d[1] = 1
#print(d)
#print(g[v])
while queue:
    v = queue.popleft()
    for i in g[v]:
        if not d[i[0]]:
            if i[1]%2==0:
                d[i[0]]=1
                queue.append(i[0])
                
if np.all(d>0):
    print(0)
    print(0)
    for i in range(3,N+1):
        print(1)
else:
    for i in range(1,N+1):
        print(d[i])

Submission status: Wrong Answer

Solution code:
N = int(input())
g = [[] for _ in range(N+1)]
for _ in range(N-1):
    u,v,w = map(int,input().split())
    g[u].append((v,w))
    g[v].append((u,w))
from collections import deque
import numpy as np
queue = deque([1])
d = np.array([0]*(N+1))
d[1] = 1
#print(d)
#print(g[v])
while queue:
    v = queue.popleft()
    for i in g[v]:
        if not d[i[0]]:
            if i[1]%2==0:
                d[i[0]]=1
                queue.append(i[0])
                
if np.all(d>0):
    print(0)
    print(0)
    for i in range(3,N+1):
        print(1)
else:
    for i in range(1,N+1):
        print(d[i])

Submission status: Wrong Answer

Solution code:
N = int(input())
g = [[] for _ in range(N+1)]
for _ in range(N-1):
    u,v,w = map(int,input().split())
    g[u].append((v,w))
    g[v].append((u,w))
from collections import deque
import numpy as np
queue = deque([1])
d = np.array([0]*(N+1))
d[1] = 1
print(d)
#print(g[v])
while queue:
    v = queue.popleft()
    for i in g[v]:
        if not d[i[0]]:
            if i[1]%2==0:
                d[i[0]]=1
                queue.append(i[0])
                
if np.all(d>0):
    print(0)
    print(0)
    for i in range(3,N+1):
        print(1)
else:
    for i in range(1,N+1):
        print(d[i])

Submission status: Wrong Answer

Solution code:
N = int(input())
g = [[] for _ in range(N+1)]
for _ in range(N-1):
    u,v,w = map(int,input().split())
    g[u].append((v,w))
    g[v].append((u,w))
from collections import deque
queue = deque([1])
d = [0]*(N+1)
d[1] = 1
#print(d)
#print(g[v])
while queue:
    v = queue.popleft()
    for i in g[v]:
        if not d[i[0]]:
            if i[1]%2==0:
                d[i[0]]=1
                queue.append(i[0])
for i in range(1,N+1):
    print(d[i])

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
from itertools import accumulate
from itertools import permutations
from itertools import combinations
from collections import defaultdict
from collections import Counter
import fractions
import math
from collections import deque
from bisect import bisect_left
from bisect import insort_left
import itertools
from heapq import heapify
from heapq import heappop
from heapq import heappush
import heapq
import numpy as np
INF = float("inf")
#d = defaultdict(int)
#d = defaultdict(list)
#N = int(input())
#A = list(map(int,input().split()))
#S = list(input())
#S.remove("\n")
#N,M = map(int,input().split())
#S,T = map(str,input().split())
#A = [int(input()) for _ in range(N)]
#S = [input() for _ in range(N)]
#A = [list(map(int,input().split())) for _ in range(N)]
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))
color = [-1]*N
black = 0
white = 1
color[0] = white
def dfs(cur,parent):
    li = tree[cur]
    for chi,dis in li:
        if chi == parent:
            continue
        if dis % 2 == 0:
            color[chi] = color[cur]
            dfs(chi,cur)
        else:
            if color[cur] == black:
                color[chi] = white
            else:
                color[chi] = black
dfs(0,-1)
for i in color:
    print(i)

Submission status: Wrong Answer

Solution code:
n=int(input())
l=[]
ans=[0 for i in range(n)]
for i in range(n-1):
  u,v,w=[int(x) for x in input().rstrip().split()]
  if w%2==0:
    ans[u-1]=1
    ans[v-1]=1

for i in ans:
  print(i)

Submission status: Wrong Answer

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

ans = [0]*N
used = [0]*N
for i in range(N-1):
    u = T[i][0]
    v = T[i][1]
    w = T[i][2]
    if used[v] == 1:
        k = ans[v]
        if k == 1:
            if w == 1:
                ans[u] = 0
            else:
                ans[u] = 1
        else:
            if w == 1:
                ans[u] = 1
            else:
                ans[u] = 0
        used[u] = 1
    elif used[u] == 1:
        k = ans[u]
        if k == 1:
            if w == 1:
                ans[v] = 0
            else:
                ans[v] = 1
        else:
            if w == 1:
                ans[v] = 1
            else:
                ans[v] = 0
        used[v] = 1
    else:
        if w == 1:
            ans[v] = 1
        else:
            pass
        used[u] = 1
        used[v] = 1

for i in ans:
    print(i)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
class edge:
    def __init__(self, to, cost):
        self.to, self.cost = to, cost

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

    color = [1] * (N+1)

    def dfs(ver):
        for e in g[ver]:
            if color[e.to] == 1 and e.cost%2 == 0:
                color[e.to] = color[ver]
                dfs(e.to)
    color[1] = 0
    dfs(1)
    print(*color[1:], sep='\n')
    return

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#n=int(input())
#n,m=list(map(int,input().split()))
#a=list(map(int,input().split()))
input_list = lambda : list(map(int,input().split()))

from collections import Counter 
from collections import defaultdict
import itertools
import sys
sys.setrecursionlimit(10**6)

def sol(num,w,node,ans):
    now=node[num]
    
    for each in now:
        ne_n=each[0]
        ne_w=each[1]
        if ans[ne_n]!=-1:
            continue
        if ne_w==0:
            ans[ne_n]=ans[num]
        else:
            ans[ne_n]=1-ans[num]
        
        sol(ne_n,ne_w,node,ans)




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

ans=[-1 for _ in range(n)]
ans[0]=0
sol(0,0,node,ans)
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
tree = [[] for i in range(n)]
d_list = [-1]*n
d_list[0] = 0
stack = []
for i in range(n-1):
  u,v,w = map(int,input().split())
  tree[u-1].append((u-1,v-1,w))
for e in tree[0]:
  stack.append(e)
while len(stack) > 0:
  u,v, w = stack.pop()
  d_list[v] = d_list[u]+w
  for e in tree[v]:
    stack.append(e)
    
for v in d_list:
  if v % 2 == 0:
    print(0)
  else:
    print(1)

Submission status: Wrong Answer

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

sys.setrecursionlimit(10 ** 7)

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

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

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

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


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

    return True


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