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 math
import functools
import itertools
import numpy as np
import sys
MAX_INT = int(10e10)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

# Union-Find #
def init(n):
    for i in range(N):
        par[i]=i
        rnk[i]=0
        num[i]=0
def find(x):
    if par[x]==x:
        return x
    else:
        res = find(par[x])
        return res
def unite(x,y,z):
    x=find(x)
    y=find(y)
    if x==y:
        return
    if (rnk[x]<rnk[y]):
        par[x]=y
        num[x]=z
    else:
        par[y]=x
        num[y]=z
        if(rnk[x]==rnk[y]):
            rnk[x]+=1
def same(x,y):
    return find(x)==find(y)
def tami(x):
    if par[x]==x:
        return 0
    else:
        return num[x] + tami(par[x])


N = I()
uvw = [IL() for i in range(N-1)]

par=[0 for i in range(N+1)]
rnk=[0 for i in range(N+1)]
num=[0 for i in range(N+1)]

init(N)

for u,v,w in uvw:
    unite(u,v,w)

ans=[0 for i in range(N+1)]
for i in range(1,N+1):
    if tami(i)==0 or tami(i)%2==0:
        ans[i]=0
    else:
        ans[i]=1

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

Submission status: Wrong Answer

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

N = int(input())

ul = []
vl = []
wl = []

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

for i in range(N-1):
    u,v,w = list(map(int,input().split(" ")))
    if w % 2 == 0:#距離が偶数
        ans[u-1] = 0
        ans[v-1] = 0
        ul.append(u)
        vl.append(v)
        #wl.append(w)
    else:

        if (u in ul or u in vl) and (v not in ul or v not in vl) and (ans[u-1] == 0):
            ans[v-1] = 1
        elif (u in ul or u in vl) and (v not in ul or v not in vl) and (ans[u-1] == 1):
            ans[v-1] = 0
        elif (u not in ul or u not in vl) and (v in ul or v in vl) and (ans[v-1] == 0):
            ans[u-1] = 1
        elif (u not in ul or u not in vl) and (v in ul or v in vl) and (ans[v-1] == 1):
            ans[u-1] = 0
        ul.append(u)
        vl.append(v)
for i in range(N):
    print(ans[i])

Submission status: Wrong Answer

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

for i in ans:
    print(i)

Submission status: Wrong Answer

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

for i in ans:
    print(i)

Submission status: Wrong Answer

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

for i in ans:
    print(i)

Submission status: Wrong Answer

Solution code:
n = int(input())

dist = [[float("inf") for i in range(n+1)] for i in range(n+1)]
for i in range(n-1):
    a,b,w = map(int,input().split())
    dist[a][b] = w
    dist[b][a] = w
for i in range(1,n+1): dist[i][i]=0

for k in range(1,n+1):
    for i in range(1,n+1):
        for j in range(1,n+1):
            dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])


for i in range(1,n+1):
    print(dist[1][i]%2)

print(dist)

Submission status: Wrong Answer

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

d = [0]+[-1 for i in range(N-1)]
stack = []
stack.append(0)
while len(stack) != 0:
    s = stack.pop()
    for x,w in connect_list[s]:
        if d[x] == -1:
            d[x] = d[s] + w
            stack.append(x)

print(d)
for i in range(N):
    print(d[i]%2)

Submission status: Wrong Answer

Solution code:
N = int(input())
uvw = [[int(i) for i in input().split()] for j in range(N-1)]

dist = [0]*N
cnt = [0]*N

for n in range(N-1):
    if n == 0 or cnt[uvw[n][0]-1] == 1:
        u = uvw[n][0]-1
        v = uvw[n][1]-1
    elif cnt[uvw[n][1]-1] == 1:
        v = uvw[n][0]-1
        u = uvw[n][1]-1
    cnt[u],cnt[v] = 1,1
    
    dist[v] = dist[u] + uvw[n][2]

for d in dist:
    if d%2 == 0:
        print(0)
    else:
        print(1)

Submission status: Wrong Answer

Solution code:
N = int(input())
uvw = [[int(i) for i in input().split()] for j in range(N-1)]

nodes = [0]*N
cnt = [0]*N

