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: Runtime Error

Solution code:
N = int(input())

class Tree():
    def __init__(self):
        self.connections = []

    def set_connection(self, root, distance):
        self.connections.append((root, distance))

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

COLOR_WHITE = 0
COLOR_BLACK = 1
COLOR_NONE = 2

colors = [COLOR_NONE for _ in range(N)]

def set_color(node, distance):
    for root, d in node.connections:
        if colors[root] == COLOR_NONE:
            new_distance = distance + d
            colors[root] = COLOR_WHITE if new_distance % 2 == 0 else COLOR_BLACK
            set_color(trees[root], new_distance)

colors[0] = COLOR_WHITE
set_color(trees[0], 0)
for c in colors:
    print(c)

Submission status: Runtime Error

Solution code:
N=int(input())
G=[[] for i in range(N)]
res=[0]*N
def dfs(v,p,c):
  res[v]=c
  for nv,w in G[v]:
    if nv==p:
      continue
    if (w & 1):
      dfs(nv,v,1-c)
    else:
      dfs(nv,v,c)

for i in range(N-1):
  u,v,w=map(int,input().split())
  u-=1
  v-=1
  G[u].append((v,w))
  G[v].append((u,w))
dfs(0,-1,1)
for i in range(N):
  print(res[i])

Submission status: Runtime Error

Solution code:
N=int(input())
if N==1:
  print(33)
  quit()
G=[[] for i in range(N)]
res=[0]*N
def dfs(v,p,c):
  res[v]=c
  for nv,w in G[v]:
    if nv==p:
      continue
    if (w & 1):
      dfs(nv,v,1-c)
    else:
      dfs(nv,v,c)

for i in range(N-1):
  u,v,w=map(int,input().split())
  u-=1
  v-=1
  G[u].append((v,w))
  G[v].append((u,w))
dfs(0,-1,0)
for i in range(N):
  print(res[i])

Submission status: Runtime Error

Solution code:
N=int(input())
G=[[] for i in range(N)]
res=[0]*N
def dfs(v,p,c):
  res[v]=c
  for nv,w in G[v]:
    if nv==p:
      continue
    if (w & 1):
      dfs(nv,v,1-c)
    else:
      dfs(nv,v,c)

for i in range(N-1):
  u,v,w=map(int,input().split())
  u-=1
  v-=1
  G[u].append((v,w))
  G[v].append((u,w))
dfs(0,-1,0)
for i in range(N):
  print(res[i])

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N=int(input());p=[[]for _ in range(N)]
for _ in range(N-1):u,v,w=map(int,input().split());p[u].append((v,w));p[v].append((u,w))
s=[(1,0)];l=[-1]*N
while s:
 a,w=s.pop()
 l[a-1]=w%2
 for b,c in p[a]:
  if l[b-1]==-1:s.append((b,w+c))
for i in l:print(i)

Submission status: Runtime Error

Solution code:
from collections import deque

N = int(input())
        
G = [[] for i in range(N)]
for _ in range(N - 1):
    a, b, c = map(int, input().split())
    G[a - 1].append([b - 1, c])
    G[b - 1].append([a - 1, c])
    
color = [-1] * N
color[0] = 0
Q = deque()
Q = [0]

while len(Q) != 0:
    cur = Q.popleft()
    for to, dist in G[cur]:
        if color[to] == -1:
            color[to] = (color[cur] + dist) % 2
            Q.append(to)
            
for i in range(N):
    print(color[i])

Submission status: Runtime Error

Solution code:
'''input
3
1 2 2
2 3 1
'''

N = int(input())

edges = [[] for i in range(N)]

for i in range(N-1):
	a, b, w = [int(x) for x in input().split()]
	a -= 1
	b -= 1
	edges[a].append((b, w))
	edges[b].append((a, w))


color = [None]*N

color[0] = 0

def dfs(curr, prev):
	for i in edges[curr]:
		if i[0] == prev: continue

		if i[1] % 2 == 0: color[i[0]] = color[curr]
		else: color[i[0]] = 1 - color[curr]

		dfs(i[0], curr)

dfs(0, -1)

for i in color:
	print(i)

Submission status: Runtime Error

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

def dfs(x):
    for y in t[x]:
        del t[y][x]
        ans[y] = ans[x] if t[x][y] % 2 == 0 else 1-ans[x]
        if len(t[y]) > 0:
            dfs(y)

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

