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
readline = sys.stdin.readline

n = int(input())
tree = [[] for _ in range(n+1)]
for i in range(n-1):
    u1, u2, l = list(map(int, readline().split()))
    tree[u1].append([u2, l])

ans = [0 for _ in range(n+1)]
for i in range(n+1):
    if len(tree[i]) == 0: continue
    for j in range(len(tree[i])):
        if tree[i][j][1] % 2 == 0:
            ans[i] = 1
            ans[tree[i][j][0]] = 1
        else:
            if ans[i] == 1:
              ans[tree[i][j][0]] = 0
            else:
              ans[tree[i][j][0]] = 1
# print(tree)
# print(ans)

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

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
import queue

def getlist():
	return list(map(int, input().split()))

class Graph(object):
    def __init__(self):
        self.graph = defaultdict(list)

    def __len__(self):
        return len(self.graph)

    def add_edge(self, a, b, w):
        self.graph[a].append([b, w])
        self.graph[b].append([a,w])

    def get_nodes(self):
        return self.graph.keys()

class BFS(object):
    def __init__(self, graph, N):
        self.g = graph.graph
        self.dist = [0] * (N + 1)
        self.visit = [0] * (N + 1)
        self.Q = queue.Queue()
        self.visit[1] = 1
        self.Q.put(1)

        while not self.Q.empty():
            v = self.Q.get()
            for i, w in self.g[v]:
            	if self.visit[i] == 0:
            		self.dist[i] = self.dist[v] + w
            		self.Q.put(i)
            		self.visit[i] = 1

    def weight(self):
        return self.dist

N = int(input())
G = Graph()
for i in range(N - 1):
	u, v, w = getlist()
	G.add_edge(u, v, w)
XX = BFS(G, N)
ans = XX.weight()
for i in range(1, N + 1):
	if ans[i] % 2 == 0:
		print(i,0)
	else:
		print(i,1)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
import queue

def getlist():
	return list(map(int, input().split()))

class Graph(object):
    def __init__(self):
        self.graph = defaultdict(list)

    def __len__(self):
        return len(self.graph)

    def add_edge(self, a, b, w):
        self.graph[a].append([b, w])

    def get_nodes(self):
        return self.graph.keys()

class BFS(object):
    def __init__(self, graph, N):
        self.g = graph.graph
        self.dist = [0] * (N + 1)
        self.visit = [0] * (N + 1)
        self.Q = queue.Queue()
        self.visit[1] = 1
        self.Q.put(1)

        while not self.Q.empty():
            v = self.Q.get()
            for i, w in self.g[v]:
            	if self.visit[i] == 0:
            		self.dist[i] = self.dist[v] + w
            		self.Q.put(i)
            		self.visit[i] = 1

    def weight(self):
        return self.dist

N = int(input())
G = Graph()
for i in range(N - 1):
	u, v, w = getlist()
	G.add_edge(u, v, w)
XX = BFS(G, N)
ans = XX.weight()
for i in range(1, N + 1):
	if ans[i] % 2 == 0:
		print(0)
	else:
		print(1)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
import queue

def getlist():
	return list(map(int, input().split()))

class Graph(object):
    def __init__(self):
        self.graph = defaultdict(list)

    def __len__(self):
        return len(self.graph)

    def add_edge(self, a, b, w):
        self.graph[a].append([b, w])

    def get_nodes(self):
        return self.graph.keys()

class BFS(object):
    def __init__(self, graph, N):
        self.g = graph.graph
        self.dist = [0] * (N + 1)
        self.visit = [0] * (N + 1)
        self.Q = queue.Queue()
        self.Q.put(1)

        while not self.Q.empty():
            v = self.Q.get()
            for i, w in self.g[v]:
            	if self.visit[i] == 0:
            		self.dist[i] = self.dist[v] + w
            		self.Q.put(i)
            		self.visit[i] = 1

    def weight(self):
        return self.dist

N = int(input())
G = Graph()
for i in range(N - 1):
	u, v, w = getlist()
	G.add_edge(u, v, w)
XX = BFS(G, N)
ans = XX.weight()
for i in range(1, N + 1):
	if ans[i] % 2 == 0:
		print(0)
	else:
		print(1)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
import queue

def getlist():
	return list(map(int, input().split()))

