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())
vers = [[]] * n
is_ver_checked = [False] * n
ans = [0] * n

for i in range(n):
  vers[i] = []

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

# print(n)
# print(vers)

def dfs(ver_i):
  if is_ver_checked[ver_i]:
    return
  for next_ver_i, w in vers[ver_i]:
    # 接続先の色を決定
    if w % 2 == 1:
      # 幅が奇数の場合、現在の頂点と違う色
      if ans[ver_i] == 1:
        ans[next_ver_i] = 0
      else:
        ans[next_ver_i] = 1
    else:
      # 辺の幅が偶数の場合、現在の頂点を同じ色
      ans[next_ver_i] = ans[ver_i]
    dfs(next_ver_i)
  is_ver_checked[ver_i] = True

dfs(0)
for i in range(n):
  print(ans[i])

Submission status: Wrong Answer

Solution code:
n = int(input())
vers = [[]] * n
is_ver_checked = [False] * n
ans = [0] * n

for i in range(n):
  vers[i] = []

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

# print(n)
# print(vers)

def dfs(ver_i):
  if is_ver_checked[ver_i]:
    return
  for next_ver_i, w in vers[ver_i]:
    # 接続先の色を決定
    if w % 2 == 1:
      ans[next_ver_i] = 1
    dfs(next_ver_i)
  is_ver_checked[ver_i] = True

dfs(0)
for i in range(n):
  print(ans[i])

Submission status: Wrong Answer

Solution code:
N = int(input())
search_lst = [[] for i in range(N)]
Tree = [[0]*N for i in range(N)]
for i in range(N-1):
    u,v,w = map(int,input().split())
    Tree[u-1][v-1] = Tree[v-1][u-1] = w
    search_lst[u-1].append(v-1)

#print(Tree)
search_flag = [False]*N
Color = [False]*N

for i in range(N):
    if search_flag[i]: continue
    for j in search_lst[i]:
        if search_flag[j] and search_flag[i]:continue
        if search_flag[j]:
            if Tree[i][j] %2 == 0:
                Color[i] = Color[j]
            else:
                Color[i] = not Color[j]
        else:
            if Tree[i][j] %2 == 0:
                Color[i] = True
                Color[j] = True
            else:
                Color[i] = True
                Color[j] = False
            #探索済み
            search_flag[j] = True
        #探索済み
        search_flag[i] = True

Flag = {False:0 ,True:1}
for i in range(N):
    print(Flag[Color[i]])

Submission status: Wrong Answer

Solution code:
N=int(input())
vertexes=[[] for i in range(N)]
edges=[[int(s) for s in input().split()] for i in range(N-1)]
#頂点ごとに、頂点から伸びる辺のデータを記録
for e in edges:
  vertexes[e[0]-1].append([e[1]-1,e[2]])
  vertexes[e[1]-1].append([e[0]-1,e[2]])

#幅優先探索の開始（v_possibleに入った順に頂点を訪れる）
answer=[-1 for i in range(N)]  #到達していない頂点は-1
answer[0]=0
v_possible=[] #到達可能な頂点と、辺の情報
v=0 #現在の位置
for i in range(N-1):
  for e in vertexes[v]: #行き先の更新
    v_possible.append([e[0],v,e[1]]) #[行き先,つながる頂点,距離]
  for j in v_possible:
    if answer[j[0]]==-1: #まだ訪れていないなら移動
      answer[j[0]]=(answer[j[1]]+j[2])%2
      v=j[0]
      break
  print(answer)

#答を出力
for i in range(N):
  print(answer[i])

Submission status: Wrong Answer

Solution code:
import heapq as hp
N = int(input())
ans = [-1]*N
arr = []
for i in range(N-1):
    u,v,w = map(int,input().split())
    u -= 1
    v -= 1
    arr.append([u,v,w])
e_list = [[] for i in range(N)]
for u,v,w in arr:
    e_list[u].append([v,w])
    e_list[v].append([u,w])

