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())
v_list = []
for i in range(N-1):
    a = list(map(int, input().split(" ")))
    v_list.append(a)


#動的計画法
ans_list = [-1 for t in range(N)]

ans_list[v_list[0][0]-1] = 0
for u in range(N-1):
    if v_list[u][2] % 2 == 0:
        if ans_list[v_list[u][0]-1] == -1:
            ans_list[v_list[u][0]-1] = 0
        b = ans_list[v_list[u][0]-1]
        ans_list[v_list[u][1]-1] = b
    else:
        if ans_list[v_list[u][0]-1] == -1:
            ans_list[v_list[u][0]-1] = 0 
        if ans_list[v_list[u][0]-1] == 0:
            ans_list[v_list[u][1]-1] = 1
        elif ans_list[v_list[u][0]-1] == 1:
            ans_list[v_list[u][1]-1] = 0
for x in range(N):
    print(ans_list[x])

Submission status: Wrong Answer

Solution code:
class WeightedUnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n+1)]
        self.rank = [0] * (n+1)
        # 根への距離を管理
        self.weight = [0] * (n+1)

    # 検索
    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            y = self.find(self.par[x])
            # 親への重みを追加しながら根まで走査
            self.weight[x] += self.weight[self.par[x]]
            self.par[x] = y
            return y

    # 併合
    def union(self, x, y, w):
        rx = self.find(x)
        ry = self.find(y)
        # xの木の高さ < yの木の高さ
        if self.rank[rx] < self.rank[ry]:
            self.par[rx] = ry
            self.weight[rx] = w + self.weight[x] + self.weight[y]
        # xの木の高さ ≧ yの木の高さ
        else:
            self.par[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)

    # xからyへのコスト
    def diff(self, x, y):
        return self.weight[x] - self.weight[y]

n=int(input())
u=WeightedUnionFind(n)
for i in range(n-1):
    x,y,z=map(int,input().split())
    u.union(x,y,z)

'''
print(u.par)
print(u.rank)
print(u.weight)    
'''

ans=[0]
for i in range(2,n+1):
    
    if u.diff(u.find(1),i)%2==0:
        ans.append(0)
    else:
        ans.append(1)
for x in ans:
    print(x)

Submission status: Wrong Answer

Solution code:
import collections


class BellmanFord():
    def __init__(self):
        self.edges = []

    def add(self, u, v, d, directed=False):
        if directed is False:
            self.edges.append([u, v, d])
            self.edges.append([v, u, d])
        else:
            self.edges.append([u, v, d])

    def BellmanFord_search(self, s):
        d = collections.defaultdict(lambda: float('inf'))
        d[s] = 0
        numEdges = len(self.edges)
        while True:
            update = False
            for i in range(numEdges):
                e = self.edges[i]
                if d[e[0]] != float("inf") and d[e[1]] > d[e[0]] + e[2]:
                    d[e[1]] = d[e[0]] + e[2]
                    update = True
            if not update:
                break
        return d

N = int(input())
uvw = []
tree = BellmanFord()
for i in range(N-1):
    uvw.append(list(map(int,input().split())))
    tree.add(uvw[i][0],uvw[i][1],uvw[i][2])


a = tree.BellmanFord_search(1)
flg = False
for i in range(2,N+1):
    b = a[i] - a[i-1]
    if b % 2 == 0:
        print(0)
        if flg == False:
            print(0)
            flg = True
    else:
        print(1)
        flg = False

Submission status: Wrong Answer

Solution code:
# 入力が10**5とかになったときに100ms程度早い
import sys
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_matrix(H):
    '''
    H is number of rows
    '''
    return [list(map(int, read().split())) for _ in range(H)]


def read_map(H):
    '''
    H is number of rows
    文字列で与えられた盤面を読み取る用
    '''
    return [read()[:-1] for _ in range(H)]


def read_col(H, n_cols):
    '''
    H is number of rows
    n_cols is number of cols

    A列、B列が与えられるようなとき
    '''
    ret = [[] for _ in range(n_cols)]
    for _ in range(H):
        tmp = list(map(int, read().split()))
        for col in range(n_cols):
            ret[col].append(tmp[col])

    return ret