class Graph(object):
    def __init__(self):
        self.graph = defaultdict(list)

    def __len__(self):
        return len(self.graph)

    def add_edge(self, a, b, w):
        self.graph[a].append([b, w])

    def get_nodes(self):
        return self.graph.keys()

class BFS(object):
    def __init__(self, graph, N):
        self.g = graph.graph
        self.dist = [0] * (N + 1)
        self.visit = [0] * (N + 1)
        self.Q = queue.Queue()
        self.Q.put(1)

        while not self.Q.empty():
            v = self.Q.get()
            for i, w in self.g[v]:
            	if self.visit[i] == 0:
            		self.dist[i] = self.dist[v] + w
            		self.Q.put(i)
            	
            	self.visit[i] = 1

    def weight(self):
        return self.dist

N = int(input())
G = Graph()
for i in range(N - 1):
	u, v, w = getlist()
	G.add_edge(u, v, w)
XX = BFS(G, N)
ans = XX.weight()
print(ans)
for i in range(1, N + 1):
	if ans[i] % 2 == 0:
		print(0)
	else:
		print(1)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
import queue

def getlist():
	return list(map(int, input().split()))

class Graph(object):
    def __init__(self):
        self.graph = defaultdict(list)

    def __len__(self):
        return len(self.graph)

    def add_edge(self, a, b, w):
        self.graph[a].append([b, w])

    def get_nodes(self):
        return self.graph.keys()

class BFS(object):
    def __init__(self, graph, N):
        self.g = graph.graph
        self.dist = [0] * (N + 1)
        self.visit = [0] * (N + 1)
        self.Q = queue.Queue()
        self.Q.put(1)

        while not self.Q.empty():
            v = self.Q.get()
            for i, w in self.g[v]:
            	if self.visit[i] == 0:
            		self.dist[i] = self.dist[v] + w

            	self.Q.put(i)
            	self.visit[i] = 1

    def weight(self):
        return self.dist

N = int(input())
G = Graph()
for i in range(N - 1):
	u, v, w = getlist()
	G.add_edge(u, v, w)
XX = BFS(G, N)
ans = XX.weight()

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

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
import queue

def getlist():
	return list(map(int, input().split()))

class Graph(object):
    def __init__(self):
        self.graph = defaultdict(list)

    def __len__(self):
        return len(self.graph)

    def add_edge(self, a, b, w):
        self.graph[a].append([b, w])

    def get_nodes(self):
        return self.graph.keys()

class BFS(object):
    def __init__(self, graph, W, N):
        self.g = graph.graph
        self.W = W
        self.visit = [0] * (N + 1)
        self.Q = queue.Queue()
        self.Q.put(1)

        while not self.Q.empty():
            v = self.Q.get()
            for i, w in self.g[v]:
            	if self.visit[i] == 0:
            		if w % 2 != 0:
            			if self.W[v] == 0:
            				self.W[i] = 1
            		else:
            			if self.W[v] == 1:
            				self.W[i] = 1

            	self.Q.put(i)
            	self.visit[i] = 1


    def weight(self):
        return self.W

N = int(input())
G = Graph()
for i in range(N - 1):
	u, v, w = getlist()
	G.add_edge(u, v, w)
W = [0] * (N + 1)
XX = BFS(G, W, N)
ans = XX.weight()

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

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
import queue

def getlist():
	return list(map(int, input().split()))

class Graph(object):
    def __init__(self):
        self.graph = defaultdict(list)

    def __len__(self):
        return len(self.graph)

    def add_edge(self, a, b, w):
        self.graph[a].append([b, w])

    def get_nodes(self):
        return self.graph.keys()

class BFS(object):
    def __init__(self, graph, W, N):
        self.g = graph.graph
        self.W = W
        self.visit = [0] * (N + 1)
        self.Q = queue.Queue()
        self.Q.put(1)

        while not self.Q.empty():
            v = self.Q.get()
            for i, w in self.g[v]:
            	if self.visit[i] == 0:
            		if w % 2 != 0:
            			if self.W[v] == 0:
            				self.W[i] = 1
            		else:
            			if self.W[v] == 1:
            				self.W[i] = 1

            	self.Q.put(i)
            	self.visit[i] = 1


    def weight(self):
        return self.W