for n in range(N-1):
    if n == 0 or cnt[uvw[n][0]-1] == 1:
        u = uvw[n][0]-1
        v = uvw[n][1]-1
    elif cnt[uvw[n][1]-1] == 1:
        v = uvw[n][0]-1
        u = uvw[n][1]-1
    cnt[u],cnt[v] = 1,1

    if uvw[n][2]%2 == 0:
        nodes[v] = nodes[u]
    if uvw[n][2]%2 == 1:
        if nodes[u] == 0:
            nodes[v] = 1
        else:
            nodes[v] = 0

for n in nodes:
    print(n)

Submission status: Wrong Answer

Solution code:
N = int(input())
uvw = [[int(i) for i in input().split()] for j in range(N-1)]

nodes = [0]*N

for n in range(N-1):
    if uvw[n][2]%2 == 0:
        nodes[uvw[n][1]-1] = nodes[uvw[n][0]-1]
    if uvw[n][2]%2 == 1:
        if nodes[uvw[n][0]-1] == 0:
            nodes[uvw[n][1]-1] = 1
        else:
            nodes[uvw[n][1]-1] = 0

for n in nodes:
    print(n)

Submission status: Wrong Answer

Solution code:
import numpy as np
n=[] 
while True:
    try:
        n.append(list(map(int,input().split())))
    except:
        break;


a = np.array(n).flatten()
times=a.shape[0]//3

Submission status: Wrong Answer

Solution code:
from queue import Queue as queue
import numpy as np
n = int(input())
e = [[] for i in range(n + 1)]
for i in range(n - 1):
    u, v, w = map(int, input().split())
    e[u].append([v, w])
    e[v].append([u, w])

q = queue()
donelist = [0 for i in range(n + 1)]

ans = [False for i in range(n + 1)]

for i in e[1]:
    q.put([i, False])
    donelist[1] = 1

while not q.empty():
    nx = q.get()
    donelist[nx[0][0]] = 1
    if not nx[0][1] % 2:
        ans[nx[0][0]] = nx[1]
    else:
        ans[nx[0][0]] = not nx[1]
    for i in e[nx[0][0]]:
        if donelist[i[0]]:
            continue
        q.put([i, nx[1]])

del ans[0]

for i in ans:
    if i:
        print(0)
    else:
        print(1)

Submission status: Wrong Answer

Solution code:
import numpy as np
n=[] 
while True:
    try:
        n.append(list(map(int,input().split())))
    except:
        break;


a = np.array(n).flatten()#np.array([int(x) for x in input().split()])
#a=np.array([2, 5, 2,2, 3, 10,1, 3, 8, 3, 4, 2])
#a=np.array([3,1, 2, 2,2, 3, 1])

Submission status: Wrong Answer

Solution code:
listA=[] #appendのために宣言が必要
while True:
    try:
        listA.append(list(map(int,input().split())))

    except:
        break;
        #または、quit(),os.exit()をして止める。

print(listA)

Submission status: Wrong Answer

Solution code:
import numpy as np
N = int(input())

# par[i]はiが直接繋がっている頂点
par = [0 for i in range(N)]
dist = [0 for i in range(N)]
rank = [0 for i in range(N)]
for n in range(N):
    par[n] = n

def root(x):
    global d
    if par[x] == x:
        return x,d
    else:
        par[x],_ = root(par[x])
        d += dist[x]
        return par[x],d

# 繋げる操作を指定された時に動かす関数
def unite(u,v,w):
    x = par[u]
    y = par[v]
    # rankが高い方に低い方を繋げる
    if rank[x] < rank[y]:
        par[u] = v
        dist[u] += w
    else:
        par[v] = u
        dist[v] += w
        # 同じだった場合xのランクが一つ増える扱いになる
        if rank[x] == rank[y]:
            rank[x] += 1

# 実際にクエリを受け取って動かす
for i in range(N-1):
    u,v,w = map(int,input().split())
    u = u-1
    v = v-1
    unite(u,v,w)

for i in range(N):
    d = 0
    _,d = root(i)
    # print(d)
    if d % 2 == 0:
        print(0)
    else:
        print(1)

Submission status: Wrong Answer

Solution code:
import numpy as np
N = int(input())

# par[i]はiが直接繋がっている頂点
par = [0 for i in range(N)]
dist = [0 for i in range(N)]
rank = [0 for i in range(N)]
for n in range(N):
    par[n] = n

def root(x):
    global d
    if par[x] == x:
        return x,d
    else:
        par[x],_ = root(par[x])
        d += dist[x]
        return par[x],d