Submission status: Runtime Error

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

def dfs(x):
    for y in t[x]:
        del t[y][x]
        ans[y] = ans[x] if t[x][y] % 2 == 0 else 1-ans[x]
        dfs(y)

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

Submission status: Runtime Error

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

def dfs(x):
    for y, w in t[x].items():
        del t[y][x]
        ans[y] = ans[x] if w % 2 == 0 else 1-ans[x]
        dfs(y)

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

Submission status: Runtime Error

Solution code:
'''input
1

'''

N = int(input())

edges = [[] for i in range(N)]

for i in range(N-1):
	a, b, w = [int(x) for x in input().split()]
	a -= 1
	b -= 1
	edges[a].append((b, w))
	edges[b].append((a, w))


color = [None]*N

color[0] = 0

def dfs(curr, prev):
	for i in edges[curr]:
		if i[0] == prev: continue

		color[i[0]] = color[curr] if i[1] % 2 == 0 else 1 - color[curr]
		dfs(i[0], curr)

dfs(0, -1)

for i in color:
	print(i)

Submission status: Runtime Error

Solution code:
n = int(input())
t = {i:[] for i in range(n)}
ans = [-1]*n
for _ in range(n-1):
    u, v, w = map(int, input().split())
    t[u-1].append((v-1, w))
    t[v-1].append((u-1, w))

def dfs(x):
    for y, w in t[x]:
        if ans[y] == -1:
            ans[y] = ans[x] if w % 2 == 0 else 1-ans[x]
            dfs(y)

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

Submission status: Runtime Error

Solution code:
'''input
5
2 5 2
2 3 10
1 3 8
3 4 2
'''

N = int(input())

edges = [[] for i in range(N)]

for i in range(N-1):
	a, b, w = [int(x) for x in input().split()]
	a -= 1
	b -= 1
	edges[a].append((b, w))
	edges[b].append((a, w))


color = [None]*N

color[0] = 0

def dfs(curr, prev):
	
	for i in edges[curr]:
		if i[0] == prev: continue

		color[i[0]] = color[curr] if i[1] % 2 == 0 else 1 - color[curr]
		dfs(i[0], curr)

dfs(0, -1)

for i in color:
	print(i)

Submission status: Runtime Error

Solution code:
N: int = int(input())
graph = [[] for _ in range(N)]
for _ in range(N - 1):
    u, v, w = map(int, input().split())
    u -= 1
    v -= 1
    graph[u].append((v, w))
    graph[v].append((u, w))
qu = [0]
colors = [0 for _ in range(N)]
visited = set()
while len(qu) != 0:
    cur = qu.pop()
    visited.add(cur)
    for nxt, weight in graph[cur]:
        if nxt not in visited:
            colors[nxt] = (colors[cur] + weight)%2
            qu.append(nxt)
for v in colors:print(v)

Submission status: Runtime Error

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

# abc126_d

from collections import namedtuple

DistFrom = namedtuple('DistFrom', ['f', 'dist'])

def main():
    # Input
    n, = [int(s) for s in input().split()]
    fs = [[int(s) for s in input().split()] for _ in range(n-1)]
    # Get ans
    gs = [None] * n
    for i in range(n):
        gs[i] = list()
    for x, y, d in fs:
        x -= 1
        y -= 1
        d = d%2
        gs[x].append(DistFrom(y, d))
        gs[y].append(DistFrom(x, d))
    distlist = [None] * n
    def dfs(cur, dist):
        if distlist[cur] != None:
            return
        distlist[cur] = dist
        for i, d in gs[cur]:
            dfs(i, dist^d)
    dfs(0, 0)
    # Output
    for i in range(n):
        print(distlist[i])

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:


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

costs = [-1 for _ in range(n)]

def dfs(cur, dist):
    if costs[cur] != -1:
        return
    costs[cur] = dist
    for v, d in p[cur]:
        dfs(v, dist^d)
dfs(0, 0)

for c in costs:
    print(c)

Submission status: Runtime Error

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

# abc126_d

from collections import namedtuple

DistFrom = namedtuple('DistFrom', ['f', 'dist'])