N = int(input())
G = Graph()
for i in range(N - 1):
	u, v, w = getlist()
	G.add_edge(u, v, w)
W = [0] * (N + 1)
XX = BFS(G, W, N)
ans = XX.weight()

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

Submission status: Wrong Answer

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

ln = [0] * n

for v in lv:
	pn = v[0]
	cn = v[1]
	w = v[2]
	
	if w % 2 == 0:
		ln[cn-1] = ln[pn-1]
	else:
		if ln[pn-1] == 0:
			ln[cn-1] = 1
		else:
			ln[cn-1] = 0
		
for c in ln:
	print(c)

Submission status: Wrong Answer

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

ln = [0] * n
visited = [False] * n

stack = [0]

while True:
	node = stack.pop()
	for i in range(n):
		if not visited[i]:
			for v in lv:
				if v[0] == i:
					cn = v[1]
					cw = v[2]
					stack.append(cn)
					if cw % 2 == 1:
						ln[cn-1] = 1
			visited[i] = True
	if len(stack) == 0:
		break

for c in ln:
	print(c)

Submission status: Wrong Answer

Solution code:
n = int(input())
nodes = [[] for _ in range(n+1)]
for _ in range(n-1):
    u, v, w = list(map(int, input().split()))
    nodes[u].append((v, w))
    nodes[v].append((u, w))
    
#print(*nodes, sep='\n')

color = [0]*(n+1)
stack = []
stack.extend(nodes[1])
color[nodes[1][0][0]]

while stack:
    #print(stack)
    nextnode, weight = stack.pop(0)
    if color[nextnode] != 0:
        continue
    if weight % 2 == 0:
        color[nextnode] = 1
        stack.extend(nodes[nextnode])

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

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(500000)

def dfs(v,w):
    if w % 2 == 0:
        color[v] = 0
    else:
        color[v] = 1

    for i in a[v]:
        if color[i[0]] != -1:continue
        dfs(i[0],i[1])

n = int(input())

a = [[]for i in range(n)]
color = [-1 for _ in range(n)]

for i in range(n-1):
    x,y,z = map(int,input().split())
    a[x-1].append([y-1,z])
    a[y-1].append([x-1,z])
for i in range(n):
    for j in range(len(a[i])):
        if color[a[i][j][0]] != -1:continue
        dfs(a[i][j][0],a[i][j][1])
print(*color,sep="\n")

Submission status: Wrong Answer

Solution code:
from collections  import deque
N = int(input())
graph = [[] for _ in range(N)]
for _ in range(N - 1):
    a , b , w = map(int , input().split())
    a -= 1
    b -= 1
    graph[b].append([a,w])
    graph[a].append([b,w])

#print(len(graph.pop()[0]))
#print(graph.pop())
#print(len(graph.pop()))
def dfs():
    q = deque(graph[0])
    color = [0] * N
    corrent_color = 0
    other_color = 1
    searched = [0]
    while q:
        count = len(q)
        #print(count)
        search = q.popleft()
        #print("search : ",search)
        #print("search[0] : ",search[0])
        #print(len(search))
        for i in range(count):

            if not search[0] in searched:
                searched.append(search[0])
                if search[1] % 2 == 0:
                    color[search[0]] = corrent_color
                else:
                    color[search[0]] = other_color
                #print(len(graph[search[0]]))
                #print(graph[search[0]][0])
                for i in range(len(graph[search[0]])):
                    #print("i : ",i)
                    #print(graph[search[0]][i])
                    q.append(graph[search[0]][i])
                #print(q)
        corrent_color = color[search[0]]
        other_color = 1 - corrent_color
    for i in range(len(color)):
        print(color[i])




dfs()
#if __name__ == "__main__":

Submission status: Wrong Answer

Solution code:
from collections import deque


def main():
    def bfs(start):
        q = deque([start])
        while q:
            u = q.popleft()
            for v, w in g[u]:
                if color[v] == -1:
                    q.append(v)
                    if w == 0:
                        color[v] = color[u]
                    else:
                        color[v] = color[u] ^ 1

    n = int(input())
    g = [[] for _ in range(n)]
    color = [-1] * n
    color[0] = 1
    for _ in range(n - 1):
        u, v, w = map(int, input().split())
        u -= 1
        v -= 1
        w %= 2
        g[u].append((v, w))
        g[v].append((u, w))
    start = float("inf")
    for i in range(n - 1):
        if len(g[i]) == 1:
            start = i
            break
    bfs(start)
    print(*color)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque


def bfs(start):
    q = deque([start])
    while q:
        u = q.popleft()
        for v, w in g[u]:
            if color[v] == -1:
                q.append(v)
                if w == 0:
                    color[v] = color[u]
                else:
                    color[v] = color[u] ^ 1


n = int(input())
g = [[] for _ in range(n)]
color = [-1] * n
color[0] = 1
for _ in range(n - 1):
    u, v, w = map(int, input().split())
    u -= 1
    v -= 1
    w %= 2
    g[u].append((v, w))
    g[v].append((u, w))
start = float("inf")
for i in range(n - 1):
    if len(g[i]) == 1:
        start = i
        break
bfs(start)
print(*color)

Submission status: Wrong Answer

Solution code:
n = int(input())

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

from collections import deque
q = deque()
q.append(0)    
seen = [-1]*n
seen[0] = 0  #Node0 White    

while q:
    node = q.popleft()
    for pair,length in graph[node]:
        if seen[pair]==-1:
            q.append(pair)
            if length%2 == 0:
                seen[pair] = 0
            else:
                seen[pair] = 1
                          
for i in range(n):
    print(seen[i])

Submission status: Wrong Answer

Solution code:
n = int(input())

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

from collections import deque
q = deque()
q.append(0)    
seen = [-1]*n
seen[0] = 0  #Node0 White    

while q:
    node = q.pop()
    for pair,length in graph[node]:
        if seen[pair]==-1:
            q.append(pair)
            if length%2 == 0:
                seen[pair] = 0
            else:
                seen[pair] = 1
                          
for i in range(n):
    print(seen[i])

Submission status: Wrong Answer

Solution code:
n = int(input())

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

from collections import deque
q = deque()
q.append(0)    
seen = [-1]*n
seen[0] = 0  #Node0 White    

while q:
    node = q.popleft()
    for pair,length in graph[node]:
        if seen[pair]==-1:
            if length%2 == 0:
                seen[pair] = 0
            else:
                seen[pair] = 1
            q.append(pair)
    
for i in range(n):
    print(seen[i])

Submission status: Wrong Answer

Solution code:
n = int(input())

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

from collections import deque
q = deque()
q.append(0)    
seen = [-1]*n
seen[0] = 0  #Node0 White    

while q:
    node = q.pop()
    for pair,length in graph[node]:
        if seen[pair]==-1:
            if length%2 == 0:
                seen[pair] = 0
            else:
                seen[pair] = 1
            q.append(pair)
    
for i in range(n):
    print(seen[i])

Submission status: Wrong Answer

Solution code:
n = int(input())

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

from collections import deque
q = deque()
q.append(0)    
seen = [-1]*n
seen[0] = 0  #Node0 White    

while q:
    node = q.pop()
    for pair,length in graph[node]:
        if seen[pair]==-1:
            if length%2 == 0:
                seen[pair] = 0
            else:
                seen[pair] = 1
            q.append(pair)
    
for i in range(n):
    print(seen[i])

Submission status: Wrong Answer

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


def solve(N: int, u: "List[int]", v: "List[int]", w: "List[int]"):
    same = {}
    vary = {}
    for i, j, k in zip(u, v, w):
        if k % 2 == 0:
            same.setdefault(i, [])
            same[i].append(j)
            same.setdefault(j, [])
            same[j].append(i)
        else:
            vary.setdefault(i, [])
            vary[i].append(j)
            vary.setdefault(j, [])
            vary[j].append(i)
    color = [0 for i in range(N + 1)]
    # print(same, vary)
    for i in range(1, N):
        for j in same.get(i, []):
            color[j] = color[i]
        for j in vary.get(i, []):
            color[j] = not color[i]
        # print(color)
    for c in color[1:]:
        print(int(c))
    return


# Generated by 1.1.5 https://github.com/kyuridenamida/atcoder-tools
# (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    u = [int()] * (N - 1)  # type: "List[int]"
    v = [int()] * (N - 1)  # type: "List[int]"
    w = [int()] * (N - 1)  # type: "List[int]"
    for i in range(N - 1):
        u[i] = int(next(tokens))
        v[i] = int(next(tokens))
        w[i] = int(next(tokens))
    solve(N, u, v, w)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