N = read_a_int()

from collections import defaultdict
node = defaultdict(lambda: set())  # キーで指定したノードが隣接するノードを記録しておく。
for _ in range(N-1):
    u, v, w = read_ints()
    if w % 2:
        # wじゃ奇数だったらTrue
        w = True
    else:
        w = False
    node[v-1].add((u-1, w))
    node[u-1].add((v-1, w))


# 0番目から探索していく
import numpy as np
ans = np.full(N, 0, dtype='int16')
# -1は未探索、1は黒、0は白、奇数の重みに当たるたびに反転
visited = np.full(N, False, dtype=bool)
from collections import deque
# 幅優先探索で白黒を記録していく
que = deque([(0, 0)])  # (nodeidx, 現在の色)
ans[0] = 0
visited[0] = True
while que:
    n, c = que.popleft()
    ans[n] = c

    for nex in node[n]:
        nex_node, nex_w = nex
        if visited[nex_node]:
            continue
        # 重みが奇数なら色を反転して探索に追加
        if nex_w:
            if c == 0:
                c = 1
            else:
                c = 0

        que.append((nex_node, c))
        visited[nex_node] = True

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

Submission status: Wrong Answer

Solution code:
import sys

class Graph():
    def __init__(self, n):
        self.even_edges = []
        self.odd_edges = []
        self.n = n
        self.coloring = [-1] * n

    def add_edge(self, a, b, cost):
        a -= 1
        b -= 1
        
        if cost % 2 != 0:
            self.odd_edges.append((a,b))
        else:
            self.even_edges.append((a,b))

    def paint(self):

        # paint odd-edge holders
        while self.odd_edges:
            a, b = self.odd_edges.pop()

            if self.coloring[a] != -1:
                self.coloring[b] = 1-a
            else:
                if self.coloring[b] != -1:
                    self.coloring[a] = 1-b
                else:
                    self.coloring[a] = 0
                    self.coloring[b] = 1

        # paint even-edges
        while self.even_edges:
            a, b = self.even_edges.pop()

            if self.coloring[a] != -1:
                self.coloring[b] = self.coloring[a]
            elif self.coloring[b] != -1:
                self.coloring[a] = self.coloring[b]
            else:
                self.coloring[a] = self.coloring[b] = 0

        # paint isolated vertices
        self._paint_isolated()

    def _paint_isolated(self):
        for i in range(self.n):
            if self.coloring[i] == -1:
                self.coloring[i] = 0


input = sys.stdin.readline

N = int(input())
g = Graph(N)

while True:
    s = input().rstrip()

    if s == '':
        break

    a,b,c = map(int, s.split())
    g.add_edge(a,b,c)

g.paint()

for x in g.coloring:
    print(x)

Submission status: Wrong Answer

Solution code:
N = int(input())
ans = [None] * N
A = [[int(i) for i in input().split()] for j in range(N-1)]
A= sorted(A, key=lambda a: (a[-1] % 2))
for u,v,w in A:
    if w % 2 == 0:
        if ans[u-1] is None and ans[v-1] is None:
            ans[u-1] = ans[v-1] = 1
        elif ans[u-1] is not None:
            ans[v-1] = ans[u-1]
        elif ans[v-1] is not None:
            ans[u-1] = ans[v-1]
    else:
        if ans[u-1] is None and ans[v-1] is None:
            ans[u-1] = 0
            ans[v-1] = 1
        elif ans[u-1] is not None:
            ans[v-1] = (ans[u-1] + 1) % 2
        elif ans[v-1] is not None:
            ans[u-1] = (ans[v-1] + 1) % 2
for a in ans:
    print(a)

Submission status: Wrong Answer

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

edge.sort(key=lambda x: x[1])
edge.sort(key=lambda x: x[0])
l[edge[0][0] - 1] = 0

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

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

Submission status: Wrong Answer

Solution code:
N = int(input())
wset = set()
bset = set()