def main():
    # Input
    n, = [int(s) for s in input().split()]
    #if n==1: return
    fs = [[int(s) for s in input().split()] for _ in range(n-1)]
    # Get ans
    gs = [None] * n
    for i in range(n):
        gs[i] = list()
    for x, y, d in fs:
        x -= 1
        y -= 1
        d = d%2
        gs[x].append(DistFrom(y, d))
        gs[y].append(DistFrom(x, d))
    distlist = [None] * n
    def dfs(cur, dist):
        if distlist[cur] != None:
            return
        distlist[cur] = dist
        for i, d in gs[cur]:
            dfs(i, dist^d)
    dfs(0, 0)
    ans = distlist
    # Output
    for a in ans:
        print(a)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
from collections import defaultdict

input = sys.stdin.readline

N = int(input())
G = defaultdict(list)
for _ in range(N - 1):
    u, v, w = map(int, input().split())
    G[u - 1].append((v - 1, w))
    G[v - 1].append((u - 1, w))
res = [None] * N


def dfs(v, c):
    res[v] = c
    for u, w in G[v]:
        if res[u] is not None:
            continue
        if w % 2 == 0:
            dfs(u, c)
        else:
            dfs(u, 1 - c)


dfs(0, 0)
for x in res:
    print(x)

Submission status: Runtime Error

Solution code:
import sys
from collections import defaultdict

input = sys.stdin.readline

N = int(input())
G = defaultdict(list)
for _ in range(N - 1):
    u, v, w = map(int, input().split())
    G[u - 1].append((v - 1, w))
    G[v - 1].append((u - 1, w))
res = [None] * N


def dfs(v, p, c):
    if res[v] is not None:
        return
    res[v] = c
    for u, w in G[v]:
        if u == p:
            continue
        if w % 2 == 0:
            dfs(u, v, c)
        else:
            dfs(u, v, 1 - c)


dfs(0, -1, 0)
for x in res:
    print(x)

Submission status: Runtime Error

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

# abc126_d

from collections import namedtuple

DistFrom = namedtuple('DistFrom', ['f', 'dist'])

def main():
    # input
    n, = [int(s) for s in input().split()]
    #if n==1: return
    fs = [[int(s) for s in input().split()] for _ in range(n-1)]
    # getans
    gs = [None] * n
    for i in range(n):
        gs[i] = list()
    for x, y, d in fs:
        x -= 1
        y -= 1
        d = d%2
        gs[x].append(DistFrom(y, d))
        gs[y].append(DistFrom(x, d))
    distlist = [None] * n
    def search(cur, prev, dist):
        #distlist[cur] != None: return
        distlist[cur] = dist
        for i, d in gs[cur]:
            if i != prev:
                search(i, cur, dist^d)
    distlist[0] = 0
    for i, d in gs[0]:
        search(i, 0, d)
    ans = distlist
    # output
    for a in ans:
        print(a)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

# abc126_d

from collections import namedtuple

DistFrom = namedtuple('DistFrom', ['f', 'dist'])

def main():
    # input
    n, = [int(s) for s in input().split()]
    #if n==1: return
    fs = [[int(s) for s in input().split()] for _ in range(n-1)]
    # getans
    gs = [None] * n
    for i in range(n):
        gs[i] = list()
    for x, y, d in fs:
        x -= 1
        y -= 1
        gs[x].append(DistFrom(y, d))
        gs[y].append(DistFrom(x, d))
    distlist = [None] * n
    def search(cur, prev, dist):
        #distlist[cur] != None: return
        distlist[cur] = dist
        for i, d in gs[cur]:
            if i != prev:
                search(i, cur, dist+d)
    distlist[0] = 0
    for i, d in gs[0]:
        search(i, 0, d)
    ans = [d%2 for d in distlist]
    # output
    for a in ans:
        print(a)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys

sys.setrecursionlimit(2000)

n = int(input())

p = [[] for i in range(n)]

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

c = [-1 for i in range(n)]

def dfs(num, zo):
    if c[num] != -1:
        return
    else:
        c[num] = zo
    for v, w in p[num]:
        if w % 2 == 0:
            dfs(v, zo)
        else:
            dfs(v, not zo)
dfs(0, 0)

for i in range(n):
    print(1 if c[i] else 0)

Submission status: Runtime Error

Solution code:
n = int(input())

p = [[] for i in range(n)]

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

c = [-1 for i in range(n)]

def dfs(num, zo):
    if c[num] != -1:
        return
    else:
        c[num] = zo
    for v, w in p[num]:
        if w % 2 == 0:
            dfs(v, zo)
        else:
            dfs(v, -1*(zo-1))