stack = [0]
ans[0] = 0
for rnd in range(N):
    if stack == []:
        break
    cur_pos = stack.pop()
    for des,cost in e_list[cur_pos]:
        if ans[des] >  0:
            continue
        ans[des] = (ans[cur_pos] + cost)%2
        stack.append(des)
for i in ans:
    print(i)

Submission status: Wrong Answer

Solution code:
N = int(input())

S = [{} for i in range(N + 1)]
answer = [0] * (N + 1)
for i in range(N - 1):
    u, v, w = map(int, input().split())
    S[u][v] = w
    S[v][u] = w
answer[1] = 1
for i in range(1, N + 1):
    for k, v in S[i].items():
        if v % 2 == 0:
            answer[k] = answer[i]
for ans in answer[1:]:
    print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

gusuu_list = []
kisuu_list = []
for i in range(N-1):
    u, v, w = map(int, input().split())
    if w % 2 == 0:
        gusuu_list += [u, v]
    else:
        kisuu_list += [u, v]

gusuu_set = set(gusuu_list)
for i in range(1, N+1):
    if i in gusuu_set:
        print(1)
    else:
        print(0)

Submission status: Wrong Answer

Solution code:
N = int(input())

S = [{} for i in range(N+1)]
answer = [0] * (N+1)
for i in range(N-1):
    u, v, w = map(int, input().split())
    S[u][v] = w
    S[v][u] = w
answer[1] = 0
for i in range(1, N+1):
    for k, v in S[i].items():
        if v % 2 != 0:
            answer[k] = 1
for ans in answer[1:]:
    print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

S = [{} for i in range(N+1)]
answer = [0] * (N+1)
for i in range(N-1):
    u, v, w = map(int, input().split())
    S[u][v] = w
    S[v][u] = w
answer[1] = 1
for i in range(1, N+1):
    for k, v in S[i].items():
        if v % 2 == 0:
            answer[k] = 1
for ans in answer[1:]:
    print(ans)

Submission status: Wrong Answer

Solution code:
def p_d():
    from sys import stdin

    input = stdin.readline
    n = int(input())
    tree = [-1] * (n + 1)
    u, v, w = map(int, input().split())
    tree[u] = 0
    tree[v] = 0 if w & 1 == 0 else 1
    for _ in range(n - 2):
        u, v, w = map(int, input().split())
        if tree[u] != -1:
            tree[v] = 0 if (w + tree[v]) & 1 == 0 else 1
        elif tree[v] != -1:
            tree[u] = 0 if (w + tree[u]) & 1 == 0 else 1
    print("\n".join(map(str, tree[1:])))
    
if __name__ == '__main__':
    p_d()

Submission status: Wrong Answer

Solution code:
n = int(input())
uvw = [[0] * 3] * (n - 1)
p = [-1] * n

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

for i in range(n - 1):
    if (p[uvw[i][0] - 1] != -1 and p[uvw[i][1] - 1] != -1):
        next
    elif (p[uvw[i][0] - 1] != -1):
        if (p[uvw[i][0] - 1] == 0):
            if (uvw[i][2] % 2 == 0):
                p[uvw[i][1] - 1] = 0
            else:
                p[uvw[i][1] - 1] = 1
        elif (p[uvw[i][0] - 1] == 1):
            if (uvw[i][2] % 2 == 0):
                p[uvw[i][1] - 1] = 1
            else:
                p[uvw[i][1] - 1] = 0
    elif (p[uvw[i][1] - 1] != -1):
        if (p[uvw[i][1] - 1] == 0):
            if (uvw[i][2] % 2 == 0):
                p[uvw[i][0] - 1] = 0
            else:
                p[uvw[i][0] - 1] = 1
        elif (p[uvw[i][1] - 1] == 1):
            if (uvw[i][2] % 2 == 0):
                p[uvw[i][0] - 1] = 1
            else:
                p[uvw[i][0] - 1] = 0
    else:
        p[uvw[i][0] - 1] = 0

        if ((uvw[i][2]) % 2 == 0):
            p[uvw[i][1] - 1] = 0
        else:
            p[uvw[i][1] - 1] = 1

for e in p:
    print(e)

Submission status: Wrong Answer