for i in range(N-1):
    u,v,w = map(int,input().split())
    if w%2 != 0:
        if u in wset:   bset.add(v)
        elif u in bset: wset.add(v)
        elif v in wset: bset.add(u)
        elif v in bset: wset.add(u)
        else:
            wset.add(u)
            bset.add(v)
    else:
        if u in wset:   wset.add(v)
        elif u in bset: bset.add(v)
        elif v in wset: wset.add(u)
        elif v in bset: bset.add(u)

for i in range(1,N+1):
    if i in wset:   print(0)
    elif i in bset: print(1)
    else:   print(0)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import re
import bisect
from datetime import datetime as dt
N = input()
color = [-1 for i in range(N)]

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

for i in color:
    print str(i)

Submission status: Wrong Answer

Solution code:
n = int(input())
tree = ['0' for i in range(n)]

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

for i in tree:
    print(i)

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:
        tmp = answer[rel[1]-1]
        for ans in answer:
            if ans == tmp:
                ans = (ans + 1) % 2 
                
        answer[rel[0]-1] = (answer[rel[0]-1]+1) % 2
        
print(answer)

Submission status: Wrong Answer

Solution code:
def solve(string):
    n, *uvw = map(int, string.split())
    uvw = list(zip(uvw[::3], uvw[1::3], uvw[2::3]))
    table = [-i for i in range(n + 1)]

    def find(x):
        curr = x
        while table[curr] > 0:
            curr = table[curr]
        return curr

    for u, v, w in uvw:
        ru = find(u)
        rv = find(v)
        l, b = min(ru, rv), max(ru, rv)
        if w % 2 == 0:
            table[b] = l
    r1 = find(1)
    ans = [0] * (n + 1)
    for i in range(1, n + 1):
        ans[i] = "0" if find(i) == r1 else "1"
    return "\n".join(ans[1:])


if __name__ == '__main__':
    n = int(input())
    print(solve('{}\n'.format(n) + '\n'.join([input() for _ in range(n - 1)])))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

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

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

for u, v, w in edge:
    u -= 1; v -= 1
    ad[u].append((v,w))
    ad[v].append((u,w))

ans = [0]*N
ans[0] = 1

def dfs(v=0,pre=-1):
    for nv,w in ad[v]:
        if nv != pre:
            if w % 2 == 0:
                ans[nv] = 1
            dfs(nv,v)

dfs()

for a in ans:
    print(a)

Submission status: Wrong Answer

Solution code:
n=int(input())
even=tuple()
for i in range(n-1):
    u,v,w=map(int,input().split())
    if w%2==0:
        even+=(u,v)
for i in range(1,n+1):
    if i in even:
        print(0)
    else:
        print(1)

Submission status: Wrong Answer

Solution code:
N = int(input())

dict = {}
for i in range(N-1):
    u, v, w = map(int, input().split())
    if w % 2 == 1:
        if u not in dict:
            dict[u] = [v]
        else:
            dict[u].append(v)

arr = [-1] * (N+1)
for i in range(1, N+1):
    if arr[i] == -1:
        arr[i] = 0
        if i in dict:
            v = dict[i]
            for j in v:
                arr[j] = 1
    elif arr[i] == 0:
        if i in dict:
            v = dict[i]
            for j in v:
                arr[j] = 1
    elif arr[i] == 1:
        if i in dict:
            v = dict[i]
            for j in v:
                arr[j] = 0

for i in arr[1:]:
    print(i)

Submission status: Wrong Answer

Solution code:
class Edge:
    def __init__(self, v1, v2, weight):
        self.v1 = min(v1, v2)
        self.v2 = max(v1, v2)
        self.weight = weight
        self.isOdd = weight % 2 > 0
        self.isEven = not self.isOdd

    def __lt__(self, other):
        return (self.v1, self.v2) < (other.v1, other.v2)

White = True
Black = False

N = int(input())
edges = {}
colored_v = {}
uncolored_v = [x for x in range(1, N+1)]

for i in range(1, N):
    v1, v2, w = list(map(int, input().split()))
    edges[i] = Edge(v1, v2, w)