dfs(0, 0)

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

Submission status: Runtime Error

Solution code:
class G:
    def __init__(self, i):
        self.graph = [i]
        self.id = i
        self.gid = -1


def concat(u, v):
    g1 = data[u].graph
    g2 = data[v].graph
    g = g1 + g2
    all_g.append(g)
    id = min(data[u].id, data[v].id)
    
    for elm in g:
        data[elm].graph = g
        data[elm].id = id

def make_revid(id):
    revid = id + 1 if id %2 == 0 else id - 1
    return revid


def sep_g(u, v):
    global gid_
    global all_gid

    gid0 = data[u].gid
    gid1 = data[v].gid
    cond1 = data[u].gid >= 0
    cond2 = data[v].gid >= 0

    if cond1 or cond2:
        if cond1 and cond2:
            if gid0 <= gid1:
                old_gid = data[v].gid
                new_gid = make_revid(gid0)
                data[v].gid = new_gid
            else:
                old_gid = data[u].gid
                new_gid = make_revid(gid1)
                data[u].gid = new_gid

        elif cond1 and not cond2:
            old_gid = data[v].gid
            new_gid = make_revid(gid0)
            data[v].gid = new_gid
        elif not cond1 and not cond2:
            old_gid = data[u].gid
            new_gid =  make_revid(gid1)
            data[u].gid = new_gid

        all_gid.remove(old_gid)
        all_gid.append(new_gid)

    else:
        data[u].gid = gid_
        data[v].gid = gid_ + 1
        all_gid += [gid_, gid_+1]
        gid_ += 2


gid_ = 0
N = int(input())

odd_pairs = []
all_g = []
all_gid = []

data = {}

for i in range(N):
    g = G(i)
    data[i] = g
    all_g.append(g)

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

    if w %2 == 1:
        odd_pairs.append([u, v, w])
        continue
    
    if v not in data[u].graph:
        concat(u, v)

for u, v, w in odd_pairs:
    sep_g(u, v)


for i in range(N):
    gid = data[i].gid
    if gid < 0:
        out = 0
    else:
        out = gid % 2
    print(out)

Submission status: Runtime Error

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

# abc126_d

from collections import namedtuple

DistFrom = namedtuple('DistFrom', ['f', 'dist'])

def main():
    # input
    n, = [int(s) for s in input().split()]
    fs = [[int(s) for s in input().split()] for _ in range(n-1)]
    # getans
    getters = [None] * n
    for i in range(n):
        getters[i] = list()
    for x, y, d in fs:
        x -= 1
        y -= 1
        getters[x].append(DistFrom(y, d))
        getters[y].append(DistFrom(x, d))
    distlist = [None] * n
    def search(cur_i, prev, dist):
        if distlist[cur_i] != None: return  # TODO delete
        distlist[cur_i] = dist
        for i, d in getters[cur_i]:
            if i != prev:
                search(i, cur_i, dist+d)
    distlist[0] = 0
    for i, d in getters[0]:
        search(i, 0, d)
    ans = [d%2 for d in distlist]
    # output
    for a in ans:
        print(a)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
from collections import defaultdict

input = sys.stdin.readline

N = int(input())
G = defaultdict(list)

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

res = [None] * N


def dfs(v, p, c):
    res[v] = c
    for u, w in G[v]:
        if u == p:
            continue
        if w % 2 == 0:
            dfs(u, v, c)
        else:
            dfs(u, v, 1 - c)


dfs(0, -1, 0)
for x in res:
    print(x)

Submission status: Runtime Error

Solution code:
from sys import exit

N = int(input())

q = {}

for i in range(N):
    q[i + 1] = []

for i in range(N - 1):
    _u, _v, _w = map(int, input().split())

    q[_u].append((_v, _w))
    q[_v].append((_u, _w))

reached = []
distances = {}


def search(x, distance):
    if x in reached:
        # 既に達しているところだったら
        return

    # 達したことを記録
    reached.append(x)
    distances[x] = distance

    for s in q[x]:
        search(s[0], distance + s[1])


search(1, 0)
print(distances)

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

Submission status: Runtime Error

Solution code:
from sys import stdin
import math
import fractions
from collections import deque

WHITE = 0
BLACK = 1

