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 sys
sys.setrecursionlimit(10**7)

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

e =[[] for _ in range(n)]
color = [-1]*n

dw = {}
for u,v,w in uvw:
  e[u-1].append(v-1)
  e[v-1].append(u-1)
  dw[(u-1,v-1)] = dw[(v-1,u-1)] = w

def dfs(i,c)  :
  color[i] = c
  for ei in e[i]:
    if color[ei] != -1:
      continue
      dfs(ei, c+dw[(i,ei)])

dfs(0,0)   

for c in color:
  print(c%2)

Submission status: Wrong Answer

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

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

e =[[] for _ in range(n)]
color = [-1]*n

dw = {}
for u,v,w in uvw:
  e[u-1].append(v-1)
  e[v-1].append(u-1)
  dw[(u-1,v-1)] = dw[(u-1,v-1)] = w

def dfs(i,c)  :
  color[i] = c
  for ei in e[i]:
    if color[ei] != -1:
      continue
      dfs(ei, c+dw[(i,ei)])

dfs(0,0)   

for c in color:
  print(c%2)

Submission status: Wrong Answer

Solution code:
import sys
import collections


def solve():
    input = sys.stdin.readline
    mod = 10 ** 9 + 7
    tmd = collections.defaultdict(list)
    n = int(input().rstrip('\n'))
    d = collections.defaultdict(int)
    for i in range(n-1):
        tma, tmb, cost = list(map(int, input().rstrip('\n').split()))
        tmd[tma-1] += [tmb-1]
        tmd[tmb-1] += [tma-1]
        d[tma-1, tmb-1] = cost
        d[tma-1, tmb-1] = cost

    ql = [[0, 0]]
    ql = collections.deque(ql)
    fq = collections.defaultdict(int)
    while True:
        if len(ql) != 0:
            cost, tmp = ql.popleft()
            for tmv in tmd[tmp]:
                if tmv not in fq:
                    ql.append([cost + d[tmv, tmp], tmv])
                    fq[tmv] = (cost + d[tmv, tmp]) % 2
        else:
            break
    for i in range(n):
        print(fq[i])


if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
import sys
import collections


def solve():
    input = sys.stdin.readline
    mod = 10 ** 9 + 7
    tmd = collections.defaultdict(list)
    n = int(input().rstrip('\n'))
    d = collections.defaultdict(int)
    for i in range(n-1):
        tma, tmb, cost = list(map(int, input().rstrip('\n').split()))
        tmd[tma-1] += [tmb-1]
        tmd[tmb-1] += [tma-1]
        d[tma-1] = cost
        d[tmb-1] = cost

    ql = [[0, 0]]
    ql = collections.deque(ql)
    fq = collections.defaultdict(int)
    while True:
        if len(ql) != 0:
            cost, tmp = ql.popleft()
            for tmv in tmd[tmp]:
                if tmv not in fq:
                    ql.append([cost + d[tmv], tmv])
                    fq[tmv] = (cost + d[tmv]) % 2
        else:
            break
    for i in range(n):
        print(fq[i])


if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
from scipy.sparse.csgraph import floyd_warshall
import sys
readline = sys.stdin.readline
readlines = sys.stdin.readlines
read = sys.stdin.read


N = int(readline())
graph = [[] for _ in range(N + 1)]

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

stack = [1]
parent = [0] * (N + 1)
color = [0] * (N + 1)

while stack:
    here = stack.pop()
    for x, w in graph[here]:
        if x == parent[here]:
            continue
        parent[x] = here
        color[x] = color[here] ^ (w % 2)
        stack.append(x)

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

Submission status: Wrong Answer

Solution code:
n = int(input())
x = [[int(i) for i in input().split()] for i in range(n-1)]
c = [0]*n
for i in range(0,n-1):
    if x[i][2]%2==1:
        if c[x[i][0]-1]==0:
            c[x[i][1]-1]=1
        else:
            c[x[i][1]-1]=0
    else:
        continue
for i in range(n):
    print(c[i])

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import collections
import heapq