for _ in range(100):
    if len(uncolored_v) == 0:
        break
    colored_v[uncolored_v[0]] = White
    uncolored_v.remove(uncolored_v[0])
    for ek,ev in sorted(edges.items()):
        if ev.v1 not in colored_v and ev.v2 not in colored_v:
            continue
        if ev.v1 in colored_v and ev.v2 in colored_v:
            continue
        if ev.v1 in colored_v:
            s, e = ev.v1, ev.v2
        else:
            s, e = ev.v2, ev.v1
        if ev.isEven:
            colored_v[e] = colored_v[s]
            uncolored_v.remove(e)
        if ev.isOdd:
            colored_v[e] = not colored_v[s]
            uncolored_v.remove(e)




for v in sorted(colored_v.items()):
    print(0 if v[1] else 1)

Submission status: Wrong Answer

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

i  = lambda: int(input())
mi = lambda: map(int, input().split())

N = i()
white = []
black = []
ans = [0] * (N+1)

for _ in range(N-1):
    u, v, w = mi()
    if w % 2 == 0:
        if u in white or v in white:
            white += [u,v]
        else:
            black += [u,v]
            ans[u] = 1
            ans[v] = 1
    else:
        if u in white:
            black += [v]
            ans[v] = 1
        elif u in black:
            white += [v]
        elif v in white:
            black += [u]
            ans[u] = 1
        elif v in black:
            white += [u]
        else:
            white += [u]
            black += [v]
            ans[v] = 1

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

Submission status: Wrong Answer

Solution code:
import copy
import sys

sys.setrecursionlimit(10000000)
N=int(input())
data=[[int(i) for i in input().split()] for _ in range(N-1)]
state = [-1 for _ in range(N+1)]
def search(depth,copyed_state):
    if depth==len(data):
        # print('end')
        return copyed_state
    kl = data[depth]
    anss=[]
    # print('depth:{} state:{}'.format(depth,copyed_state))
    if kl[2]%2 ==0:
        if copyed_state[kl[0]]==-1 and copyed_state[kl[1]]==-1:
            new_lizeros = copy.copy(copyed_state)
            new_lizeros[kl[0]] = 0
            new_lizeros[kl[1]] = 0
            anss.append(search(depth+1,new_lizeros))
            new_liones = copy.copy(copyed_state)
            new_liones[kl[0]] = 1
            new_liones[kl[1]] = 1
            anss.append(search(depth+1,new_liones))
        elif copyed_state[kl[0]]==0 and copyed_state[kl[1]]==-1:
            one_lizeros = copy.copy(copyed_state)
            one_lizeros[kl[1]] = 0
            anss.append(search(depth+1,one_lizeros))
        elif copyed_state[kl[0]]==1 and copyed_state[kl[1]]==-1:
            one_lizeros = copy.copy(copyed_state)
            one_lizeros[kl[1]] = 1
            anss.append(search(depth+1,one_lizeros))
        elif copyed_state[kl[0]]==-1 and copyed_state[kl[1]]==0:
            one_lizeros = copy.copy(copyed_state)
            one_lizeros[kl[0]] = 0
            anss.append(search(depth+1,one_lizeros))
        elif copyed_state[kl[0]]==-1 and copyed_state[kl[1]]==1:
            one_lizeros = copy.copy(copyed_state)
            one_lizeros[kl[0]] = 1
            anss.append(search(depth+1,one_lizeros))
    for i in anss:
        if len(i)!=0:
            return i
    if copyed_state[kl[0]] == -1 and copyed_state[kl[1]] == -1:
        new_lizeros2 = copy.copy(copyed_state)
        new_lizeros2[kl[0]] = 0
        new_lizeros2[kl[1]] = 1
        anss.append(search(depth + 1, new_lizeros2))
        new_liones2 = copy.copy(copyed_state)
        new_liones2[kl[0]] = 1
        new_liones2[kl[1]] = 0
        anss.append(search(depth + 1, new_liones2))
    elif copyed_state[kl[0]] == 0 and copyed_state[kl[1]] == -1:
        one_lizeros2 = copy.copy(copyed_state)
        one_lizeros2[kl[1]] = 1
        anss.append(search(depth + 1, one_lizeros2))
    elif copyed_state[kl[0]] == 1 and copyed_state[kl[1]] == -1:
        one_lizeros2 = copy.copy(copyed_state)
        one_lizeros2[kl[1]] = 0
        anss.append(search(depth + 1, one_lizeros2))
    elif copyed_state[kl[0]] == -1 and copyed_state[kl[1]] == 0:
        one_lizeros2 = copy.copy(copyed_state)
        one_lizeros2[kl[0]] = 1
        anss.append(search(depth + 1, one_lizeros2))
    elif copyed_state[kl[0]] == -1 and copyed_state[kl[1]] == 1:
        one_lizeros2 = copy.copy(copyed_state)
        one_lizeros2[kl[0]] = 0
        anss.append(search(depth + 1, one_lizeros2))
    else: # dottimo haittetara
        sktyu = copy.copy(copyed_state)
        anss.append(search(depth + 1, sktyu))

    for i in anss:
        if len(i)!=0:
            return i