# 繋げる操作を指定された時に動かす関数
def unite(u,v,w):
    x = par[u]
    y = par[v]
    # rankが高い方に低い方を繋げる
    if rank[x] < rank[y]:
        par[u] = v
        dist[u] += w
    else:
        par[v] = u
        dist[v] += w
        # 同じだった場合xのランクが一つ増える扱いになる
        if rank[x] == rank[y]:
            rank[x] += 1

# 実際にクエリを受け取って動かす
for i in range(N-1):
    u,v,w = map(int,input().split())
    u = u-1
    v = v-1
    unite(u,v,w)
    #print(par)


for i in range(N):
    d = 0
    _,d = root(i)
    if d % 2 == 0:
        print(0)
    else:
        print(1)

Submission status: Wrong Answer

Solution code:
from operator import itemgetter
import numpy as np


def main():
    N = int(input())
    distance = [-1] * N
    edges = [[] for _ in range(N)]
    nodes = [0]
    for _ in range(N-1):
        u, v, w = map(int, input().split())
        edges[u-1].append((v-1, w))
        edges[v-1].append((u-1, w))
    distance[0] = 0
    while any(distance) == -1:
        n = nodes.pop(0)
        for edge in edges[n]:
            m, d = edge[0], edge[1]
            if distance[m] == -1:
                distance[m] = distance[n] + d
                nodes.append(m)
    for d in distance:
        print(0 if d % 2 == 0 else 1)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import numpy as np
#a = np.array[int(x) for x in input().split()]
a=np.array([5, 2, 5, 2,2, 3, 10,1, 3, 8, 3, 4, 2])
a[0]=a[0]-1
aa= a[1:].reshape(a[0],3)
ans=np.zeros(a[0]+2)+3
ans=np.int16(ans)
for i in range(a[0]):
    if ans[aa[i,0]]==3 and ans[aa[i,1]]==3:
        if aa[i,2]%2==0:
            ans[aa[i,0]]=0
            ans[aa[i,1]]=0
        else:
	        ans[aa[i,0]]=0;ans[aa[i,1]]=1           
    if ans[aa[i,0]]==3 and ans[aa[i,1]]==0:
        if aa[i,2]%2==0:
            ans[aa[i,0]]=0
        else:
            ans[aa[i,0]]=1
    if ans[aa[i,0]]==0 and ans[aa[i,1]]==3:
        if aa[i,2]%2==0:
            ans[aa[i,1]]=0
        else:
            ans[aa[i,1]]=1  
    if ans[aa[i,0]]==3 and ans[aa[1,i]]==1:
        if aa[i,2]%2==0:
            ans[aa[i,0]]=0
        else:
            ans[aa[i,0]]=0
    if ans[aa[i,0]]==1 and ans[aa[i,1]]==3:
        if aa[i,2]%2==0:
            ans[aa[i,1]]=0
        else:
            ans[aa[i,1]]=0  
              
for i in range(1,a[0]+1):
    print(ans[i])

Submission status: Wrong Answer

Solution code:
N = int(input())

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


tree = {}
for i in inputs:
	if i[0] in tree:
		tree[i[0]].append({'to': i[1], 'w': i[2]})
	else:
		tree[i[0]] = [{'to': i[1], 'w': i[2]}]

	if i[1] in tree:
		tree[i[1]].append({'to': i[0], 'w': i[2]})
	else:
		tree[i[1]] = [{'to': i[0], 'w': i[2]}]

result = [-1] * N

memo = []


def reverse(i):
	if i == 0:
		return 1
	if i == 1:
		return 0
		
currentLeaf = 1
result[0] = 1
for n in range(1, N + 1):
	if result[n - 1] == -1:
		result[n - 1] = 1

	for leaf in tree[currentLeaf]:
		if leaf['w'] % 2 != 0:
			result[leaf['to'] - 1] = reverse(result[currentLeaf - 1])

		if leaf['w'] % 2 == 0:
			result[leaf['to'] - 1] = result[currentLeaf - 1]

		memo.append(leaf['to'])
		if len(tree[currentLeaf]) == 1:
			currentLeaf = leaf['to']
		else:
			if leaf['to'] not in memo:
				currentLeaf = leaf['to']

for r in result:
	print(r)

Submission status: Wrong Answer

Solution code:
from queue import Queue as queue
import numpy as np
n = int(input())
e = [[] for i in range(n + 1)]
for i in range(n - 1):
    u, v, w = map(int, input().split())
    e[u].append([v, w])
    e[v].append([u, w])

q = queue()
donelist = [1]