Solution code:
n = int(input())
uvw = [[0] * 3] * (n - 1)
p = [-1] * n

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

for i in range(n - 1):
    if (p[i] != -1 and p[i + 1] != -1):
        next
    elif (p[i] != -1):
        if (p[i] == 0):
            if (uvw[i][2] % 2 == 0):
                p[i + 1] = 0
            else:
                p[i + 1] = 1
        elif (p[i] == 1):
            if (uvw[i][2] % 2 == 0):
                p[i + 1] = 1
            else:
                p[i + 1] = 0
    elif (p[i + 1] != -1):
        if (p[i + 1] == 0):
            if (uvw[i][2] % 2 == 0):
                p[i] = 0
            else:
                p[i] = 1
        elif (p[i + 1] == 1):
            if (uvw[i][2] % 2 == 0):
                p[i] = 1
            else:
                p[i] = 0
    else:
        p[i] = 0

        if ((uvw[i][2]) % 2 == 0):
            p[i + 1] = 0
        else:
            p[i + 1] = 1

for e in p:
    print(e)

Submission status: Wrong Answer

Solution code:
N=int(input())
lst=[list(map(int, input().split())) for _ in range(N-1)]
lst=sorted(lst, key=lambda x:(x[0], x[1]))
d=[0 for _ in range(N+1)]
for i in range(N-1):
  u, v, w=lst[i]
  d[v]=d[u]+w
for i in range(1, N+1):
  print(0 if d[i]%2==0 else 1)

Submission status: Wrong Answer

Solution code:
def visit(i):
  for j in range(i+1, N+1):
    if matrix[i][j]>0:
      d[j]=d[i]+matrix[i][j]
      visit(j)

N=int(input())
lst=[list(map(int, input().split())) for _ in range(N-1)]
d=[0 for _ in range(N+1)]
matrix=[[0 for _ in range(N+1)] for _ in range(N+1)]
for i in range(N-1):
  u, v, w=lst[i]
  matrix[u][v]=w
visit(1)
for i in range(1, N+1):
  print(0 if d[i]%2==0 else 1)

Submission status: Wrong Answer

Solution code:
def visit(i):
  for j in range(i+1, N+1):
    if visited[j]==1:
      continue
    if matrix[i][j]>0:
      color[j]=(color[i]-1)*(-1) if matrix[i][j]%2==1 else color[i]
      visited[j]=1
      visit(j)

N=int(input())
lst=[list(map(int, input().split())) for _ in range(N-1)]
visited=[0 for _ in range(N+1)]
color=[0 for _ in range(N+1)]
matrix=[[0 for _ in range(N+1)] for _ in range(N+1)]
for i in range(N-1):
  u, v, w=lst[i]
  matrix[u][v]=w
visit(1)
for i in range(1, N+1):
  print(color[i])

Submission status: Wrong Answer

Solution code:
def visit(i):
  for j in range(i+1, N+1):
    if visited[j]==1:
      continue
    if matrix[i][j]>0:
      color[j]=(color[i]-1)*(-1) if matrix[i][j]%2==1 else color[i]
      visit(j)

N=int(input())
lst=[list(map(int, input().split())) for _ in range(N-1)]
visited=[0 for _ in range(N+1)]
color=[0 for _ in range(N+1)]
matrix=[[0 for _ in range(N+1)] for _ in range(N+1)]
for i in range(N-1):
  u, v, w=lst[i]
  matrix[u][v]=w
visit(1)
for i in range(1, N+1):
  print(color[i])

Submission status: Wrong Answer

Solution code:
N = int(input())
keirolist = [tuple(map(int, input().split())) for i in range(N-1)]

ans = [0 for i in range(N+1)]
for (u, v, w) in keirolist:
  if w % 2 == 0:
    ans[u] = 1
    ans[v] = 1

[print(a) for a in ans[1:]]

Submission status: Wrong Answer

Solution code:
def func(dic,key,color_array):
  if not (key in dic):
    return
  
  arrs = dic[key]
  parent_color = color_array[key-1]
  
  for arr in arrs:
    if arr[1] % 2 == 0:
      color_array[arr[0]-1] = parent_color
    else:
      color_array[arr[0]-1] = (not parent_color)
    
    func(dic,arr[0],color_array) 