ans=search(0,state)
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 i in range(N-1)]

answer = [0 for _ in range(N)]
odd_number = {}
for i in uvw:
  if i[2] % 2 != 0:
    if i[0] < i[1]:
      odd_number.setdefault(i[0], []).append(i[1])
    else:
      odd_number.setdefault(i[1], []).append(i[0])
    
for i in range(N):
  if odd_number.get(i+1) is None:
    pass
  else:
    for j in odd_number[i+1]:
      if answer[i] == answer[j-1]:
        if answer[j-1] == 0:
          answer[j-1] = 1
        else:
          answer[j-1] = 0
          
print(answer)

Submission status: Wrong Answer

Solution code:
N = int(input())
u = [0]*N
v = [0]*N
w = [0]*N
for n in range(N-1):
    u[n], v[n], w[n] = map(int, input().split())
color = [0]*N
color[0] = 1
x = 0
next_x =[]
position = [0]*N
while position == [1]*N:
    for n in range(N):
        if u[n] == x and position[v[n]] == 0:
            if w[n]//2 == 0:
                color[v[n]] = color[x]
            next_x.append(v[n])
        if v[n] == x and position[u[n]] == 0:
            if w[n]//2 == 0:
                color[u[n]] = color[x]
            next_x.append(u[n])
    x = next_x.pop(-1)
    position[x] = 1
    continue
    
for n in range(N):
        if u[n] == x and position[v[n]] == 0:
            if w[n]//2 == 0:
                color[v[n]] = color[x]
        if v[n] == x and position[u[n]] == 0:
            if w[n]//2 == 0:
                color[u[n]] = color[x]

for n in range(N):
    print(color[n])

Submission status: Wrong Answer

Solution code:
def solve():
    N = int(raw_input())
    ret = [0] * N
    changed = [False] * N
    for i in range(N - 1):
        ui,vi,wi = [int(_) for _ in raw_input().split()]
        if not changed[vi - 1]:
            if wi % 2 == 0:
                ret[vi - 1] = ret[ui - 1]
            else:
                ret[vi - 1] = (1 - ret[ui - 1])
            changed[vi - 1] = True
        else:
            if wi % 2 == 0:
                ret[ui - 1] = ret[vi - 1]
            else:
                ret[ui - 1] = (1 - ret[vi - 1])
            changed[ui - 1] = True
    for i in range(N):
        print ret[i]

if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:


def main():
    N = int(input())
    a = [0 for _ in range(N+1)]
    s = []
    for i in range(N-1):
        s.append(list(map(int, input().split())))
    s = sorted(s)
    # print(s)
    for y in s:
        u, v, w = tuple(y)
        if w % 2 == 0:
            if a[u] == 0:
                if a[v] == 0:
                    a[u] = 1
                    a[v] = 1
                else:
                    a[u] = a[v]
            else:
                a[v] = a[u]
        else:
            if a[u] == 0:
                if a[v] == 0:
                    a[u] = 1
                    a[v] = -1
                else:
                    a[u] = -a[v]
            else:
                a[v] = -a[u]
    for A in a[1:]:
        if A < 0:
            print(0)
        else:
            print(1)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())

lst = [0] * (N+1)