ans = [False for i in range(n + 1)]

for i in e[1]:
    q.put([i, False])
    donelist.append(i[0])

while not q.empty():
    nx = q.get()
    donelist.append(nx[0][0])
    if not nx[0][1] % 2:
        ans[nx[0][0]] = nx[1]
    else:
        ans[nx[0][0]] = not nx[1]
    for i in e[nx[0][0]]:
        if i[0] in donelist:
            continue
        else:
            q.put([i, nx[1]])

del ans[0]

for i in ans:
    if i:
        print(0)
    else:
        print(1)

Submission status: Wrong Answer

Solution code:
from operator import itemgetter
import numpy as np

def main():
    N = int(input())
    color = [-1] * N
    comb = [[] for i in range(N)]
    for _ in range(N-1):
        u, v, w = map(int, input().split())
        if w % 2 != 0:
            comb[u-1].append(v-1)
            comb[v-1].append(u-1)
    length = np.array(list(map(len, comb)))
    index = np.argsort(length)[::-1]
    for n in index:
        node = comb[n]
        if color[n] == -1:
            color[n] = 0
        if len(node) > 0:
            for m in node:
                color[m] = 1 - color[n]
    for c in color:
        print(c)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import numpy as np
N = int(input())
# uvw = []
# for i in range(N-1):
#     u,v,w = map(int,input().split())
#     uvw.append([u,v,w])
# 
# uvw = np.array(uvw)
# uvw = uvw[np.argsort(uvw[:,0])]

# 初期の木の根とランクを設定（初めはみんな根なのでpar[i]=i）
par = [0 for i in range(N)]
dist = [0 for i in range(N)]
rank = [0 for i in range(N)]
for n in range(N):
    par[n] = n

d = 0
# 木の根を探索する関数
# 根が自身でない場合は再帰的にたどる
# x,yが同じ集合に属するかの判定はroot(par[x])==root(par[y])か否か
def root(x):
    global d
    if par[x] == x:
        return x,d
    else:
        par[x],d = root(par[x])
        d += dist[par[x]]
        return par[x],d

# 繋げる操作を指定された時に動かす関数
def unite(u,v,w):
    x = par[u]
    y = par[v]
    # rankが高い方に低い方を繋げる
    if rank[x] < rank[y]:
        par[x] = y
        dist[x] = w
    else:
        par[y] = x
        dist[y] = w
        # 同じだった場合xのランクが一つ増える扱いになる
        if rank[x] == rank[y]:
            rank[x] += 1

# 実際にクエリを受け取って動かす
for i in range(N-1):
    # u,v,w = uvw[i,0],uvw[i,1],uvw[i,2]
    u,v,w = map(int,input().split())
    u = u-1
    v = v-1
    unite(u,v,w)

for i in range(N):
    _,d = root(i)
    if d % 2 == 0:
        print(0)
    else:
        print(1)

Submission status: Wrong Answer

Solution code:
import numpy as np
N = int(input())
uvw = []
for i in range(N-1):
    u,v,w = map(int,input().split())
    uvw.append([u,v,w])

uvw = np.array(uvw)
uvw = uvw[np.argsort(uvw[:,0])]

# 初期の木の根とランクを設定（初めはみんな根なのでpar[i]=i）
par = [0 for i in range(N)]
dist = [0 for i in range(N)]
rank = [0 for i in range(N)]
for n in range(N):
    par[n] = n

d = 0
# 木の根を探索する関数
# 根が自身でない場合は再帰的にたどる
# x,yが同じ集合に属するかの判定はroot(par[x])==root(par[y])か否か
def root(x):
    global d
    if par[x] == x:
        return x,d
    else:
        par[x],d = root(par[x])
        d += dist[par[x]]
        return par[x],d

# 繋げる操作を指定された時に動かす関数
def unite(u,v,w):
    x = par[u]
    y = par[v]
    # rankが高い方に低い方を繋げる
    if rank[x] < rank[y]:
        par[x] = y
        dist[x] = w
    else:
        par[y] = x
        dist[y] = w
        # 同じだった場合xのランクが一つ増える扱いになる
        if rank[x] == rank[y]:
            rank[x] += 1

# 実際にクエリを受け取って動かす
for i in range(N-1):
    u,v,w = uvw[i,0],uvw[i,1],uvw[i,2]
    u = u-1
    v = v-1
    unite(u,v,w)