n = int(input())
color_array = [True] * n
dic = {}

for i in range(n-1):
  a,b,c = map(int,input().split())
  if not (a in dic.keys()):
    dic[a] = []
  
  dic[a].append([b,c])

func(dic,1,color_array)

for color in color_array:
  if color:
    print(0)
  else:
    print(1)

Submission status: Wrong Answer

Solution code:
import math

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors

#a = list(map(int, input().split()))

#####################################


#0:49
n = int(input())
nlist = [[]for i in range(n+1)]
for i in range(n-1):
    temp = list(map(int, input().split()))
    nlist[temp[0]].append([temp[1],temp[2]])
    nlist[temp[1]].append([temp[0],temp[2]])


queue = []    
for i in range(n+1):
    if(len(nlist[i])==1):
        queue.append([-1,i])
        break
        

searched = 0
color=[-1 for i in range(n+1)]
nowcol=0



queue.append([queue[0][1],nlist[queue[0][1]][0][0]])
if(nlist[queue[0][1]][0][1]%2==0):
    color[queue[0][1]]=nowcol
else:
    nowcol=1-nowcol
    color[queue[0][1]]=nowcol
searched+=1
    
while(True):
    nowpos = queue[searched][1]
    #print(queue[searched:])
    #print(nowpos)
    for k in nlist[nowpos]:
        if(k[0]!=queue[searched][0]):
            queue.append([nowpos,k[0]])
        #print(k[1])
        if(k[1]%2==0):
            color[k[0]]=nowcol
        else:
            nowcol = 1-nowcol
            color[k[0]]=nowcol
    searched+=1
    if(len(queue)==searched):
        break
for i in range(n):
    print(color[i+1])

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from collections import deque


def main():
    BLACK = 1
    WHITE = 0
    EDGE = 0
    WEIGHT = 1
    N = int(input())
    graph = [[] for i in range(N+1)]
    colors = [-1] * (N + 1)
    REVERSE = {
        BLACK: WHITE,
        WHITE: BLACK
    }

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

    q = deque()
    q.append(1)
    while len(q) != 0:
        parentIdx = q.popleft()
        parent = graph[parentIdx]
        # print(u+1, [i + 1 for i in q])
        # print(parent, u)
        for v, e in enumerate(parent):
            q.append(e[EDGE])
            if colors[e[EDGE]] == -1:
                if e[WEIGHT] % 2 == 0:
                    colors[e[EDGE]] = colors[parentIdx]
                else:
                    colors[e[EDGE]] = 1 - colors[parentIdx]

    for i in range(1, len(colors)):
        print(colors[i])


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque


def main():
    BLACK = 1
    WHITE = 0
    EDGE = 0
    WEIGHT = 1
    N = int(input())
    graph = [[] for i in range(N+1)]
    colors = [BLACK] * (N + 1)
    REVERSE = {
        BLACK: WHITE,
        WHITE: BLACK
    }

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

    q = deque()
    q.append(1)
    while len(q) != 0:
        parentIdx = q.popleft()
        parent = graph[parentIdx]
        # print(u+1, [i + 1 for i in q])
        # print(parent, u)
        for v, e in enumerate(parent):
            q.append(e[EDGE])
            if e[WEIGHT] % 2 == 0:
                colors[e[EDGE]] = colors[parentIdx]
            else:
                colors[e[EDGE]] = 1 - colors[parentIdx]

    for i in range(1, len(colors)):
        print(colors[i])


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque


def main():
    BLACK = 1
    WHITE = 0
    EDGE = 0
    WEIGHT = 1
    N = int(input())
    graph = [[] for i in range(N+1)]
    colors = [BLACK] * (N + 1)
    REVERSE = {
        BLACK: WHITE,
        WHITE: BLACK
    }

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

    q = deque()
    q.append(1)
    while len(q) != 0:
        parentIdx = q.popleft()
        parent = graph[parentIdx]
        # print(u+1, [i + 1 for i in q])
        # print(parent, u)
        for v, e in enumerate(parent):
            q.append(e[EDGE])
            if e[WEIGHT] % 2 == 0:
                colors[e[EDGE]] = colors[parentIdx]
            else:
                colors[e[EDGE]] = REVERSE[colors[parentIdx]]

    for i in range(1, len(colors)):
        print(colors[i])


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque
import sys
import math
input = sys.stdin.readline

MOD = 10 ** 9 + 7


def main():
    BLACK = 1
    WHITE = 0
    EDGE = 0
    WEIGHT = 1
    N = int(input())
    graph = [[] for i in range(N+1)]
    colors = [BLACK] * (N + 1)
    REVERSE = {
        BLACK: WHITE,
        WHITE: BLACK
    }

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

    q = deque()
    q.append(1)
    while len(q) != 0:
        parentIdx = q.popleft()
        parent = graph[parentIdx]
        # print(u+1, [i + 1 for i in q])
        # print(parent, u)
        for v, e in enumerate(parent):
            q.append(e[EDGE])
            if e[WEIGHT] % 2 == 0:
                colors[e[EDGE]] = colors[parentIdx]
            else:
                colors[e[EDGE]] = REVERSE[colors[parentIdx]]

    for i in range(1, len(colors)):
        print(colors[i])


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque

n = int(input())
g = [[] for i in range(n)]
for i in range(n-1) :
    a,b,c = map(int,input().split()); a -= 1; b -= 1
    if a > b : a, b = b, a
    g[a].append([b, c])

d = [0]*n
q = deque([0])
while q :
    now = q.popleft()
    for nxt, c in g[now] :
        d[nxt] = (d[now] + c) % 2
        q.append(nxt)
print(*d, sep='\n')

Submission status: Wrong Answer

Solution code:
from collections import deque

n = int(input())
g = [[] for i in range(n)]
for i in range(n-1) :
    a,b,c = map(int,input().split()); a -= 1; b -= 1
    if a > b : a, b = b, a
    g[a].append([b, c])

d = [0]*n
q = deque([0])
while q :
    now = q.popleft()
    for nxt, c in g[now] :
        d[nxt] = (d[0] + c) % 2
        q.append(nxt)
print(*d, sep='\n')

Submission status: Wrong Answer

Solution code:
from collections import deque

n = int(input())
g = [[] for i in range(n)]
for i in range(n-1) :
    a,b,c = map(int,input().split()); a -= 1; b -= 1
    if a > b : a, b = b, a
    g[a].append([b, c])

visited = [0]*n
d = [0]*n
q = deque([])
for i in range(n) :
    if visited[i] : continue
    q.append(i)
    while q :
        now = q.popleft()
        for nxt, c in g[now] :
            if visited[nxt] : continue
            visited[nxt] = 1
            d[nxt] = (d[now] + c) % 2
            q.append(nxt)
print(*d, sep='\n')

Submission status: Wrong Answer

Solution code:
from collections import deque

n = int(input())
g = [[] for i in range(n)]
for i in range(n-1):
    a,b,c = map(int,input().split()); a-=1;b-=1
    g[a].append([b, c]); g[b].append([a, c])

d = 0
res = [0]*n
q = deque([0])
visited = [0]*n; visited[0] = 1
while q :
    now = q.popleft()
    for nxt, c in g[now] :
        if visited[nxt] : continue
        visited[nxt] = 1
        d += c
        res[nxt] = (d % 2)
        q.append(nxt)

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

Submission status: Wrong Answer

Solution code:
from collections import deque

N = int(input())
dic = {i + 1:[] for i in range(N)}
color = [-1 for i in range(N + 1)]

for i in range(N - 1):
    f, t, d = map(int, input().split())
    dic[f].append((t, d & 1)) #奇数だったら1
    dic[t].append((f, d & 1))

q = deque()
q.append(1)
color[1] = 0
while len(q) > 0:
    v = q.popleft()
    for t in dic[v]:
        if not color[t[0]] == -1:
            continue
        color[t[0]] = 1 - color[v] if t[1] == 1 else color[v]
        q.append(t[0])