N = int(stdin.readline().rstrip())
data = [list(map(int, stdin.readline().rstrip().split())) for _ in range(N-1)]

# 根から頂点までの距離
# 根は適当な頂点を選べば良いので、u_0とする
dist = [None] * (N+1)
dist[data[0][0]] = 0

# 根からの距離を算出
for u, v, w in data:
    # 木構造なので、どちらか一方はすでに求まっているはず
    if dist[u] != None:
        dist[v] = dist[u] + w
    else:
        dist[u] = dist[v] + w

# distが偶数の頂点には白で、奇数の頂点には黒で塗ることとする
for i in range(1, N+1):
    if dist[i] % 2 == 0:
        print(WHITE)
    else:
        print(BLACK)

Submission status: Runtime Error

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

# abc126_d

from collections import namedtuple

DistFrom = namedtuple('DistFrom', ['f', 'dist'])

def main():
    # input
    n, = [int(s) for s in input().split()]
    fs = [[int(s) for s in input().split()] for _ in range(n-1)]
    # getans
    getters = [None] * n
    for i in range(n):
        getters[i] = list()
    for x, y, d in fs:
        x -= 1
        y -= 1
        getters[x].append((y, d))
        getters[y].append((x, d))
    distlist = [None] * n
    def search(cur_i, prev, dist):
        distlist[cur_i] = dist
        for i, d in getters[cur_i]:
            if i != prev:
                search(i, cur_i, dist+d)
    distlist[0] = 0
    for i, d in getters[0]:
        search(i, 0, d)
    ans = [d%2 for d in distlist]
    # output
    for a in ans:
        print(a)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

# abc126_d

from collections import namedtuple

DistFrom = namedtuple('DistFrom', ['f', 'dist'])

def main():
    # input
    n, = [int(s) for s in input().split()]
    fs = [[int(s) for s in input().split()] for _ in range(n-1)]
    # getans
    getters = [None] * n
    for i in range(n):
        getters[i] = list()
    for x, y, d in fs:
        x -= 1
        y -= 1
        getters[x].append(DistFrom(y, d))
        getters[y].append(DistFrom(x, d))
    dists = [-1] * n
    def search(cur_i, prev, dist):
        dists[cur_i] = dist
        for i, d in getters[cur_i]:
            if i == prev: continue
            search(i, cur_i, dist+d)
    dists[0] = 0
    for i, d in getters[0]:
        search(i, 0, d)
    ans = [d%2 for d in dists]
    # output
    for a in ans:
        print(a)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import copy
tree_list = []
tree_dict = {}
N = int(input())
for n in range(N-1):
  tree_list.append(input())
u,v,w = tree_list[0].split(" ")
if int(w) % 2 == 0:
  tree_dict[u] = 0
  tree_dict[v] = 0
else:
  tree_dict[u] = 0
  tree_dict[v] = 1
for n in range(1,N-1):
  u,v,w = tree_list[n].split(" ")
  if int(w) % 2 == 0:
    if u not in tree_dict.keys():
      tree_dict[u] = copy.deepcopy(tree_dict[v])
    else:
      tree_dict[v] = copy.deepcopy(tree_dict[u])
  else:
    if u not in tree_dict.keys():
      tree_dict[u] = 1-tree_dict[v]
    else:
      tree_dict[v] = 1-tree_dict[u]
for i in list(tree_dict.values()):
  print(i)

Submission status: Runtime Error

Solution code:
N = int(input())

tree_dic = {i+1:[] for i in range(N)}
bw_list = [0]*N

for i in range(1, N):
    ui, vi, wi = map(int, input().split())
    tree_dic[ui].append((vi, wi))
    tree_dic[vi].append((ui, wi))

def decide_color(distance_dic, current_point, tree_dic, passed_place_lst):
    for next_point, add_distance in tree_dic[current_point]:
        passed_place_lst[next_point-1] += 1
        if next_point not in distance_dic:
            distance_dic[next_point] = add_distance + distance_dic[current_point]
            decide_color(distance_dic, next_point, tree_dic, passed_place_lst)
            
        elif add_distance + distance_dic[current_point] < distance_dic[next_point]:
            distance_dic[next_point] = add_distance + distance_dic[current_point]
            decide_color(distance_dic, next_point, tree_dic, passed_place_lst)