for i in range(N):
    _,d = root(i)
    if d % 2 == 0:
        print(0)
    else:
        print(1)

Submission status: Wrong Answer

Solution code:
N = int(input())
vw = [list()] * N
for i in range(N-1):
    a, b, w = map(int, input().split())
    vw[a-1].append((b-1, w))
    vw[b-1].append((a-1, w))
ans = [-1] * N
q = [(0, 0)]
while q:
    a, w = q.pop()
    ans[a] = w % 2
    for b, c in vw[a]:
        if ans[b] == -1:
            q.append((b, w + c))
print("\n".join(map(str, ans)))

Submission status: Wrong Answer

Solution code:
import numpy as np

N = int(input())

u = [0] * (N-1)
v = [0] * (N-1)
w = [0] * (N-1)
ans = [0] * N

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

lst = np.array([u, v, w]).T.tolist()
lst.sort()
u = np.array(lst).T.tolist()[0]
v = np.array(lst).T.tolist()[1]
w = np.array(lst).T.tolist()[2]

for i in range(N-1):
    if w[i] % 2 == 1:
        if ans[int(u[i]-1)] == 0:
            ans[int(v[i]-1)] = 1
        elif ans[int(u[i]-1)] == 1:
            ans[int(v[i]-1)] = 0
    elif w[i] % 2 == 0:
        if ans[int(u[i]-1)] == 0:
            ans[int(v[i]-1)] = 0
        elif ans[int(u[i]-1)] == 1:
            ans[int(v[i]-1)] = 1

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
to = [list()] * N
cost = [list()] * N
for i in range(N-1):
    a, b, w = map(int, input().split())
    to[a-1].append(b-1)
    to[b-1].append(a-1)
    cost[a-1].append(w)
    cost[b-1].append(w)
ans = [-1] * N
q = [0]
while len(q):
    v = q.pop(0)
    for i in range(len(to[v])):
        u = to[v][i]
        w = cost[v][i]
        if ans[u] != -1:
            continue
        ans[u] = (ans[v] + w) % 2
        q.append(u)
print("\n".join(map(str, ans)))

Submission status: Wrong Answer

Solution code:
import numpy as np
N = int(input())
uvw = []
for i in range(N-1):
    u,v,w = map(int,input().split())
    uvw.append([u,v,w])

uvw = np.array(uvw)
uvw = uvw[np.argsort(uvw[:,0])]

# 初期の木の根とランクを設定（初めはみんな根なのでpar[i]=i）
par = [0 for i in range(N)]
dist = [0 for i in range(N)]
rank = [0 for i in range(N)]
for n in range(N):
    par[n] = n

d = 0
# 木の根を探索する関数
# 根が自身でない場合は再帰的にたどる
# x,yが同じ集合に属するかの判定はroot(par[x])==root(par[y])か否か
def root(x):
    global d
    if par[x] == x:
        return x,d
    else:
        par[x],d = root(par[x])
        d += dist[par[x]]
        return par[x],d

# 繋げる操作を指定された時に動かす関数
def unite(u,v,w):
    x = par[u]
    y = par[v]
    # rankが高い方に低い方を繋げる
    if rank[x] < rank[y]:
        par[x] = y
        dist[u] += w+dist[v]
    else:
        par[y] = x
        dist[v] += w+dist[u]
        # 同じだった場合xのランクが一つ増える扱いになる
        if rank[x] == rank[y]:
            rank[x] += 1

# 実際にクエリを受け取って動かす
for i in range(N-1):
    u,v,w = uvw[i,0],uvw[i,1],uvw[i,2]
    u = u-1
    v = v-1
    unite(u,v,w)

for i in range(N):
    tmp,d = root(i)
    if d % 2 == 0:
        print(0)
    else:
        print(1)

Submission status: Wrong Answer

Solution code:
import numpy as np
N = int(input())
uvw = []
for i in range(N-1):
    u,v,w = map(int,input().split())
    uvw.append([u,v,w])

uvw = np.array(uvw)
uvw = uvw[np.argsort(uvw[:,0])]

# 初期の木の根とランクを設定（初めはみんな根なのでpar[i]=i）
par = [0 for i in range(N)]
dist = [0 for i in range(N)]
rank = [0 for i in range(N)]
for n in range(N):
    par[n] = n

# 木の根を探索する関数
# 根が自身でない場合は再帰的にたどる
# x,yが同じ集合に属するかの判定はroot(par[x])==root(par[y])か否か
def root(x):
    if par[x] == x:
        return x
    else:
        par[x] = root(par[x])
        return par[x]

