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())
a = sorted([list(map(int, input().split())) for i in range(n - 1)])
color = [-1 for i in range(n)]
color[0] = 1
for i in a:
    u, v, w = i
    u -= 1
    v -= 1
    if w % 2 == 0:
        if color[u] == 0:
            color[v] = 0
        if color[u] == 1:
            color[v] = 1
        if color[v] == 0:
            color[u] = 0
        if color[v] == 1:
            color[u] = 1
    else:
        if color[u] == 0:
            color[v] = 1
        if color[u] == 1:
            color[v] = 0
        if color[v] == 0:
            color[u] = 1
        if color[v] == 1:
            color[u] = 0
for i in color:
    if i == -1:
        print(0)
    else:
        print(i)

Submission status: Wrong Answer

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

"""
import sys


class DisjointSet(object):
    def __init__(self, n):
        self.rank = [0] * n
        self.p = list(range(n))
        self.size = [1] * n

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

    def unite(self, x, y):
        self.link(self.findSet(x), self.findSet(y))

    def link(self, x, y):
        if self.rank[x] > self.rank[y]:
            self.size[self.p[x]] += self.size[self.p[y]]
            self.p[y] = self.findSet(x)
        else:
            self.size[self.p[y]] += self.size[self.p[x]]
            self.p[x] = self.findSet(y)
            if self.rank[x] == self.rank[y]:
                self.rank[y] += 1

    def findSet(self, x):
        if x != self.p[x]:
            self.p[x] = self.findSet(self.p[x])
        return self.p[x]


def solve(N, path):
    d = DisjointSet(N+1)
    for f, t, w in path:
        if w%2 == 0:
            d.unite(f, t)
    black = path[0][1]
    ans = []
    for n in range(1, N+1):
        if d.same(black, n):
            ans.append(0)
        else:
            ans.append(1)
    return ans


def main(args):
    N = int(input())
    path = [[int(i) for i in input().split()] for _ in range(N-1)]
    ans = solve(N, path)
    print(*ans, sep='\n')


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Wrong Answer

Solution code:
class WUF:
    __slots__ = ['ps', 'rs', 'ws']

    def __init__(self, n):
        self.ps = [i for i in range(n + 1)]
        self.rs = [0 for _ in range(n + 1)]
        self.ws = [0 for _ in range(n + 1)]

    def find(self, x):
        if self.ps[x] == x:
            return x
        else:
            y = self.find(self.ps[x])
            self.ws[x] += self.ws[self.ps[x]]
            self.ps[x] = y
            return y

    def union(self, x, y, w):
        rx = self.find(x)
        ry = self.find(y)
        if self.rs[rx] < self.rs[ry]:
            self.ps[rx] = ry
            self.ws[rx] = w - self.ws[x] + self.ws[y]
        else:
            self.ps[ry] = rx
            self.ws[ry] = -w - self.ws[y] + self.ws[x]
            if self.rs[rx] == self.rs[ry]:
                self.rs[rx] += 1

    def diff(self, x, y):
        return self.ws[x] - self.ws[y]


def task_d():
    n = int(input())
    uvw = (map(int, input().split()) for _ in range(n-1))
    
    wuf = WUF(n)
    for u, v, w in uvw:
        wuf.union(u, v, w)
    for i in range(1, n+1):
        print(wuf.diff(i, wuf.ps[i]) % 2)
task_d()

Submission status: Wrong Answer

Solution code:
n=int(input())
ans=[0]*(n+1)
s=set()
u,v,w=map(int,input().split())
s.add(u)
s.add(v)
if w%2==0:
  ans[u]=1
  ans[v]=1
elif w%2==1:
  ans[u]=1
  ans[v]=0
for i in range(n-2):
  u,v,w=map(int,input().split())
  if u in s:
    s.add(v)
    if w%2==0:
      ans[v]=ans[u]
    else:
      ans[v]=abs(ans[u]-1)
  elif v in s:
    s.add(u)
    if w%2==0:
      ans[u]=ans[v]
    else:
      ans[u]=abs(ans[v]-1)
for i in range(1,n+1):
  print(ans[i])

Submission status: Wrong Answer

Solution code:
n=int(input())
uvw=[list(map(int,input().split())) for _ in range(n-1)]
uvw=[[u-1,v-1,w] for u,v,w in uvw]
uvw=sorted(uvw,key=lambda x:x[0])

res=[-1 for _ in range(n)]
res[0]=0
for u,v,w in uvw:
    if w%2==0:
        if res[u]>=0:
            res[v]=res[u]
        elif res[v]>=0:
            res[u]=res[v]
    else:
        if res[u]==0:
            res[v]=1
        elif res[u]==1:
            res[v]=0
        elif res[v]==0:
            res[u]=1
        elif res[v]==1:
            res[u]=0
uvw=sorted(uvw,key=lambda x:-x[1])
for u,v,w in uvw:
    if w%2==0:
        if res[u]>=0:
            res[v]=res[u]
        elif res[v]>=0:
            res[u]=res[v]
    else:
        if res[u]==0:
            res[v]=1
        elif res[u]==1:
            res[v]=0
        elif res[v]==0:
            res[u]=1
        elif res[v]==1:
            res[u]=0
for ans in res:
    print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
ans = [2] * N
for i in range(1, N):
    u, v, w = map(int, input().split())
    if w % 2 == 0:
        ans[u-1] = 1
        ans[v-1] = 1
    else:
        if ans[u-1] == 2:
            ans[u-1] = 0
        if ans[v-1] == 2:
            ans[v-1] = 0
for l in ans:
    print(l)

Submission status: Wrong Answer

Solution code:
N = int(input())

BLACK = -1
GRAY = 0
WHITE = 1
nodes = [GRAY] * N
nodes[0] = BLACK
neighbors = [[] for _ in range(N)]

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

for u in range(N):
    for neis in neighbors:
        v, w = neis
        if nodes[u] == GRAY and nodes[v] == GRAY:
            next
        if nodes[u] == GRAY:
            if w % 2 == 0:
                nodes[u] = nodes[v]
            else:
                nodes[u] = -nodes[v]
        if nodes[v] == GRAY:
            if w % 2 == 0:
                nodes[v] = nodes[u]
            else:
                nodes[v] = -nodes[u]

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

Submission status: Wrong Answer

Solution code:
n = int(input())

edges = []
for i in range(n-1):
	edges.append(list(map(int, input().split())))
edges = sorted(edges, key=lambda x: x[0])

colors = [-1]*n
for edge in edges:
	u, v, w = edge
	if colors[u-1]==-1:
		colors[u-1]=0

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

for i in colors:
	print(i)

Submission status: Wrong Answer

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

n = int(input())
#uvw = [[int(i) for i in input().split()] for i in range(n)]

uvw = []
e = {}
for i in range(n-1):
    x = input().split()
    uvw.append([int(x[0])-1, int(x[1])-1, int(x[2])])
    e[i] = []
e[n-1] = []

w_mat = [[0 for i in range(n)] for j in range(n)]
for u,v,w in uvw:
    e[u].append(v)
    e[v].append(u)
    w_mat[u][v] = w
    w_mat[v][u] = w

res = [0]*n
def f(idx, pre_idx):
    if w_mat[idx][pre_idx]%2 == 1:
        res[idx] = int(not res[pre_idx])
    for i in e[idx]:
        if i != pre_idx:
            f(i, idx)

f(0,0)

for x in res:
    print(x)

Submission status: Wrong Answer

Solution code:
n = int(input())

Li = [0] * (n + 1)
uvs = []

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


for u, v in uvs:
    if Li[u] == 0:
        Li[v] = 1
    elif Li[v] == 0:
        Li[u] = 1

for item in Li[1:]:
    print(item)

Submission status: Wrong Answer

Solution code:
n=int(input())

uvw=[]
for i in range(n-1):
    uvw.append([int(j) for j in input().split()])

l=[1]*(n)

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

for i in l:
    print(i)

Submission status: Wrong Answer

Solution code:
import queue

n = int(input())

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

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

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

q = queue.Queue()
q.put(0)

while not q.empty():
    p = q.get()
    for i in range(len(a[p])):
        if a[p][i][1]==0 and b[a[p][i][0]]==1:
            q.put(a[p][i][0])
            b[a[p][i][0]]=0

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

Submission status: Wrong Answer

Solution code:
n = int(input().strip())
tree = {}

edges = {}

for i in range(1, n+1):
	tree[i] = []

colors = [0]*(n+1)

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

for parent in range(1, n+1):
	for child in tree[parent]:
		if edges[(parent, child)] % 2 == 0:
			colors[child] = colors[parent]
		else:
			colors[child] = 0 if colors[parent] else 1

print('\n'.join([str(i) for i in colors[1:]]))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n = int(input())
w = [[0,0,0]] * (n-1)
ans = [2] * n
for i in range(n-1):
    w[i] = input().split()
    w[i][0] = int(w[i][0])
    w[i][1] = int(w[i][1])
    w[i][2] = int(w[i][2])
    if ans[w[i][0] - 1] == 0:
        if w[i][2] % 2 == 0:
            ans[w[i][1] - 1] = 0
        else:
            ans[w[i][1] - 1] = 1
    elif ans[w[i][0] - 1] == 1:
        if w[i][2] % 2 == 0:
            ans[w[i][1] - 1] = 1
        else:
            ans[w[i][1] - 1] = 0
    elif ans[w[i][1] - 1] == 0:
        if w[i][2] % 2 == 0:
            ans[w[i][0] - 1] = 0
        else:
            ans[w[i][0] - 1] = 1
    elif ans[w[i][1] - 1] == 1:
        if w[i][2] % 2 == 0:
            ans[w[i][0] - 1] = 1
        else:
            ans[w[i][0] - 1] = 0
    else:
        ans[w[i][0] - 1] = 0
        if w[i][2] % 2 == 0:
            ans[w[i][1] - 1] = 0
        else:
            ans[w[i][1] - 1] = 1
for i in range(n):
    print(ans[i])

Submission status: Wrong Answer

Solution code:
#D問題
N=int(input())
UVW=[]
B=[]
W=[]
for i in range(N-1):
    u,v,w = map(int,input().split())
    if w%2 == 0:
        if u in B:
            B.append(v)
        elif u in W:
            W.append(v)
        elif v in B:
            B.append(u)
        elif v in W:
            W.append(u)
        else:
            B.append(u)
            B.append(v)
    else:
        if u in B:
            W.append(v)
        elif u in W:
            B.append(v)
        elif v in B:
            W.append(u)
        elif v in W:
            B.append(u)
        else:
            B.append(u)
            W.append(v)

for i in range(N):
    if i+1 in B:
        print(0)
    else:
        print(1)

Submission status: Wrong Answer

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits

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()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N = INT()
uvw = [LIST() for _ in range(N-1)]
color = [0] * N
search = list(range(N))
searched = []
color[0] = -1  # 0番目のノードを-1で初期化
for u, v, w in uvw:
	if w % 2 == 0:
		color[v-1] = color[u-1]
	else:
		color[v-1] = color[u-1] * -1

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

Submission status: Wrong Answer

Solution code:
n = int(input())
g = [[]]*(n+1)
k = [[]]*(n+1)
for i in range(n-1):
  u,v,w = map(int,input().split())
  if w%2 == 0:
    g[v].append(u)
    g[u].append(v)
  else:
    k[v].append(u)
    k[u].append(v)
 
#色
c = [-1]*(n+1)
c[1] = 0
q = [1]
while len(q) > 0:
  x = q.pop(0)
  for y in g[x]:
    if c[y] == -1:
      q.append(y)
      c[y] = c[x]
  for y in k[x]:
    if c[y] == -1:
      q.append(y)
      c[y] = (c[x]+1)%2
    
for i in range(1,n+1):
  print(c[i])

Submission status: Wrong Answer

Solution code:
N = int(input())

L = [1 for i in range(N)]

for i in range(N-1) :
    u,v,w = map(int,input().split())
    u -= 1
    v -= 1
    if w % 2 == 0 :
        L[u] = 0
        L[v] = 0
print(*L,sep="\n")

Submission status: Wrong Answer

Solution code:
N = int(input())

ans = [None for i in range(N)]

for i in range(N-1):
    u, v, w = map(int, input().split(" "))
    u -= 1
    v -= 1
    if w % 2 == 0:
        if ans[u] != None:
            ans[v] = ans[u]
        elif ans[v] != None:
            ans[u] = ans[v]
        else:
            ans[v] = True
            ans[u] = True
    else:
        if ans[u] != -1:
            ans[v] = not(ans[u])
        elif ans[v] != -1:
            ans[u] = not(ans[v])
        else:
            ans[u] = True
            ans[v] = False

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

Submission status: Wrong Answer

Solution code:
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

def another (n):
    if n== 0:
        return 1
    else:
        return 0
n = ni()
pli = [0] * n
for i in range(n-1):
    u, v, w = na()
    if w % 2 == 1:
        pli[v-1] = another(pli[u-1])
    else:
        pli[v - 1] = pli[u-1]

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

Submission status: Wrong Answer

Solution code:
N = int(input())

data = []

for i in range(N-1):
    a, b, c = map(int, input().split())

    data.append((a,b,c))
    data.append((b,a,c))

data.sort()

ans = [0] * (N+1)
went = [False] * (N+1)

went[0] = True
went[1] = True
before = 1

for i in range(2*(N-1)):
    a, b, c = data[i]
    if went[a]:
       if not went[b]:
           went[b] = True
           if c % 2 == 0:
               ans[b] = ans[a]
           else:
               ans[b] = 1 - ans[a]

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

print()

Submission status: Wrong Answer

Solution code:
n=int(input())
q=[list(map(int,input().split())) for i in range(n-1)]
class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n + 1)]
        self.rank = [0] * (n + 1)
        self.count = [1] * (n + 1)

    # find
    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    # union
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if self.rank[x] < self.rank[y]:
            self.par[x] = y
            self.count[y] += self.count[x]
        else:
            self.par[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            self.count[x] += self.count[y]

    # size
    def size(self, x):
        return self.count[self.par[x]]

    # same
    def issame(self, x, y):
        return self.find(x) == self.find(y)
uf = UnionFind(n)
for u,v,w in q:
    if w%2==0:
        uf.union(u-1,v-1)
for i in range(n):
    print(0 if uf.issame(0,i) else 1)

Submission status: Wrong Answer

Solution code:
def sol(inp=input):
    n=int(inp())
    pots=[None for _ in range(n)]
    for _ in range(n-1):
        u,v,w,=map(int,inp().split())
        if pots[u-1] is None and pots[v-1] is None:
            pots[u-1]=0
        if pots[v-1] is None:
            pots[v-1]=pots[u-1]+w
        else:
            pots[u-1]=pots[v-1]+w
    return [str(p%2) for p in pots]
for c in sol():
  print(c)

Submission status: Wrong Answer

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

ans=[1]*(n+1)
ans[l[0][0]]=-1
for i in range(n-1):
    if l[i][2]%2==0:
        ans[l[i][1]]=ans[l[i][0]]
    else:
        ans[l[i][1]]=(-1)*ans[l[i][0]]

for k in range(1,n+1):
    if ans[k]==-1:
        print(0)
    else:
        print(1)

Submission status: Wrong Answer

Solution code:

class WeightedUnionFind:
    def __init__(self, n):
        self.parent = [i for i in range(n + 1)]
        self.rank = [0 for _ in range(n + 1)]
        self.weight = [0 for _ in range(n + 1)]

    def find(self, x):
        if self.parent[x] == x:
            return x
        else:
            y = self.find(self.parent[x])
            self.weight[x] += self.weight[self.parent[x]]
            self.parent[x] = y
            return y

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

        if self.rank[rx] < self.rank[ry]:
            self.parent[rx] = ry
            self.weight[rx] = w - self.weight[x] + self.weight[y]
        else:
            self.parent[ry] = rx
            self.weight[ry] = -w - self.weight[y] + self.weight[x]
            if self.rank[rx] == self.rank[ry]:
                self.rank[rx] += 1

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

    def diff(self, x, y):
        return self.weight[x] - self.weight[y]

N = int(input())
import sys
graph = [[dict()] * N for _ in range(N)]
wu = WeightedUnionFind(N)
for i in range(N-1):
    u, v, w = map(int, sys.stdin.readline().split())
    wu.union(u, v, w)
wei = wu.weight
for i in range(1, N + 1):
    if i == 1:
        print(0)
    else:
        if abs(wu.diff(1, i)) % 2 == 0:
            print(0)
        else:
            print(1)

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict, deque

def main():
    input = sys.stdin.readline
    N = int(input())
    d = defaultdict(list)
    for _ in range(N-1):
        u, v, w = map(int, input().split())
        d[u-1].append((v-1, w))
        d[v-1].append((u-1, w))

    dist = [0] * N
    q = deque()
    q.append((0, 0))
    visited = set()

    while len(q) > 0:
        q_ = deque()
        while len(q) > 0:
            node1, cost1 = q.popleft()

            dist[node1] = cost1
            visited.add(node1)

            for node2, cost2 in d[node1]:
                if node2 in visited:
                    continue

                q_.append((node2, cost1 + cost2))

        q = q_

    print(dist)
    for d in dist:
        print(d % 2)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

def another (n):
    if n== 0:
        return 1
    else:
        return 0
n = ni()
pli = [0] * n
for i in range(n-1):
    u, v, w = na()
    if w % 2 == 1:
        pli[v-1] = another(pli[u-1])

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

Submission status: Wrong Answer

Solution code:
N = int(input())

sss = []
for _ in range(N - 1):
    u, v, w = [int(s) for s in input().split()]
    if u <= v:
        sss.append((u, v, w))
    else:
        sss.append((v, u, w))

sss = sorted(sss)

vec = [None for _ in range(N)]

for u, v, w in sss:
    u, v = u - 1, v - 1
    if vec[u] is None:
        vec[u] = False
    vec[v] = vec[u] ^ (w % 2 == 1)

for v in vec:
    print(1 if v else 0)

Submission status: Wrong Answer

Solution code:
n = int(input())
v = []
for _ in range(n-1):
    v.append(list(map(int, input().split())))
for _ in range(n):
    print('0')

Submission status: Wrong Answer

Solution code:
# coding: utf-8
class PotUnionFind:
    def __init__(self, size):
        self.parent = [-1 for i in range(size)]#非負なら親ノード，負ならグループの要素数
        self.diff_p = [0 for i in range(size)]#親ノードを基準としたポテンシャル

    def root(self, x): #root(x): xの根ノードを返す
        if self.parent[x] < 0:
            return x
        else:
            rx = self.root(self.parent[x]) #xをxの根rxに直接つなぎたい
            self.diff_p[x] += self.diff_p[self.parent[x]] #そのために，diff(rx,x)を計算
            self.parent[x] = rx #xをrxに直接つなげる
            return rx

    def merge(self, x, y, dxy): #ポテンシャル差の条件p(y)-p(x)=dxyでxとyのグループをまとめる
        dxy = self.diff_p[x] + dxy - self.diff_p[y] #dxyをdiff(rx,ry)で置き換え
        x = self.root(x) #rxを新たにxと名づける
        y = self.root(y) #ryを新たにyと名づける
        if x == y:
            return False
        if self.parent[x] > self.parent[y]: #xの要素数がyの要素数より「小さい」とき入れ替える
            x,y=y,x
            dxy = -dxy
        self.parent[x] += self.parent[y] #xの要素数を更新
        self.parent[y] = x #yをxにつなぐ
        self.diff_p[y] = dxy #yの相対ポテンシャルを更新
        return True

    def issame(self, x, y): #issame(x,y): xとyが同じグループにあるならTrue
        return self.root(x) == self.root(y)
        
    def diff(self,x,y): #diff(x,y): xを基準としたyのポテンシャルを返す 
        if self.root(x) == self.root(y): #この時点でxの親はroot(x)
            return self.diff_p[y] - self.diff_p[x]
        else:
            return None

    def size(self,x): #size(x): xのいるグループの要素数を返す
        return -self.parent[self.root(x)]

n = int(input())

T=PotUnionFind(n)
for _ in [0]*(n-1):
    u,v,w = [int(i) for i in input().split()]
    T.merge(u-1,v-1,w%2)

for i in T.diff_p:
    print(i%2)

Submission status: Wrong Answer

Solution code:
N = int(input())

relation_list = []
for i in range(N-1):
    relation_list.append(list(map(int, input().split())))
    
answer = [0] * N 

for rel in relation_list:
    if rel[2] % 2 == 1:
        answer[rel[1]-1] = (answer[rel[0]-1] + 1) % 2

for color in answer:
    print(color)

Submission status: Wrong Answer

Solution code:

class WeightedUnionFind:
    def __init__(self, n):
        self.parent = [i for i in range(n + 1)]
        self.rank = [0 for _ in range(n + 1)]
        self.weight = [0 for _ in range(n + 1)]

    def find(self, x):
        if self.parent[x] == x:
            return x
        else:
            y = self.find(self.parent[x])
            self.weight[x] += self.weight[self.parent[x]]
            self.parent[x] = y
            return y

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

        if self.rank[rx] < self.rank[ry]:
            self.parent[rx] = ry
            self.weight[rx] = w - self.weight[x] + self.weight[y]
        else:
            self.parent[ry] = rx
            self.weight[ry] = -w - self.weight[y] + self.weight[x]
            if self.rank[rx] == self.rank[ry]:
                self.rank[rx] += 1

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

    def diff(self, x, y):
        return self.weight[x] - self.weight[y]

N = int(input())

graph = [[dict()] * N for _ in range(N)]
wu = WeightedUnionFind(N)
for i in range(N-1):
    u, v, w = map(int, input().split())
    wu.union(u, v, w)
wei = wu.weight
for i in range(1, N + 1):
    if i == 1:
        print(0)
    else:
        if wu.diff(1, i) % 2 == 0:
            print(0)
        else:
            print(1)

Submission status: Wrong Answer

Solution code:
N = int(input())

relation_list = []
for i in range(N-1):
    relation_list.append(list(map(int, input().split())))
    
answer = [0] * N 

for rel in relation_list:
    if rel[2] % 2 == 1:
        answer[rel[1]-1] = (answer[rel[0]-1] + 1) % 2

print(answer)

Submission status: Wrong Answer

Solution code:
N = int(input())

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

sss = sorted(sss)

vec = [None for _ in range(N)]

for u, v, w in sss:
    u, v = u - 1, v - 1
    if vec[u] is None:
        vec[u] = False
    vec[v] = vec[u] ^ (w % 2 == 1)

for v in vec:
    print(1 if v else 0)

Submission status: Wrong Answer

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

print(*T, sep="\n")

Submission status: Wrong Answer

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

for u,v,w in q:
    if w%2==0:
        t[v-1],t[u-1]=1,1
for i in range(n):
    print(t[i])

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import collections


class Node(object):

  def __init__(self):
    self.edges = []
    self.color = None


def main():
  nodes = [Node() for _ in range(int(input()))]

  for _ in range(len(nodes) - 1):
    u, v, w = map(int, input().split())

    nodes[u - 1].edges.append((nodes[v - 1], w))
    nodes[v - 1].edges.append((nodes[u - 1], w))

  queue = collections.deque()

  nodes[0].color = 0
  queue.append(nodes[0])

  while len(queue) != 0:
    node = queue.pop()

    for e, w in node.edges:
      if e.color is not None:
        continue

      e.color = (w % 2) - node.color

      queue.append(e)

  for node in nodes:
    print(node.color)


if __name__ == '__main__':
  main()

Submission status: Wrong Answer

Solution code:
N=int(input())
color=[-1]*N
from collections import defaultdict
branch=defaultdict(dict)
check=[]
for i in range(N-1):
    u,v,w=map(int,input().split())
    branch[u-1][v-1]=w
    branch[v-1][u-1]=w
    if w%2==1:
        check+=[u-1,v-1]
if len(check)>0:
    color[check[-1]]=0
while len(check)>0:
    now=check.pop()
    for nex,w in branch[now].items():
        if w%2==1 and color[nex]==-1:
            color[nex]=(color[now]+1)%2
            check+=[nex]
color=[i if i!=-1 else 0 for i in color]
for i in range(N):
    print(color[i])

Submission status: Wrong Answer

Solution code:
n = int(input())
listN = [0]*n
index=0
while index < n-1:
    u, v, w = map(int, input().split())
    if w%2!=0:
        if listN[u-1]:
            listN[v-1] = 0
        else:
            listN[v-1] = 1
    index+=1
for color in listN:
    print(color)

Submission status: Wrong Answer

Solution code:
N = int(input())
edges = [list(map(int, input().split(" ")))
         for _ in range(N - 1)]
colors = [None] * N
for m, n, l in edges:
  if colors[m - 1] is None:
    colors[m - 1] = 0
  if l % 2 == 0:
    colors[n - 1] = colors[m - 1]
  else:
    colors[n - 1] = int(not colors[m - 1])

for c in colors:
  print(c)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque


fin = sys.stdin.readline
N = int(fin())
adj = [[] for _ in range(N)]
w = {}
for _ in range(N - 1):
    u, v, w_ = [int(elem) for elem in fin().split()]
    u -= 1
    v -= 1
    w[u, v] = w_
    w[v, u] = w_
    adj[u].append(v)
    adj[v].append(u)

que = deque()
que.append([0, 0])
color_of_node = [-1] * N

while que:
    node, dist = que.popleft()
    print(node, dist)
    if dist % 2 == 0:
        color_of_node[node] = 0
    else:
        color_of_node[node] = 1
    for neighbor in adj[node]:
        if color_of_node[neighbor] == -1:
            que.append([neighbor, dist + w[node, neighbor]])

print(*color_of_node, sep="\n")

Submission status: Wrong Answer

Solution code:
n = int(input())
graph = dict()
for i in range(1,n+1):
    graph[i] = []

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

arrival = [-1 for i in range(n+1)]
q = [1]
while len(q) != 0:
    arrival[q[0]] = 0
    for i in range(len(graph[q[0]])):
        if arrival[graph[q[0]][i][0]] == -1 and graph[q[0]][i][1] % 2 == 0:
            arrival[graph[q[0]][i][0]] == 0
            q.append(graph[q[0]][i][0])
    q.pop(0)
for i in range(1,n+1):
    if arrival[i] != 0:
        print("1")
    else:
        print("0")

Submission status: Wrong Answer

Solution code:
n=int(input())
uvw=[list(map(int,input().split())) for _ in range(n-1)]
uvw=sorted(uvw,key=lambda x:x[0])

res=[0 for _ in range(n)]
for u,v,w in uvw:
    u-=1
    v-=1
    if w%2==0:
        if res[u]>0:
            res[v]=res[u]
        elif res[v]>0:
            res[u]=res[v]
    else:
        if res[u]==0:
            res[v]=1
        elif res[u]==1:
            res[v]=0
        elif res[v]==0:
            res[u]=1
        elif res[v]==1:
            res[u]=0
for ans in res:
    print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[0]*n
b=[]
for i in range(n-1):b.append(list(map(int,input().split())))
b.sort()
for i,j,k in b:
  if k%2==0:a[j-1]=a[i-1]
  else:a[j-1]=-(a[i-1]-1)
for i in a:print(a)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(1000000)

def solve(N: int, u: "List[int]", v: "List[int]", w: "List[int]"):
    col = [-1 for _ in range(N+1)]
    G = [[] for _ in range(N+1)]
    for i in range(N-1):
        G[u[i]].append((v[i], w[i]))
        G[v[i]].append((u[i], w[i]))

    def dfs(p, c, cnt):
        if cnt == N:
            return
        for np, dis in G[p]:
            if col[np] == -1:
                cnt += 1
                if dis%2 == 0:
                    col[np] = c
                else:
                    c = 1 - c
                    col[np] = c
                dfs(np, c, cnt)

    col[1] = 0
    dfs(1, 0, 0)
    for elem in col[1:]:
        print(elem)


    return


# Generated by 1.1.4 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 defaultdict, deque
N = int( input())
d = defaultdict( int)
E = [ [] for _ in range(N)]
for _ in range(N-1):
    u, v, w = map( int, input().split())
    u, v = u-1, v-1
    E[u].append(v)
    E[v].append(u)
    d[(u,v)] = w%2
V = [-1]*N
V[0] = 0
q = deque([0])
while q:
    v = q.popleft()
    l = V[0]
    for t in E[v]:
        if V[t] == -1:
            V[t] = (l+d[(v,t)])%2
            q.append(t)
for i in range(N):
    print(V[i])