print(color)

Submission status: Wrong Answer

Solution code:
class Node:
    def __init__(self):
        self.val = None
        self.a = []



n = int(input())
nodes = []

for i in range(n):
    nodes.append(Node())

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

root = nodes[0]
root.val = True

def traverse(node, distance):
    for pair in node.a:
        adjNode = pair[0]
        dis = pair[1]
        if (distance + dis) % 2 == 0:
            adjNode.val = root.val
        else:
            adjNode.val = not root.val
        traverse(adjNode, distance + dis)

traverse(root, 0)

for i in range(n):
    node = nodes[i]
    if node.val:
        print(1)
    else:
        print(0)

Submission status: Wrong Answer

Solution code:
class Node:
    def __init__(self):
        self.val = None
        self.a = []

def traverse(node):
    for pair in node.a:
        adjNode = pair[0]
        dis = pair[1]
        if dis % 2 == 0:
            adjNode.val = node.val
        else:
            adjNode.val = not node.val
        traverse(adjNode)

n = int(input())
nodes = []

for i in range(n):
    nodes.append(Node())

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

root = nodes[0]
root.val = True
traverse(root)

for i in range(n):
    node = nodes[i]
    if node.val:
        print(1)
    else:
        print(0)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-

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


## 不一致分で他の場所で登場してないやつ
ls = []
tmp = []
for i in range(N-1):
    if w[i] % 2 == 0:
        ls.append(u[i])
        ls.append(v[i])
    else:
        tmp.append([u[i], v[i]])
ls_unique = list(set(ls))
#print(ls_unique)
result = [0] * N
for i in range(len(tmp)):
    if tmp[i][0] in ls_unique:
        result[u[i]-1] = 1
    else:
        result[v[i]-1] = 1
for i in range(len(ls_unique)):
    result[ls_unique.pop()-1] = 1
for i in range(N):
    print(result[i])

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-

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

ls = []
tmp = []

for i in range(N-1):
    if w[i] % 2 == 0:
        ls.append(u[i])
        ls.append(v[i])
ls_unique = list(set(ls))
#print(ls_unique)
result = [0] * N
for i in range(N-1):
    if u[i] in ls_unique != True and w[i] % 2 != 0:
        result[v[i]-1] = 1
for i in range(len(ls_unique)):
    result[ls_unique.pop()-1] = 1
for i in range(N):
    print(result[i])

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-

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

ls = []

for i in range(N-1):
    if w[i] % 2 == 0:
        ls.append(u[i])
        ls.append(v[i])
ls_unique = list(set(ls))
#print(ls_unique)
result = [0] * N
for i in range(len(ls_unique)):
    result[ls_unique.pop()-1] = 1
for i in range(N):
    print(result[i])

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

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

l_a = []
l_b = []
l_a.append(u[0])
for i in range(N-1):
    if w[i] % 2 != 0:
        if u[i] in l_a:
            l_b.append(v[i])
        elif u[i] in l_b:
            l_a.append(v[i])
        elif v[i] in l_a:
            l_b.append(u[i])
        else:
            l_a.append(u[i])
    else:
        if u[i] in l_a:
            l_a.append(v[i])
        elif u[i] in l_b:
            l_b.append(v[i])
        elif v[i] in l_a:
            l_a.append(u[i])
        else:
            l_b.append(u[i])
#print(l_a)
#print(l_b)
result = [0] * N
for i in range(len(l_a)):
    #print(l_a.pop())
    result[l_a.pop()-1] = 1
for i in range(N):
    print(result[i])

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-

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

l_a = []
l_b = []
l_a.append(u[0])
for i in range(N-1):
    if w[i] % 2 != 0:
        if u[i] in l_a:
            l_b.append(v[i])
        elif u[i] in l_b:
            l_a.append(v[i])
        elif v[i] in l_a:
            l_b.append(u[i])
        else:
            l_a.append(u[i])
    else:
        if u[i] in l_a:
            l_a.append(v[i])
        elif u[i] in l_b:
            l_b.append(v[i])
        elif v[i] in l_a:
            l_a.append(u[i])
        else:
            l_b.append(u[i])