passed_place_lst = [0]*N
passed_place_lst[0] = 1
distance_dic = {1:0}
current_point = 1
while 0 in passed_place_lst:
    decide_color(distance_dic, current_point, tree_dic, passed_place_lst)
    for place in range(N):
        if passed_place_lst[place] != 0 and (place + 1) in tree_dic:
            del tree_dic[place+1]
    if len(tree_dic) != 0:
        current_point = list(tree_dic.keys())[0]

            


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

Submission status: Runtime Error

Solution code:
tree_list = []
tree_dict = {}
N = int(input())
for n in range(N-1):
  tree_list.append(input())
u,v,w = tree_list[0].split(" ")
if int(w) % 2 == 0:
  tree_dict[u] = 0
  tree_dict[v] = 0
else:
  tree_dict[u] = 0
  tree_dict[v] = 1
for n in range(1,N):
  u,v,w = tree_list[n].split(" ")
  if int(w) % 2 == 0:
    if u not in tree_dict.keys():
      tree_dict[u] = tree_dict[v]
    else:
      tree_dict[v] = tree_dict[u]
for i in list(tree_dict.value()):
  print(i)

Submission status: Runtime Error

Solution code:
n=int(input())
d=[-1]*n
l=deque([])
for i in range(n-1):
    u,v,w=map(int,input().split())
    u,v=min(u,v),max(u,v)
    l.append((u-1,v-1,w))
f=0
while len(l):
    x=l.popleft()
    if f:
        if x[2]%2 and d[x[0]]==0:
            d[x[1]]=1
        elif x[2]%2 and d[x[0]]==1:
            d[x[1]]=0
        elif x[2]%2==0 and d[x[0]]==1:
            d[x[1]]=1
        elif x[2]%2==0 and d[x[0]]==0:
            d[x[1]]=0
        elif x[2]%2 and d[x[1]]==0:
            d[x[0]]=1
        elif x[2]%2 and d[x[1]]==1:
            d[x[0]]=0
        elif x[2]%2==0 and d[x[1]]==1:
            d[x[0]]=1
        elif x[2]%2==0 and d[x[1]]==0:
            d[x[0]]=0
        else:
            l.append(x)
    else:
        f=1
        if x[2]%2:
            d[x[0]]=0
            d[x[1]]=1
        else:
            d[x[0]]=0
            d[x[1]]=0
for i in d:
    print(i)

Submission status: Runtime Error

Solution code:
import sys

sys.setrecursionlimit(10000)
N = int(input())
W = []
for _ in range(N - 1):
    u, v, w = map(int, input().split())
    W.append([u - 1, v - 1, w])
color = [0] * N
tree = [[] for _ in range(N)]
for w in W:
    tree[w[0]].append([w[1], w[2]])
    tree[w[1]].append([w[0], w[2]])
for i, t in enumerate(tree):
    if len(t) == 1:
        start = i
        break
 
 
def rec(n, prev, w):
    if color[prev] == color[n]:
        if w % 2 == 1:
            color[n] = 1 if color[prev] == 0 else 0
    elif w % 2 == 0:
        color[n] = color[prev]
    for i in tree[n]:
        if i[0] == prev:
            continue
        else:
            rec(i[0], n, i[1])
 
 
rec(tree[start][0][0], start, tree[start][0][1])
for c in color:
    print(c)

Submission status: Runtime Error

Solution code:
noded = {}
edad = {}

class Node:
    def __init__(self, index):
        self.index = index
        self.color = -1
        self.eda_list = []

class Eda:
    def __init__(self, index, w, left, right):
        self.index = index
        self.w = w
        self.left = left
        self.right = right

def color_oppo_of(node):
    c = node.color
    for eda in [edad[edai] for edai in node.eda_list]:
        if eda.right == node.index:
            oppo = eda.left
        else:
            oppo = eda.right

        if noded[oppo].color == -1:
            if eda.w % 2 == 0:
                noded[oppo].color = node.color
            else:
                noded[oppo].color = (node.color + 1) % 2
            color_oppo_of(noded[oppo])

def solve():
    n = int(input())
    for i in range(n-1):
        U, V, W = map(int, input().split())
        u = U
        v = V
        w = W

        eda = Eda(i, w, u, v)
        edad[i] = eda

        if u not in noded.keys():
            noded[u] = Node(u)
        noded[u].eda_list.append(i)

        if v not in noded.keys():
            noded[v] = Node(v)
        noded[v].eda_list.append(i)

    noded[1].color = 0
    color_oppo_of(noded[1])

    for i in range(1, n+1):
        print(noded[i].color)
        #print(vars(noded[i]))