class Dijkstra():
    def __init__(self):
        self.e=collections.defaultdict(list)
    def add(self,u,v,d,directed=False):
        #0-indexedでなくても良いことに注意
        #u=from,v=to,d=cost
        #directed =Trueなら、有向グラフ
        if directed is False:
            self.e[u].append([v,d])
            self.e[v].append([u,d])
        else:
            self.e[u].append([v,d])
    def delete(self,u,v):
        self.e[u]=[_ for _ in self.e[u] if _[0]!=v]
        self.e[v]=[_ for _ in self.e[v] if _[0]!=u]
    def search(self,s):
        #:param s:始点
        #:return: 始点から各点までの最短経路
        d = collections.defaultdict(lambda: float('inf'))
        d[s] = 0
        q = []
        heapq.heappush(q, (0, s))
        v = collections.defaultdict(bool)
        while len(q):
            k, u = heapq.heappop(q)
            if v[u]:
                continue
            v[u] = True

            for uv, ud in self.e[u]:
                if v[uv]:
                    continue
                vd = k + ud
                if d[uv] > vd:
                    d[uv] = vd
                    heapq.heappush(q, (vd, uv))
        return d

#Saving Shuuk
n=int(input())
uvw=l=[list(map(int, input().split())) for i in range(n-1)]
#print(l)
graph1=Dijkstra()
for u,v,w in l:
    if w%2==0:
        graph1.add(u,v,0)
    else:
        graph1.add(u,v,1)

result_a=graph1.search(1) #{2: 0, 1: 1, 4: 2, 3: 21})
#print(result_a) #{3: 0, 1: 1, 4: 101, 2: 11})

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

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(1000000)
n=int(input())
l=[[] for i in range(n+1)]
for i in range(n-1):
  a,b,c=map(int,input().split())
  l[a].append([b,c])
  l[b].append([a,c])
color=[-1 for i in range(n+1)]
def dfs(s,c):
  color[s]=c
  for i in range(len(l[s])):
    index=l[s][i][0]
    dis=l[s][i][1]
    if color[index]!=-1:
      if dis%2==1:
        if color[index]==c:
          color[index]=1-c
          dfs(index,1-c)
    else:
      if dis%2==1:
        color[s]=1-c
        dfs(index,1-c)
      else:
        color[s]=c
        dfs(index,1-c)
dfs(1,1)
for i in range(1,n+1):
  print(color[i])

Submission status: Wrong Answer

Solution code:
import sys

n = int(input())

already = set()
color_ls = [-1 for i in range(n)]
for i in range(n - 1):
    u, v, w = [int(j) for j in input().split()]
    u -= 1
    v -= 1
    if i == 0:
        color_ls[u] = 0
        already.add(u)
    if (u not in already) and (v in already):
        if w % 2 == 0:
            if color_ls[v] == 0:
                color_ls[u] = 0
            else:
                color_ls[u] = 1
        else:
            if color_ls[v] == 0:
                color_ls[u] = 1
            else:
                color_ls[u] = 0
    else:
        if w % 2 == 0:
            if color_ls[u] == 0:
                color_ls[v] = 0
            else:
                color_ls[v] = 1
        else:
            if color_ls[u] == 0:
                color_ls[v] = 1
            else:
                color_ls[v] = 0
    already.update({u, v})
for c in color_ls:
    print(c)

Submission status: Wrong Answer

Solution code:
import sys

n = int(input())

already = set()
color_ls = {i:-1 for i in range(n)}
for i in range(n - 1):
    u, v, w = [int(j) for j in input().split()]
    u -= 1
    v -= 1
    if i == 0:
        color_ls[u] = 0
        already.add(u)
    if (u not in already) and (v in already):
        if w % 2 == 0:
            if color_ls[v] == 0:
                color_ls[u] = 0
            else:
                color_ls[u] = 1
        else:
            if color_ls[v] == 0:
                color_ls[u] = 1
            else:
                color_ls[u] = 0
    else:
        if w % 2 == 0:
            if color_ls[u] == 0:
                color_ls[v] = 0
            else:
                color_ls[v] = 1
        else:
            if color_ls[u] == 0:
                color_ls[v] = 1
            else:
                color_ls[v] = 0
    already.update({u, v})