#print(l_a)
#print(l_b)
result = [0] * N
for i in range(len(l_a)):
    #print(l_a.pop())
    result[l_a.pop()-1] = 1
print(*result)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(100000)
print(sys.getrecursionlimit())
n=int(input())
visitedlist=[-1 for  i in range(n)]
kilist=[[] for i in range(n)]
anslist=[0 for i in range(n)]
searchedlist=[-1 for i in range(n)]
for i in range(n-1):
        a,b,c=map(int,input().split())
        kilist[a-1].append((b,c))
        kilist[b-1].append((a,c))
def search(i):
        searchedlist[i-1]=1
        visitedlist[i-1]=1
        flag=0
        for j in kilist[i-1]:
            if visitedlist[j[0]-1]<0:
                visitedlist[j[0]-1]=1
                flag+=1
                anslist[j[0]-1]=anslist[i-1]+j[1]
        else:
            pass

        if flag>0:    
            for k in kilist[i-1]:
                if searchedlist[k[0]-1]==-1:
                    search(k[0])
search(2)
for i in anslist:
    print(i%2)

Submission status: Wrong Answer

Solution code:
#d-even relation
n=int(input())
lists=[tuple(map(int,input().split())) for i in range(n-1)]
kilist=[[] for i in range(n)]
for i in lists:
    kilist[i[0]-1].append(i)
    kilist[i[1]-1].append(i)
#最大でも2*nの探索で必ず終了する
anslist=[-1 for i in range(n)]
anslist[0]=0
for i in range(n):
    for j in kilist[i]:
        if j[0]==i+1:
            if j[2]%2==0 and anslist[j[1]-1]<0:
                anslist[j[1]-1]=0
            elif j[2]%2!=0 and anslist[j[1]-1]<0:
                anslist[j[1]-1]=1
        elif j[1]==i+1:
            if j[2]%2==0 and anslist[j[0]-1]<0:
                anslist[j[0]-1]=0
            elif j[2]%2!=0 and anslist[j[0]-1]<0:
                anslist[j[0]-1]=1
for j in anslist:
    print(j)

Submission status: Wrong Answer

Solution code:
# -*- coding utf-8 -*-

MOD = 10 ** 9 + 7

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

ans = [0] * (n + 1)

for u, v, w in uvw_n:
    if (ans[u] + w) % 2 == 0:
        ans[v] = ans[u]
    else:
        ans[v] = (ans[u] + 1) % 2

for a in ans[1:]:
    print(a)

Submission status: Wrong Answer

Solution code:
# -*- coding utf-8 -*-

MOD = 10 ** 9 + 7

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

ans = [0] * (n + 1)

for u, v, w in uvw_n:
    if w % 2 == 0:
        ans[v] = ans[u]
    else:
        ans[v] = (ans[u] + 1) % 2

for a in ans[1:]:
    print(a)

Submission status: Wrong Answer

Solution code:
n = int(input())
uvw = [tuple(map(int,input().split())) for i in range(n-1)]
to_dist = [[] for i in range(n)]
for u,v,w in uvw:
    u,v = u-1,v-1
    to_dist[u].append((v,w))
    to_dist[v].append((u,w))

visited = [0]* n
visited[0] = 1
ans = [0]*n
to_dist_ = to_dist[0]
start = True

while to_dist:
    if start == False:
        to_dist_ = to_dist.pop()

    for to, dist in to_dist_:
        if visited[to]: continue
        ans[to] = (ans[u]+dist)%2
        visited[to] = 1
    start = False
print(*ans, sep='\n')

Submission status: Wrong Answer

Solution code:
n = int(input())
uvw = [tuple(map(int,input().split())) for i in range(n-1)]
to_dist = [[] for i in range(n)]
for u,v,w in uvw:
    u,v = u-1,v-1
    to_dist[u].append((v,w))
    to_dist[v].append((u,w))

visited = [0]* n
visited[0] = 1
ans = [0]*n
to_dist_ = to_dist[0]
 