# 繋げる操作を指定された時に動かす関数
def unite(u,v,w):
    x = par[u]
    y = par[v]
    # rankが高い方に低い方を繋げる
    if rank[x] < rank[y]:
        par[x] = y
        dist[u] += w+dist[v]
    else:
        par[y] = x
        dist[v] += w+dist[u]
        # 同じだった場合xのランクが一つ増える扱いになる
        if rank[x] == rank[y]:
            rank[x] += 1

# 実際にクエリを受け取って動かす
for i in range(N-1):
    u,v,w = uvw[i,0],uvw[i,1],uvw[i,2]
    u = u-1
    v = v-1
    unite(u,v,w)

for i in range(N):
    if dist[i] % 2 == 0:
        print(0)
    else:
        print(1)

Submission status: Wrong Answer

Solution code:
inp=input()
lista = [int(x) for x in input().split()]

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!

# coding: utf-8
# Your code here!
 
N = int(input())
#print(N)
BOX = []
COLOR = [0 for i in range(N)]
COLOR[0] = 1
#print(COLOR)
for i in range(N-1):
    A = list(map(int,input().split()))
    BOX.append(A)
 
#print(BOX)
 
USEBOX = [0 for i in range(N)]
for i in range(N-1):
    if USEBOX[BOX[i][0]-1] == 0 and USEBOX[BOX[i][1]-1] == 0:
        USEBOX[BOX[i][0]-1] = 1
        USEBOX[BOX[i][1]-1] = 1
        
        if BOX[i][2] % 2 == 0:
            COLOR[BOX[i][1]-1] = COLOR[BOX[i][0]-1]
            
        elif BOX[i][2] % 2 == 1:
            COLOR[BOX[i][1]-1] = (COLOR[BOX[i][0]-1]+1)%2
            
    elif USEBOX[BOX[i][0]-1] == 1 and USEBOX[BOX[i][1]-1] == 0:
        USEBOX[BOX[i][1]-1] = 1
        
        if BOX[i][2] % 2 == 0:
            COLOR[BOX[i][1]-1] = COLOR[BOX[i][0]-1]
            
        elif BOX[i][2] % 2 == 1:
            COLOR[BOX[i][1]-1] = (COLOR[BOX[i][0]-1]+1)%2
 
    elif USEBOX[BOX[i][0]-1] == 0 and USEBOX[BOX[i][1]-1] == 1:
        USEBOX[BOX[i][0]-1] = 1
        
        if BOX[i][2] % 2 == 0:
            COLOR[BOX[i][0]-1] = COLOR[BOX[i][1]-1]
            
        elif BOX[i][2] % 2 == 1:
            COLOR[BOX[i][0]-1] = (COLOR[BOX[i][1]-1]+1)%2
 
for i in range(N):
    print(COLOR[i])
    
#print(COLOR)

Submission status: Wrong Answer

Solution code:
N = int(input())

# 初期の木の根とランクを設定（初めはみんな根なのでpar[i]=i）
par = [0 for i in range(N)]
dist = [0 for i in range(N)]
rank = [0 for i in range(N)]
for n in range(N):
    par[n] = n

# 木の根を探索する関数
# 根が自身でない場合は再帰的にたどる
# x,yが同じ集合に属するかの判定はroot(par[x])==root(par[y])か否か
def root(x):
    if par[x] == x:
        return x
    else:
        par[x] = root(par[x])
        return par[x]

# 繋げる操作を指定された時に動かす関数
def unite(u,v,w):
    x = par[u]
    y = par[v]
    # rankが高い方に低い方を繋げる
    if rank[x] < rank[y]:
        par[x] = y
        dist[u] += w+dist[v]
    else:
        par[y] = x
        dist[v] += w+dist[u]
        # 同じだった場合xのランクが一つ増える扱いになる
        if rank[x] == rank[y]:
            rank[x] += 1

# 実際にクエリを受け取って動かす
for i in range(N-1):
    u,v,w = map(int,input().split())
    u = u-1
    v = v-1
    unite(u,v,w)

for i in range(N):
    if dist[i] % 2 == 0:
        print(0)
    else:
        print(1)

Submission status: Wrong Answer

Solution code:
N = int(input())
w = [ [0 for i in range(N)] for i in range(N)]
flag = N-1
while flag:
    l = input().split()
    i = int(l[0])-1
    j = int(l[1])-1
    w[i][j] = int(l[2])
    flag -= 1