import collections

def breadth_first_search(graph, root): 
    visited, queue = set(), collections.deque([root])
    color = [0] * len(graph)
    # root is always 0
    while queue: 
        vertex = queue.popleft()
        for neighbour in graph[vertex]: 
            if neighbour[0] not in visited: 
                visited.add(neighbour[0]) 
                queue.append(neighbour[0])
                color[neighbour[0]] = neighbour[1] % 2
    return color 

def solve(N: int, u: "List[int]", v: "List[int]", w: "List[int]"):
    graph = [[] for i in range(N)]
    for i in range(len(u)):
        graph[u[i]-1] += [[v[i]-1, w[i]]]
        graph[v[i]-1] += [[u[i]-1, w[i]]]
    color = breadth_first_search(graph, 0)
    print("\n".join(list(map(str, color))))
    return

def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    u = [int()] * (N - 1)  # type: "List[int]"
    v = [int()] * (N - 1)  # type: "List[int]"
    w = [int()] * (N - 1)  # type: "List[int]"
    for i in range(N - 1):
        u[i] = int(next(tokens))
        v[i] = int(next(tokens))
        w[i] = int(next(tokens))
    solve(N, u, v, w)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N=int(input())
tree_dict={i:-1 for i in range(N)}
tree_map=[[] for i in range(N)]
start=set([i for i in range(N)])

edge=[]

#数字の若いほうから見ていくとする
for i in range(N-1):
    u,v,w = map(int, input().split())
    tree_map[u-1].append([v-1,w])
    tree_map[v-1].append([u-1,w])

def fill(index, now_color, dist):
    for v,w in tree_map[index]:
        
        if tree_dict[v] == 0:
            if w%2==0:
                tree_dict[v] = now_color
                fill(v, tree_dict[v], w)
            else:
                tree_dict[v] = -1*now_color
                fill(v, tree_dict[v],w)


now_color=1
tree_dict[0]=now_color
fill(0,now_color, 0)

for i in tree_dict.values():
    if i==-1:
        print(0)
    else:
        print(1)

Submission status: Wrong Answer

Solution code:
N=int(input())
tree_dict={i:-1 for i in range(N)}
tree_map=[[] for i in range(N)]
start=set([i for i in range(N)])

edge=[]

#数字の若いほうから見ていくとする
for i in range(N-1):
    u,v,w = map(int, input().split())
    tree_map[u-1].append([v-1,w])

def fill(index, dist):
    for v,w in tree_map[index]:
        
        if tree_dict[v] == -1:
#             if w%2==0:
#                 tree_dict[v] = now_color
#                 fill(v, tree_dict[v], w)
#             else:
#                 tree_dict[v] = -1*now_color
#                 fill(v, tree_dict[v],w)
            tree_dict[v] = (dist+w)%2
            fill(v, w)


fill(0, 0)

for i in tree_dict.values():
    if i==-1:
        print(0)
    else:
        print(1)

Submission status: Wrong Answer

Solution code:
N=int(input())
tree_dict={i:-1 for i in range(N)}
tree_map=[[] for i in range(N)]
start=set([i for i in range(N)])

edge=[]

#数字の若いほうから見ていくとする
for i in range(N-1):
    u,v,w = map(int, input().split())
    tree_map[u-1].append([v-1,w])

def fill(index, now_color, dist):
    for v,w in tree_map[index]:
        
        if tree_dict[v] == -1:
#             if w%2==0:
#                 tree_dict[v] = now_color
#                 fill(v, tree_dict[v], w)
#             else:
#                 tree_dict[v] = -1*now_color
#                 fill(v, tree_dict[v],w)
            tree_dict[v] = (dist+w)%2
            fill(v, tree_dict[v],w)

now_color=1
tree_dict[0]=now_color
fill(0,now_color, 0)

for i in tree_dict.values():
    if i==-1:
        print(0)
    else:
        print(1)

Submission status: Wrong Answer

Solution code:
N=int(input())
tree_dict={i:0 for i in range(N)}
tree_map=[[] for i in range(N)]
start=set([i for i in range(N)])

edge=[]