for i in range(N-1):
    u, v, w = map(int, input().split())
    if w%2 == 0:
#        print ('A')
        if lst[u] == 0 and lst [v] == 0:
            lst[u] = 1
            lst[v] = 1
        elif lst[u] != 0:
            lst[v] = lst[u]
        elif lst[v] != 0:
            lst[u] = lst[v]
#    print (lst)

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

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(100000)
n = int(input())
d = [[] for i in range(n)]
for i in range(n-1):
	a,b,x = map(int,input().split())
	a -= 1
	b -= 1
	x = x%2
	d[a].append([b,x])
	d[b].append([a,x])
color = [0]*n
used = set()
def dfs(v):
	for e,l in d[v]:
		if e not in used:
			used.add(e)
			if l == 1:
				color[e] = 1
			dfs(e)
dfs(0)
for c in color:
	print(c)

Submission status: Wrong Answer

Solution code:
n = int(input())
u_list = []
v_list = []
w_list = []
colors = [2] * (n + 1)

for _ in range(n - 1):
    u, v, w = [int(i) for i in input().split()]
    u_list.append(u)
    v_list.append(v)
    w_list.append(w)

u_sorted_key = sorted(range(len(u_list)), key=lambda k: u_list[k])

for key in u_sorted_key:
    u = u_list[key]
    v = v_list[key]
    w = w_list[key]

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

for index, c in enumerate(colors):
    if index == 0:
        continue

    if c == 2:
        print(1)
    else:
        print(c)

Submission status: Wrong Answer

Solution code:
def task_d():
    n = 5
    uvw = [
        (2, 5, 1),
        (2, 3, 10),
        (1, 3, 8),
        (3, 4, 2)
    ]

    g = [[None] * (n + 1) for _ in range(n + 1)]
    h = [None] * (n + 1)

    for u, v, w in uvw:
        g[u][v] = w % 2

    for i in range(1, n + 1):
        if h[i] is None:
            h[i] = 0
        for j in range(i + 1, n + 1):
            if g[i][j] is not None:
                if h[j] is not None:
                    h[i] = (g[i][j] - h[j]) % 2
                else:
                    h[j] = (g[i][j] + h[i]) % 2
    for i in h[1:]:
        print(i)

                
    
task_d()

Submission status: Wrong Answer

Solution code:
from collections import defaultdict as df
from collections import deque
def bfs(k):
    s=deque()
    s.append(k)
    while(len(s)>0):
        #print(s)
        f=s.popleft()
        for j in d[f]:
            
            if vis[j]==0:
                if vis[f]=='R':
                    vis[j]='B'
                    s.append(j)
                else:
                    vis[j]='R'
                    s.append(j)
            elif vis[j]=='R' and vis[f]=='B':
                pass
            elif vis[j]=='B'and vis[f]=='R':
                pass
            else:
                return 0
    return 1
n=int(input())
d=df(list)
for i in range(n-1):
    a,b,c=list(map(int,input().split()))
    d[a].append(b)
    d[b].append(a)
vis=[0]*(n+1)
vis[1]='R'
res=bfs(1)
if res==1:
    for j in range(1,n+1):
        if vis[j]=='R':
            print('0')
        else:
            print('1')

Submission status: Wrong Answer

Solution code:
n = int(input().strip())

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

edges = []

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

edges.sort(key=lambda x:x[1])
for edge in edges:
	u, v, w = edge
	if w % 2:
		# if odd, different color is mandatory
		if color[u] == -1 and color[v] == -1:
			color[u] = 0
			color[v] = 1
		elif color[u] == -1:
			color[u] = 1-color[v]
		elif color[v] == -1:
			color[v] = 1-color[u]
		else:
			if color[u] == color[v]:
				color[u] = 1-color[v]

print('\n'.join(['0' if i == -1 else str(i) for i in color[1:]]))

Submission status: Wrong Answer

Solution code:
n = int(input())
tree = []
while 1:
  try:
    tree.append(list(int(i) for i in input().split()))
  except EOFError:
    break
    
for i in range(n):
  print("0")

Submission status: Wrong Answer