for c in color_ls:
    print(c)

Submission status: Wrong Answer

Solution code:
import sys

n = int(input())

already = set()
color_ls = [-1 for i in range(n)]
for i in range(n - 1):
    u, v, w = [int(j) for j in sys.stdin.readline().split()]
    u -= 1
    v -= 1
    if i == 0:
        color_ls[u] = 0
        already.add(u)
    if (u not in already) and (v in already):
        if w % 2 == 0:
            if color_ls[v] == 0:
                color_ls[u] = 0
            else:
                color_ls[u] = 1
        else:
            if color_ls[v] == 0:
                color_ls[u] = 1
            else:
                color_ls[u] = 0
    else:
        if w % 2 == 0:
            if color_ls[u] == 0:
                color_ls[v] = 0
            else:
                color_ls[v] = 1
        else:
            if color_ls[u] == 0:
                color_ls[v] = 1
            else:
                color_ls[v] = 0
    already.update({u, v})
for c in color_ls:
    print(c)

Submission status: Wrong Answer

Solution code:
import sys

n = int(input())

already = set()
color_ls = [0 for i in range(n)]
for i in range(n - 1):
    u, v, w = [int(j) for j in sys.stdin.readline().split()]
    u -= 1
    v -= 1
    if i == 0:
        color_ls[u] = 0
        already.add(u)
    if u not in already and v in already:
        if w % 2 == 0:
            if color_ls[v] == 0:
                color_ls[u] = 0
            else:
                color_ls[u] = 1
        else:
            if color_ls[v] == 0:
                color_ls[u] = 1
            else:
                color_ls[u] = 0
    else:
        if w % 2 == 0:
            if color_ls[u] == 0:
                color_ls[v] = 0
            else:
                color_ls[v] = 1
        else:
            if color_ls[u] == 0:
                color_ls[v] = 1
            else:
                color_ls[v] = 0
    already.update({u, v})
for c in color_ls:
    print(c)

Submission status: Wrong Answer

Solution code:
import sys

n = int(input())

already = set()
color_ls = [0 for i in range(n)]
for i in range(n - 1):
    u, v, w = [int(i) for i in sys.stdin.readline().split()]
    u -= 1
    v -= 1
    if i == 0:
        color_ls[u] = 0
        already.add(u)
    if u not in already and v in already:
        if w % 2 == 0:
            if color_ls[v] == 0:
                color_ls[u] = 0
            else:
                color_ls[u] = 1
        else:
            if color_ls[v] == 0:
                color_ls[u] = 1
            else:
                color_ls[u] = 0
    else:
        if w % 2 == 0:
            if color_ls[u] == 0:
                color_ls[v] = 0
            else:
                color_ls[v] = 1
        else:
            if color_ls[u] == 0:
                color_ls[v] = 1
            else:
                color_ls[v] = 0
    already.update({u, v})
for c in color_ls:
    print(c)

Submission status: Wrong Answer

Solution code:
import sys

n = int(input())

already = set()
color_ls = [-1 for i in range(n)]
for i in range(n - 1):
    u, v, w = [int(i) for i in sys.stdin.readline().split()]
    u -= 1
    v -= 1
    if i == 0:
        color_ls[u] = 0
        already.add(u)
    if u not in already and v in already:
        if w % 2 == 0:
            if color_ls[v] == 0:
                color_ls[u] = 0
            else:
                color_ls[u] = 1
        else:
            if color_ls[v] == 0:
                color_ls[u] = 1
            else:
                color_ls[u] = 0
    else:
        if w % 2 == 0:
            if color_ls[u] == 0:
                color_ls[v] = 0
            else:
                color_ls[v] = 1
        else:
            if color_ls[u] == 0:
                color_ls[v] = 1
            else:
                color_ls[v] = 0
    already.update({u, v})
for c in color_ls:
    print(c)

Submission status: Wrong Answer

Solution code:
import sys

n = int(input())