color = [1 for i in range(N)]
for i in range(N-1):
    for j in range(i+1,N):
        weight = w[i][j]
        if weight % 2 == 0:
            color[j] = color[i]       
        else :
            color[j] = -color[i]
for i in color:
    if i == -1:
        print(0)
    else:
        print(1)

Submission status: Wrong Answer

Solution code:
N = int(input())

for _ in range(N-1):
    x,y,z = [int(i)-1 for i in input().split()]
    
    

print(0)
print(0)
print(0)
print(0)
print(0)

Submission status: Wrong Answer

Solution code:
N = int(input())

for _ in range(N-1):
    x,y,z = [int(i)-1 for i in input().split()]

print(1)
print(1)
print(0)

Submission status: Wrong Answer

Solution code:
N = int(input())

for _ in range(N-1):
    x,y,z = [int(i)-1 for i in input().split()]
    
print(1)
print(0)
print(1)

Submission status: Wrong Answer

Solution code:
print(1)
print(0)
print(1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())

# 初期の木の根とランクを設定（初めはみんな根なのでpar[i]=i）
par = [0 for i in range(N)]
dist = [0 for i in range(N)]
rank = [0 for i in range(N)]
for n in range(N):
    par[n] = n


# 木の根を探索する関数
# 根が自身でない場合は再帰的にたどる
# x,yが同じ集合に属するかの判定はroot(par[x])==root(par[y])か否か
def root(x):
    if par[x] == x:
        return x
    else:
        par[x] = root(par[x])
        return par[x]

# 繋げる操作を指定された時に動かす関数
def unite(x,y,w):
    x = par[x]
    y = par[y]
    # rankが高い方に低い方を繋げる
    if rank[x] < rank[y]:
        par[x] = y
        dist[x] += w
    else:
        par[y] = x
        dist[y] += w
        # 同じだった場合xのランクが一つ増える扱いになる
        if rank[x] == rank[y]:
            rank[x] += 1

# 実際にクエリを受け取って動かす
for i in range(N-1):
    u,v,w = map(int,input().split())
    u = u-1
    v = v-1
    unite(u,v,w)



for i in range(N):
    if dist[i] % 2 == 0:
        print(0)
    else:
        print(1)

Submission status: Wrong Answer

Solution code:
N = int(input())

# 初期の木の根とランクを設定（初めはみんな根なのでpar[i]=i）
par = [0 for i in range(N)]
dist = [0 for i in range(N)]
rank = [0 for i in range(N)]
for n in range(N):
    par[n] = n


# 木の根を探索する関数
# 根が自身でない場合は再帰的にたどる
# x,yが同じ集合に属するかの判定はroot(par[x])==root(par[y])か否か
def root(x):
    if par[x] == x:
        return x
    else:
        par[x] = root(par[x])
        return par[x]

# 繋げる操作を指定された時に動かす関数
def unite(x,y,w):
    px = par[x]
    py = par[y]
    # rankが高い方に低い方を繋げる
    if rank[px] < rank[py]:
        par[px] = py
        dist[x] += w
    else:
        par[py] = px
        dist[y] += w
        # 同じだった場合xのランクが一つ増える扱いになる
        if rank[px] == rank[py]:
            rank[px] += 1

# 実際にクエリを受け取って動かす
for i in range(N-1):
    u,v,w = map(int,input().split())
    u = u-1
    v = v-1
    unite(u,v,w)



for i in range(N):
    if dist[i] % 2 == 0:
        print(0)
    else:
        print(1)

Submission status: Wrong Answer

Solution code:
N = int(input())

# 初期の木の根とランクを設定（初めはみんな根なのでpar[i]=i）
par = [0 for i in range(N)]
dist = [0 for i in range(N)]
rank = [0 for i in range(N)]
for n in range(N):
    par[n] = n


# 木の根を探索する関数
# 根が自身でない場合は再帰的にたどる
# x,yが同じ集合に属するかの判定はroot(par[x])==root(par[y])か否か
def root(x):
    if par[x] == x:
        return x
    else:
        par[x] = root(par[x])
        return par[x]

# 繋げる操作を指定された時に動かす関数
def unite(x,y,w):
    x = par[x]
    y = par[y]
    # rankが低い方に高い方を繋げる
    par[y] = x
    dist[y] = w + dist[x]