Solution code:
n=int(input())
aa={}
for _ in range(n-1):
    u,v,w=map(int,input().split())
    if w % 2:
        aa.setdefault(u, set()).add(v)
bb = [-1] * n
for u in aa:
    if bb[u-1] < 0:
        bb[u-1] = 0
    for v in aa[u]:
        bb[v-1] = 1-bb[u-1]
for b in bb:
    print(1 if b == 1 else 0)

Submission status: Wrong Answer

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

a = [0] * n

for i in range(n - 1):
    if tree[i][2] % 2 == 1:
        u = tree[i][0]
        v = tree[i][1]
        if a[u - 1] == 0:
            a[v - 1] = 1

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

Submission status: Wrong Answer

Solution code:


def main():
    N = int(input())
    a = [0 for _ in range(N+1)]
    s = []
    for i in range(N-1):
        s.append(list(map(int, input().split())))
    s = sorted(s)
    print(s)
    for y in s:
        u, v, w = tuple(y)
        if w % 2 == 0:
            if a[u] == 0:
                if a[v] == 0:
                    a[u] = 1
                    a[v] = 1
                else:
                    a[u] = a[v]
            else:
                a[v] = a[u]
        else:
            if a[u] == 0:
                if a[v] == 0:
                    a[u] = 1
                    a[v] = -1
                else:
                    a[u] = -a[v]
            else:
                a[v] = -a[u]
    for A in a[1:]:
        if A < 0:
            print(0)
        else:
            print(1)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())

edges = []

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

root = [-1]*N

for edge in edges:
  u, v, w = edge
  if w % 2 == 0:
    if root[u] == -1 and root[v] == -1:
      root[u] = 0
      root[v] = 0
    elif root[v] == -1:
      root[v] = root[u]
    elif root[u] == -1:
      root[u] == root[v]
  else:
    if root[u] == -1 and root[v] == -1:
      root[u] = 0
      root[v] = 1
    elif root[v] == -1:
      root[v] = 1- root[u]
    elif root[u] == -1:
      root[u] = 1- root[v]
root = [r if r >= 0 else 0 for r in root]
for i in range(N):
  print(root[i])

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline

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


lw = [[] for i in range(n+1)]
for path in p:
    lw[path[0]].append((path[1], path[2]))
    lw[path[1]].append((path[0], path[2]))

iro_list = [0] * n
lw = lw[1:]
for i, each_node in enumerate(lw):
    for each_node_list in each_node:
        if each_node_list[1] % 2 == 0:
            iro_list[each_node_list[0]-1] = 1 - iro_list[i]

for each_iro in iro_list:
    print(each_iro)

Submission status: Wrong Answer

Solution code:
N = int(input())
uvw = [tuple(input().split()) for i in range(N-1)]
ans = []
chflag = []
for i in range(N):
  ans.append('0')
  chflag.append('0')
cnt = 0
for u, v, w in uvw:
  if(cnt == 0):
    cnt = 1
    ans[int(u)-1] = '1'
    chflag[int(u)-1] == '1'
    if(int(w) % 2 == 0):
      ans[int(v)-1] = '1'
      chflag[int(v)-1] = '1'
  else:
    if(chflag[int(u)-1] == '1' and chflag[int(v)-1] == '1'):
      continue
    elif(chflag[int(u)-1] == '1'):
      if(int(w) % 2 == 0):
        ans[int(v)-1] = ans[int(u)-1] 
        chflag[int(v)-1] = '1'
      else:
        if(ans[int(u)-1] == '1'):
          ans[int(v)-1] == '0'
          chflag[int(v)-1] = '1'
        else:
          ans[int(v)-1] == '1'
          chflag[int(v)-1] = '1'
    elif(chflag[int(v)-1] == '1'):
      if(int(w) % 2 == 0):
        ans[int(u)-1] = ans[int(v)-1] 
        chflag[int(u)-1] = '1'
      else:
        if(ans[int(v)-1] == '1'):
          ans[int(u)-1] == '0'
          chflag[int(u)-1] = '1'
        else:
          ans[int(u)-1] == '1'
          chflag[int(u)-1] = '1'
for i in range(N):
  print(ans[i])