already = set()
color_ls = [-1 for i in range(n)]
for i in range(n - 1):
    u, v, w = [int(i) for i in sys.stdin.readline().split()]
    u -= 1
    v -= 1
    if i == 0:
        color_ls[u] = 0
    if u not in already and v in already:
        if w % 2 == 0:
            if color_ls[v] == 0:
                color_ls[u] = 0
            else:
                color_ls[u] = 1
        else:
            if color_ls[v] == 0:
                color_ls[u] = 1
            else:
                color_ls[u] = 0
    else:
        if w % 2 == 0:
            if color_ls[u] == 0:
                color_ls[v] = 0
            else:
                color_ls[v] = 1
        else:
            if color_ls[u] == 0:
                color_ls[v] = 1
            else:
                color_ls[v] = 0
    already.update({u, v})
for c in color_ls:
    print(c)

Submission status: Wrong Answer

Solution code:
import sys

n = int(input())

_map = {j: [0 for i in range(n)] for j in range(n)}
already = set()
color_ls = [-1 for i in range(n)]
for i in range(n - 1):
    u, v, w = [int(i) for i in sys.stdin.readline().split()]
    u -= 1
    v -= 1
    if i == 0:
        color_ls[u] = 0
    _map[u][v] = w
    _map[v][u] = w
    if u not in already and v in already:
        color_ls[u] = 0 if (w % 2 == 0 and color_ls[v] == 0) or (w % 2 == 1 and color_ls[v] == 1) else 1
    else:
        color_ls[v] = 0 if (w % 2 == 0 and color_ls[u] == 0) or (w % 2 == 1 and color_ls[u] == 1) else 1
    already.update({u, v})
for c in color_ls:
    print(c)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

n = int(input())


class Node:

    def __init__(self, parent, id, child):
        self.parent = parent
        self.id = id
        self.child = child
        self.color = None

def get_root(node):
    while True:
        parent = node.parent
        if parent == -1:
            break
        node = node_ls[parent]
    return node.id


node_ls = {i: Node(-1, i, []) for i in range(n)}
_map = {j: [0 for i in range(n)] for j in range(n)}
already = set()
for i in range(n - 1):
    u, v, w = [int(i) for i in sys.stdin.readline().split()]
    u -= 1
    v -= 1
    if i == 0:
        root = u
    _map[u][v] = w
    _map[v][u] = w
    if u not in already and v in already:
        node_ls[u].parent = v
        node_ls[v].child.append(u)
    else:
        node_ls[v].parent = u
        node_ls[u].child.append(v)
    already.update({u, v})
node_ls[root].color = 0
cur = node_ls[root]
que = deque()
already.clear()
already.add(root)
while True:
    children = cur.child
    print(children)
    for child in children:
        if child in already:
            continue
        already.add(child)
        que.append(child)
        if _map[cur.id][child] % 2 == 0:
            node_ls[child].color = 1 if cur.color == 1 else 0
        else:
            node_ls[child].color = 0 if cur.color == 1 else 1
    if len(que) == 0 or len(already) == n:
        break
    cur = que.popleft()
    cur = node_ls[cur]
res = [node.color for node in node_ls.values()]
for r in res:
    print(r)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

n = int(input())


class Node:

    def __init__(self, parent, id, child):
        self.parent = parent
        self.id = id
        self.child = child
        self.color = None


node_ls = {i: Node(-1, i, []) for i in range(n)}
_map = {j: [0 for i in range(n)] for j in range(n)}
already = set()
for i in range(n - 1):
    u, v, w = [int(i) for i in sys.stdin.readline().split()]
    u -= 1
    v -= 1
    if i == 0:
        root = u
    _map[u][v] = w
    _map[v][u] = w
    if u not in already and v in already:
        node_ls[u].parent = v
        node_ls[v].child.append(u)
    else:
        node_ls[v].parent = u
        node_ls[u].child.append(v)
    already.update({u, v})