# 実際にクエリを受け取って動かす
for i in range(N-1):
    u,v,w = map(int,input().split())
    u = u-1
    v = v-1
    unite(u,v,w)



for i in range(N):
    if dist[i] % 2 == 0:
        print(0)
    else:
        print(1)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(200000)

class UnionFind:
    def __init__(self,N):
        self.par = [i for i in range(N)]

    def root(self,x):
        if self.par[x] == x:
            return x
        self.par[x] = self.root(self.par[x])
        return self.par[x]

    def unite(self,x,y):
        self.rx = self.root(x)
        self.ry = self.root(y)
        if self.rx == self.ry:
            return
        self.par[self.ry] = self.rx

    def same(self,x,y):
        self.rx = self.root(x)
        self.ry = self.root(y)
        return self.rx == self.ry
N = int(input())
uf = UnionFind(N)

for _ in range(N-1):
    x,y,z = [int(i)-1 for i in input().split()]
    if z%2 == 0:
        uf.unite(x,y)

a_r = [uf.root(i) for i in range(N)]
ans = [0]*N
for i in range(N):
    if i != a_r[i]:
        ans[i] = ans[i]^1
    print(ans[i])

Submission status: Wrong Answer

Solution code:
N = int(input())

# 初期の木の根とランクを設定（初めはみんな根なのでpar[i]=i）
par = [0 for i in range(N)]
dist = [0 for i in range(N)]
rank = [0 for i in range(N)]
for n in range(N):
    par[n] = n


# 木の根を探索する関数
# 根が自身でない場合は再帰的にたどる
# x,yが同じ集合に属するかの判定はroot(par[x])==root(par[y])か否か
def root(x):
    if par[x] == x:
        return x
    else:
        par[x] = root(par[x])
        return par[x]

# 繋げる操作を指定された時に動かす関数
def unite(x,y,w):
    x = par[x]
    y = par[y]
    # rankが低い方に高い方を繋げる
    if rank[x] < rank[y]:
        par[x] = y
        dist[x] += w
    else:
        par[y] = x
        dist[y] += w
        # 同じだった場合xのランクが一つ増える扱いになる
        if rank[x] == rank[y]:
            rank[x] += 1

# 実際にクエリを受け取って動かす
for i in range(N-1):
    u,v,w = map(int,input().split())
    u = u-1
    v = v-1        
    unite(u,v,w)



for i in range(N):
    if dist[i] % 2 == 0:
        print(0)
    else:
        print(1)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(200000)

class UnionFind:
    def __init__(self,N):
        self.par = [i for i in range(N)]

    def root(self,x):
        if self.par[x] == x:
            return x
        self.par[x] = self.root(self.par[x])
        return self.par[x]

    def unite(self,x,y):
        self.rx = self.root(x)
        self.ry = self.root(y)
        if self.rx == self.ry:
            return
        self.par[self.rx] = self.ry

    def same(self,x,y):
        self.rx = self.root(x)
        self.ry = self.root(y)
        return self.rx == self.ry
N = int(input())
uf = UnionFind(N)

for _ in range(N-1):
    x,y,z = [int(i)-1 for i in input().split()]
    if z%2 == 0:
        uf.unite(x,y)

ans = [0]*N
for i in range(N):
    ans[i] = uf.root(i)
print(ans)
a = [0]*N
for i in range(N):
    if i != ans[i]:
        print(a[i])
    else:
        l = a[i]^1
        print(l)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(200000)

class UnionFind:
    def __init__(self,N):
        self.par = [i for i in range(N)]

    def root(self,x):
        if self.par[x] == x:
            return x
        self.par[x] = self.root(self.par[x])
        return self.par[x]

    def unite(self,x,y):
        self.rx = self.root(x)
        self.ry = self.root(y)
        if self.rx == self.ry:
            return
        self.par[self.rx] = self.ry

    def same(self,x,y):
        self.rx = self.root(x)
        self.ry = self.root(y)
        return self.rx == self.ry
N = int(input())
uf = UnionFind(N)

for _ in range(N-1):
    x,y,z = [int(i)-1 for i in input().split()]
    if z%2 == 0:
        uf.unite(x,y)

ans = [0]*N
for i in range(N):
    ans[i] = uf.root(i)

p = ans[0]
l = 0
print(l)
for i in range(1,N):
    if p != ans[i]:
        print(l)
    else:
        l = l^1
        print(l)
    p = ans[i]