solve()

Submission status: Runtime Error

Solution code:
noded = {}
edad = {}

class Node:
    def __init__(self, index):
        self.index = index
        self.color = -1
        self.eda_list = []

class Eda:
    def __init__(self, index, w, left, right):
        self.index = index
        self.w = w
        self.left = left
        self.right = right

def color_oppo_of(node):
    c = node.color
    for eda in [edad[edai] for edai in node.eda_list]:
        if eda.right == node.index:
            oppo = eda.left
        else:
            oppo = eda.right

        if noded[oppo].color == -1:
            if eda.w % 2 == 0:
                noded[oppo].color = node.color
            else:
                noded[oppo].color = (node.color + 1) % 2
            color_oppo_of(noded[oppo])

def solve():
    n = int(input())
    for i in range(n-1):
        U, V, W = map(int, input().split())
        u = U
        v = V
        w = W

        eda = Eda(i, w, u, v)
        edad[i] = eda

        if u not in noded.keys():
            noded[u] = Node(u)
        noded[u].eda_list.append(i)

        if v not in noded.keys():
            noded[v] = Node(v)
        noded[v].eda_list.append(i)

    noded[1].color = 0
    color_oppo_of(noded[1])

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


solve()

Submission status: Runtime Error

Solution code:
class Node:
    def __init__(self, id, p, w):
        self.id = id
        self.w = w
        self.parent = p
        self.childs = []


N = int(input())
uvw = [list(map(int, input().split())) for _ in range(N - 1)]
nodes = [None for i in range(N + 1)]
nodes[1] = Node(1, None, 0)
for u, v, w in sorted(uvw):
    if nodes[u] != None:
        nodes[v] = Node(v, nodes[u], nodes[u].w + w)
        nodes[u].childs.append(nodes[v])
    elif nodes[v] != None:
        nodes[u] = Node(u, nodes[v], nodes[v].w + w)
        nodes[v].childs.append(nodes[u])

for node in nodes[1:]:
    print(node.w % 2)

Submission status: Runtime Error

Solution code:
N = int(input())

tree_dic = {i+1:[] for i in range(N)}
bw_list = [0]*N

for i in range(1, N):
    ui, vi, wi = map(int, input().split())
    tree_dic[ui].append((vi, wi))
    tree_dic[vi].append((ui, wi))

passed_place_lst = [0]*N
passed_place_lst[0] = 1
distance_dic = {1:0}
def decide_color(distance_dic, current_point, tree_dic, passed_place_lst):
    for next_point, add_distance in tree_dic[current_point]:
        passed_place_lst[next_point-1] += 1
        if next_point not in distance_dic:
            distance_dic[next_point] = add_distance + distance_dic[current_point]
            decide_color(distance_dic, next_point, tree_dic, passed_place_lst)
            
        elif add_distance + distance_dic[current_point] < distance_dic[next_point]:
            distance_dic[next_point] = add_distance + distance_dic[current_point]
            decide_color(distance_dic, next_point, tree_dic, passed_place_lst)

current_point = 1
while 0 in passed_place_lst:
    decide_color(distance_dic, current_point, tree_dic, passed_place_lst)
    for place in range(N):
        if passed_place_lst[place] != 0:
            del tree_dic[place+1]
    if len(tree_dic) != 0:
        current_point = list(tree_dic.keys())[0]

            


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

Submission status: Runtime Error

Solution code:
nnn = int(input())

a = [-2 for i in range(nnn)]
a[0] = 0

def m():
	return map(int,input().split())


def oon(x,n,nn,k):
	global a
	for i in range(len(x[n])):
		if x[n][i][0] != nn and a[nn] != -2 and a[a[x[n][i][0]]] == -2:
			p = (k + x[n][i][1]) % 2
			if p == 0:
				a[x[n][i][0]] = 0
			else:
				a[x[n][i][0]] = 1
			oon(x,x[n][i][0],n,p)



b = [[] for i in range(nnn)]

for i in range(nnn-1):
	z,y,c = m();
	b[z-1].append([y-1,c])
	b[y-1].append([z-1,c])

oon(b,0,0,0)

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