#数字の若いほうから見ていくとする
for i in range(N-1):
    u,v,w = map(int, input().split())
    edge.append(v-1)
    tree_map[u-1].append([v-1,w])

start = (start - set(edge)).pop()

def fill(index, now_color, dist):
    for v,w in tree_map[index]:
        
        if tree_dict[v] == 0:
#             if w%2==0:
#                 tree_dict[v] = now_color
#                 fill(v, tree_dict[v], w)
#             else:
#                 tree_dict[v] = -1*now_color
#                 fill(v, tree_dict[v],w)
            tree_dict[v] = (dist+w)%2
            fill(v, tree_dict[v],w)

now_color=1
tree_dict[start]=now_color
fill(start,now_color, 0)

for i in tree_dict.values():
    if i==-1:
        print(0)
    else:
        print(1)

Submission status: Wrong Answer

Solution code:
N=int(input())
tree_dict={i:0 for i in range(N)}
tree_map=[[] for i in range(N)]
start=set([i for i in range(N)])

edge=[]

#数字の若いほうから見ていくとする
for i in range(N-1):
    u,v,w = map(int, input().split())
    edge.append(v-1)
    tree_map[u-1].append([v-1,w])

# start = 0
start = (start - set(edge)).pop()

def fill(index, now_color, dist):
    for v,w in tree_map[index]:
        
        if tree_dict[v] == 0:
            if w%2==0:
                tree_dict[v] = now_color
    #             print(v, tree_dict[v], old,index)

                fill(v, tree_dict[v], w)

            else:

                tree_dict[v] = -1*now_color
    #             print(v, tree_dict[v], old,index)

                fill(v, tree_dict[v],w)


now_color=1
tree_dict[start]=now_color
fill(start,now_color, 0)

for i in tree_dict.values():
    if i==-1:
        print(0)
    else:
        print(1)

Submission status: Wrong Answer

Solution code:
N=int(input())
tree_dict={i:0 for i in range(N)}
tree_map=[[] for i in range(N)]
start=set([i for i in range(N)])

edge=[]

#数字の若いほうから見ていくとする
for i in range(N-1):
    u,v,w = map(int, input().split())
    edge.append(v-1)
    tree_map[u-1].append([v-1,w])

# start = 0
start = (start - set(edge)).pop()

def fill(index, now_color, dist):
    for v,w in tree_map[index]:
        
        if tree_dict[v] != 0:
            if w%2==0:
                tree_dict[v] = now_color
    #             print(v, tree_dict[v], old,index)

                fill(v, tree_dict[v], w)

            else:

                tree_dict[v] = -1*now_color
    #             print(v, tree_dict[v], old,index)

                fill(v, tree_dict[v],w)


now_color=1
tree_dict[start]=now_color
fill(start,now_color, 0)

for i in tree_dict.values():
    if i==-1:
        print(0)
    else:
        print(1)

Submission status: Wrong Answer

Solution code:
N=int(input())
tree_dict={i:0 for i in range(N)}
tree_map=[[] for i in range(N)]
start=set([i for i in range(N)])

edge=[]

#数字の若いほうから見ていくとする
for i in range(N-1):
    u,v,w = map(int, input().split())
    edge.append(v-1)
    tree_map[u-1].append([v-1,w])

# start = 0
start = (start - set(edge)).pop()

def fill(index, now_color, dist):
    for v,w in tree_map[index]:
        
        if tree_dict[v] != -1:
            if w%2==0:
                tree_dict[v] = now_color
    #             print(v, tree_dict[v], old,index)

                fill(v, tree_dict[v], w)

            else:

                tree_dict[v] = -1*now_color
    #             print(v, tree_dict[v], old,index)

                fill(v, tree_dict[v],w)


now_color=1
tree_dict[start]=now_color
fill(start,now_color, 0)

for i in tree_dict.values():
    if i==-1:
        print(0)
    else:
        print(1)

Submission status: Wrong Answer

Solution code:
N=int(input())
tree_dict={i:0 for i in range(N)}
tree_map=[[] for i in range(N)]
start=set([i for i in range(N)])

edge=[]

#数字の若いほうから見ていくとする
for i in range(N-1):
    u,v,w = map(int, input().split())
    edge.append(v-1)
    tree_map[u-1].append([v-1,w])