node_ls[root].color = 0
cur = node_ls[root]
que = deque()
already.clear()
while True:
    children = cur.child
    for child in children:
        if child in already:
            continue
        already.add(child)
        que.append(child)
        if _map[cur.id][child] % 2 == 0:
            node_ls[child].color = 1 if cur.color == 1 else 0
        else:
            node_ls[child].color = 0 if cur.color == 1 else 1
    if len(que) == 0:
        break
    cur = que.popleft()
    cur = node_ls[cur]
res = [node.color for node in node_ls.values()]
for r in res:
    print(r)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

n = int(input())


class Node:

    def __init__(self, parent, id, child):
        self.parent = parent
        self.id = id
        self.child = child
        self.color = None
        self.distance = 0

def get_root(node):
    while True:
        parent = node.parent
        if parent == -1:
            break
        node = node_ls[parent]
    return node.id


node_ls = {i: Node(-1, i, []) for i in range(n)}
_map = {j: [0 for i in range(n)] for j in range(n)}
already = set()
for i in range(n - 1):
    u, v, w = [int(i) for i in sys.stdin.readline().split()]
    u -= 1
    v -= 1
    if i == 0:
        root = u
    _map[u][v] = w
    _map[v][u] = w
    if u not in already and v in already:
        node_ls[u].parent = v
        node_ls[v].child.append(u)
    else:
        node_ls[v].parent = u
        node_ls[u].child.append(v)
    already.update({u, v})
node_ls[root].color = 0
cur = node_ls[root]
que = deque()
while True:
    children = cur.child
    for child in children:
        if node_ls[child].color is not None:
            continue
        que.append(child)
        if _map[cur.id][child] % 2 == 0:
            node_ls[child].color = 1 if node_ls[cur.id].color == 1 else 0
        else:
            node_ls[child].color = 0 if node_ls[cur.id].color == 1 else 1
    if len(que) == 0:
        break
    cur = que.popleft()
    cur = node_ls[cur]
res = [node.color for node in node_ls.values()]
for r in res:
    print(r)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
n = int(input())
class Node:

    def __init__(self, parent, id, child):
        self.parent = parent
        self.id = id
        self.child = child
        self.color = None

def get_root(node):
    while True:
        parent = node.parent
        if parent == -1:
            break
        node = node_ls[parent]
    return node.id

node_ls = {i:Node(-1, i, []) for i in range(n)}
_map = {j:[0 for i in range(n)] for j in range(n)}
already = set()
for i in range(n-1):
    u, v, w = [int(i) for i in sys.stdin.readline().split()]
    u -= 1
    v -= 1
    if i == 0:
        root = u
    _map[u][v] = w
    _map[v][u] = w
    if u not in already and v in already:
        node_ls[u].parent = v
        node_ls[v].child.append(u)
    else:
        node_ls[v].parent = u
        node_ls[u].child.append(v)
    already.update({u, v})
node_ls[root].color = 0
cur = node_ls[root]
que = deque()
while True:
    children = cur.child
    for child in children:
        if node_ls[child].color is not None:
            continue
        que.append(child)
        if _map[cur.id][child] % 2 == 0:
            node_ls[child].color = 1 if node_ls[cur.id].color == 1 else 0
        else:
            node_ls[child].color = 0 if node_ls[cur.id].color == 1 else 1
    if len(que) == 0:
        break
    cur = que.popleft()
    cur = node_ls[cur]
res = [node.color for node in node_ls.values()]
for r in res:
    print(r)

Submission status: Wrong Answer

Solution code:
import sys
import queue
n = int(input())
class Node:

    def __init__(self, parent, id, child):
        self.parent = parent
        self.id = id
        self.child = child
        self.color = None

def get_root(node):
    while True:
        parent = node.parent
        if parent == -1:
            break
        node = node_ls[parent]
    return node.id

node_ls = {i:Node(-1, i, []) for i in range(n)}
_map = {j:[0 for i in range(n)] for j in range(n)}
already = set()
for i in range(n-1):
    u, v, w = [int(i) for i in sys.stdin.readline().split()]
    u -= 1
    v -= 1
    if i == 0:
        root = u
    _map[u][v] = w
    _map[v][u] = w
    if u not in already and v in already:
        node_ls[u].parent = v
        node_ls[v].child.append(u)
    else:
        node_ls[v].parent = u
        node_ls[u].child.append(v)
    already.update({u, v})