while to_dist:
    for to, dist in to_dist_:
        if visited[to]: continue
        ans[to] = (ans[u]+dist)%2
        visited[to] = 1
    to_dist_ = to_dist.pop()
print(*ans, sep='\n')

Submission status: Wrong Answer

Solution code:
n = int(input())
u = [0] * (n-1) 
v = [0] * (n-1) 
w = [0] * (n-1) 
for i in range(n-1):
    u[i], v[i], w[i] = map(int, input().split())

ans = [True]*n
ans[0] = True

for i in range(n-1):
    if w[i] % 2 == 1:
        ans[v[i]-1] = not ans[u[i]-1]
    else:  
        ans[v[i]-1] = ans[u[i]-1]

for i in range(n):
    if ans[i] == True:
        print(1)
    else:
        print(0)

Submission status: Wrong Answer

Solution code:
n = int(input())
bw = [0] * (n + 1)

for i in range(0,n-1):
  tmp = list(map(int, input().split()))
  u = tmp[0]
  v = tmp[1]
  w = tmp[2]

  if w%2 == 0:
    jdg = 1
  else:
    jdg = -1

  if bw[u] == 0 and bw[v] == 0:
    bw[u] = -1
    bw[v] = bw[u] * jdg
  elif bw[v] == 0:
    bw[v] = bw[u] * jdg
  elif bw[u] == 0:
    bw[u] = bw[v] * jdg

for i in range(1,n+1):
  if bw[i] == -1:
    print('0')
  else:
    print('1')

Submission status: Wrong Answer

Solution code:

n = int(input())
bw = [0] * (n + 1)

for i in range(0,n-1):
  tmp = list(map(int, input().split()))
  u = tmp[0]
  v = tmp[1]
  w = tmp[2]

  if w%2 == 0:
    jdg = 1
  else:
    jdg = -1

  if bw[u] == 0 and bw[v] == 0:
    bw[u] = -1
    bw[v] = bw[u] * jdg
  elif bw[u] != 0:
    bw[v] = bw[u] * jdg
  else:
    bw[u] = bw[v] * jdg

for i in range(1,n+1):
  if bw[i] == -1:
    print('0')
  else:
    print('1')

Submission status: Wrong Answer

Solution code:

n = int(input())
bw = [0] * (n + 1)

for i in range(1,n-1):
  tmp = list(map(int, input().split()))
  u = tmp[0]
  v = tmp[1]
  w = tmp[2]

  if w%2 == 0:
    jdg = 1
  else:
    jdg = -1

  if bw[u] == 0 and bw[v] == 0:
    bw[u] = -1
    bw[v] = bw[u] * jdg
  elif bw[u] != 0:
    bw[v] = bw[u] * jdg
  else:
    bw[u] = bw[v] * jdg

for i in range(1,n+1):
  if bw[i] == -1:
    print('0')
  else:
    print('1')

Submission status: Wrong Answer

Solution code:
n = int(input())
bw = [0] * (n + 1)

tmp = list(map(int, input().split()))
u = tmp[0]
v = tmp[1]
w = tmp[2]

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

for i in range(1,n-1):
  tmp = list(map(int, input().split()))
  u = tmp[0]
  v = tmp[1]
  w = tmp[2]

  if w%2 == 0:
    jdg = 1
  else:
    jdg = -1

  if bw[u] != 0:
      bw[v] = bw[u] * jdg
  else:
      bw[u] = bw[v] * jdg

for i in range(1,n+1):
  if bw[i] == -1:
    print('0')
  else:
    print('1')

Submission status: Wrong Answer

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]
n=int(input())
uvw=[list(map(int,input().split())) for i in range(n-1)]
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)
        if self.rank[rx] < self.rank[ry]:
            self.par[rx] = ry
            self.weight[rx] = w - self.weight[x] + self.weight[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)

    def diff(self, x, y):
        return self.weight[x] - self.weight[y]
uf=WeightedUnionFind(n)
for i in range(n-1):
    uf.union(uvw[i][0],uvw[i][1],uvw[i][2])

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