# start = 0
start = (start - set(edge)).pop()

def fill(index, now_color, dist):
    for v,w in tree_map[index]:
        
        if (dist+w)%2==0:
            tree_dict[v] = now_color
#             print(v, tree_dict[v], old,index)

            fill(v, tree_dict[v], w)

        else:

            tree_dict[v] = -1*now_color
#             print(v, tree_dict[v], old,index)

            fill(v, tree_dict[v],w)


now_color=1
tree_dict[start]=now_color
fill(start,now_color, 0)

for i in tree_dict.values():
    if i==-1:
        print(0)
    else:
        print(1)

Submission status: Wrong Answer

Solution code:
N=int(input())
tree_dict={i:0 for i in range(N)}
tree_map=[[] for i in range(N)]
start=set([i for i in range(N)])

edge=[]

def fill(index, now_color, old):
    for v,w in tree_map[index]:
        
        if w%2==0:
            tree_dict[v] = now_color
#             print(v, tree_dict[v], old,index)

            fill(v, tree_dict[v], index)

        else:

            tree_dict[v] = -1*now_color
#             print(v, tree_dict[v], old,index)

            fill(v, tree_dict[v],index)

now_color=1
tree_dict[0]=now_color
fill(0,now_color, -1)

for i in tree_dict.values():
    if i==-1:
        print(0)
    else:
        print(1)

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(2000000)

N=int(input())
tree_dict={i:0 for i in range(N)}
tree_map=[[] for i in range(N)]
start=set([i for i in range(N)])

edge=[]

#数字の若いほうから見ていくとする
for i in range(N-1):
    u,v,w = map(int, input().split())
    edge.append(v-1)
    tree_map[u-1].append([v-1,w])

# start = 0
start = (start - set(edge)).pop()

tree_dict={i:0 for i in range(N)}

def fill(index, now_color, old):
    for v,w in tree_map[index]:
        
        if w%2==0:
            tree_dict[v] = now_color
            fill(v, tree_dict[v], index)

        else:

            tree_dict[v] = -1*now_color
            fill(v, tree_dict[v],index)


tree_map

now_color=1
tree_dict[start]=now_color
fill(start,now_color, -1)

for i in tree_dict.values():
    if i==-1:
        print(0)
    else:
        print(1)

Submission status: Wrong Answer

Solution code:
N=int(input())
tree_dict={i:0 for i in range(N)}
tree_map=[[] for i in range(N)]
start=set([i for i in range(N)])

edge=[]

#数字の若いほうから見ていくとする
for i in range(N-1):
    u,v,w = map(int, input().split())
    edge.append(v-1)
    tree_map[u-1].append([v-1,w])

# start = 0
start = (start - set(edge)).pop()

tree_dict={i:0 for i in range(N)}

def fill(index, now_color, old):
    for v,w in tree_map[index]:
        
        if w%2==0:
            tree_dict[v] = now_color
#             print(v, tree_dict[v], old,index)

            fill(v, tree_dict[v], index)

        else:

            tree_dict[v] = -1*now_color
#             print(v, tree_dict[v], old,index)

            fill(v, tree_dict[v],index)


tree_map

now_color=1
tree_dict[start]=now_color
fill(start,now_color, -1)

for i in tree_dict.values():
    if i==-1:
        print(0)
    else:
        print(1)

Submission status: Wrong Answer

Solution code:
N=int(input())
tree_dict={i:0 for i in range(N)}
tree_map=[[] for i in range(N)]
start=set([i for i in range(N)])

edge=[]

#数字の若いほうから見ていくとする
for i in range(N-1):
    u,v,w = map(int, input().split())
    edge.append(v-1)
    tree_map[u-1].append([v-1,w])

# start = 0
start = (start - set(edge)).pop()

tree_dict={i:0 for i in range(N)}

def fill(index, now_color, old):
    for v,w in tree_map[index]:
        
        if w%2==0:
            tree_dict[v] = now_color
            fill(v, tree_dict[v], index)

        else:

            tree_dict[v] = -now_color
            fill(v, tree_dict[v],index)




now_color=1
tree_dict[start]=now_color
fill(start,now_color, -1)

for i in tree_dict.values():
    if i==-1:
        print(0)
    else:
        print(1)