node_ls[root].color = 0
cur = node_ls[root]
que = []
while True:
    children = cur.child
    for child in children:
        if node_ls[child].color is not None:
            continue
        queue.heappush(que, child)
        if _map[cur.id][child] % 2 == 0:
            node_ls[child].color = 1 if node_ls[cur.id].color == 1 else 0
        else:
            node_ls[child].color = 0 if node_ls[cur.id].color == 1 else 1
    if len(que) == 0:
        break
    cur = queue.heappop(que)
    cur = node_ls[cur]
res = [node.color for node in node_ls.values()]
for r in res:
    print(r)

Submission status: Wrong Answer

Solution code:
import sys
import queue
n = int(input())
class Node:

    def __init__(self, parent, id, child):
        self.parent = parent
        self.id = id
        self.child = child
        self.color = None

def get_root(node):
    while True:
        parent = node.parent
        if parent == -1:
            break
        node = node_ls[parent]
    return node.id

node_ls = [Node(-1, i, []) for i in range(n)]
_map = [[0 for i in range(n)] for j in range(n)]
already = set()
for i in range(n-1):
    u, v, w = [int(i) for i in sys.stdin.readline().split()]
    u -= 1
    v -= 1
    _map[u][v] = w
    _map[v][u] = w
    if u not in already and v in already:
        node_ls[u].parent = v
        node_ls[v].child.append(u)
    else:
        node_ls[v].parent = u
        node_ls[u].child.append(v)
    already.update({u, v})
root = get_root(node_ls[0])
cur = node_ls[root]
cur.color = 0
que = []
while True:
    children = cur.child
    for child in children:
        queue.heappush(que, child)
        if _map[cur.id][child] % 2 == 0:
            node_ls[child].color = 1 if node_ls[cur.id].color == 1 else 0
        else:
            node_ls[child].color = 0 if node_ls[cur.id].color == 1 else 1
    if len(que) == 0:
        break
    cur = queue.heappop(que)
    cur = node_ls[cur]
for i in range(n):
    print(node_ls[i].color)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(4100000)
input = sys.stdin.readline
def main():
    def Dfs(G, v, cur=0):
        color[v] = cur % 2
        for next_v in G[v]:
            if color[next_v[0]] != -1:
                continue
            Dfs(G, next_v[0], cur+next_v[1])


    n = int(input())
    e = [[0, 0, 0]] + [list(map(int, input().split())) for i in range(n-1)]
    g = [[] for i in range(n+1)]
    print(n, e);
    for i in range(1, n):
        g[e[i][0]].append([e[i][1], e[i][2]])
        g[e[i][1]].append([e[i][0], e[i][2]])

    color = [-1 for i in range(n+1)]
    for v in range(1, n+1):
        if color[v] != -1:
            continue
        Dfs(g, v)

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

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from sys import setrecursionlimit
setrecursionlimit(10**7)
N=int(input())
to = [[] for _ in range(N)]
for _ in range(N-1):
    u, v, w = [int(x) for x in input().split()]
    u -= 1
    v -= 1
    to[u].append([v, w])
    to[v].append([u, w])
ans = [-1] * N
ans[0] = 0
def dfs(v, p=-1, c=0):
    for u, w in to[v]:
        if u == p: continue
        if (w%2 != 0):
            c ^= 1
        ans[u] = c
        dfs(u, v, c)
dfs(0)
for v in ans:
    print(v)

Submission status: Wrong Answer

Solution code:
from collections import deque

N=int(input())
out=[set() for i in range(N+1)]
reached=[False for i in range(N+1)]
same=[[False for j in range(N+1)] for i in range(N+1)]

for i in range(N-1):
    u,v,w=map(int,input().split())
    out[u].add(v)
    out[v].add(u)
    same[u][v]=w%2==0

q=deque()
q.append(1)
reached[1]=True
same1=[False for i in range(N+1)]; same1[1]=True

while q:
    u=q.popleft()
    for v in out[u]:
        if not reached[v]:
            same1[v]=same1[u]==same[u][v]
            reached[v]=True
            q.append(v)

for i in range(1,N+1):
    print(0 if same1[i] else 1)

Submission status: Wrong Answer

Solution code:
N = int(input())
L = [[int(x) for x in input().split()] for _ in range(N-1)]

dic = {}
visited = [0]*(N+1)
for i in range(N-1):
    if visited[L[i][0]] == 1:
        dic[L[i][0]].append([L[i][1], L[i][2]])
    else:
        dic[L[i][0]] = [[L[i][1], L[i][2]]]
        visited[L[i][0]] = 1
    if visited[L[i][1]] == 1:
        dic[L[i][1]].append([L[i][0], L[i][2]])
    else:
        dic[L[i][1]] = [[L[i][0], L[i][2]]]
        visited[L[i][1]] = 1

ans_list = ["0"]*(N+1)
q = [1]
D = [0]
visited = [0]*(N+1)
while len(q) > 0:
    v = q.pop(0)
    d = D.pop(0)
    for i in range(len(dic[v])):
        if visited[dic[v][i][0]] == 0:
            q.append(dic[v][i][0])
            D.append(dic[v][i][1])
            visited[dic[v][i][0]] = 1
            if (d+dic[v][i][1])%2 == 1:
                ans_list[dic[v][i][0]] = "1"
                
for i in range(1, N+1):
    print(ans_list[i])

Submission status: Wrong Answer

Solution code:
#%%
import numpy as np
n = int(input())
u, v, w = [0] * (n-1), [0] * (n-1), [0] * (n-1)

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

#print(app)


ans = [5] * (n+1)
ans[1] = 0
#print(ans)
for i in range(1, n+1):
    for j in range(2):
        for k in app[i][j]:
            if ans[i] == 0:
                if j == 0:
                    ans[k] = 1
                else:
                    ans[k] = 0
            else:
                if j == 0:
                    ans[k] = 0
                else:
                    ans[k] = 1
            #print(ans)

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

Submission status: Wrong Answer

Solution code:
#%%
import numpy as np
n = int(input())
u, v, w = [0] * (n-1), [0] * (n-1), [0] * (n-1)

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

#print(app)


ans = [0] * (n+1)
for i in range(1, n+1):
    for j in range(2):
        for k in app[i][j]:
            if ans[i] == 0:
                if j == 0:
                    ans[k] = 1
                else:
                    ans[k] = 0
            else:
                if j == 0:
                    ans[k] = 0
                else:
                    ans[k] = 1

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

Submission status: Wrong Answer

Solution code:
#%%
import numpy as np
n = int(input())
u, v, w = [0] * (n-1), [0] * (n-1), [0] * (n-1)

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

#print(app)

ans = [1] * (n+1)
check = [0] * (n+1)

que = [1]
while len(que) > 0:
    t = que.pop(0)
    tmp = app[t][1]
    check[t] = 1
    ans[t] = 0
    for i in tmp:
        if check[i] == 0:
            que.append(i)
        else:
            pass

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

Submission status: Wrong Answer

Solution code:
from collections import deque
n = int(input())

ed = [[] for _ in range(n)]

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

c = [0]*n

Q = deque()

Q.append(0)
while Q:
    u = Q.popleft()###popleft
    for e in ed[u]:
        c[e[0]] = (e[1]&1) ^ c[u]  
        Q.append(e[0])

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

Submission status: Wrong Answer

Solution code:
from collections import deque
n = int(input())

ed = [[] for _ in range(n)]

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

c = [0]*n

Q = deque()

Q.append(0)
while Q:
    u = Q.pop()
    for e in ed[u]:
        c[e[0]] = (e[1]&1) ^ c[u]  
        Q.append(e[0])

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

Submission status: Wrong Answer

Solution code:
print(3)

Submission status: Wrong Answer

Solution code:
N = int(input())

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

Submission status: Wrong Answer

Solution code:
N = int(input())

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