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:
class Node:
  def __init__(self):
    self.parent = None
    self.d = 0

  def dist(self):
    if self.parent is None:
      return 0
    else:
      return self.d + self.parent.dist()
    
    
n = int(input())

nodes = dict()
for _ in range(n-1):
  u, v, w = map(int, input().split(' '))
  un = nodes[u] if u in nodes else Node()
  vn = nodes[v] if v in nodes else Node()
  
  if vn.parent is None:
    vn.d = w
    vn.parent = un
  else:
    un.d = w
    un.parent = vn
  
  nodes[u] = un
  nodes[v] = vn
  
      
for i in range(1, n+1):
  t = nodes[i]
  print(t.dist()%2)

Submission status: Runtime Error

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

times=n[0]
a = n[1:]
for i in range(times[0]-1):
    if ans[a[i][0]-1]==3 and ans[a[i][1]-1]==3:
        if a[i][2]%2==0:
            ans[a[i][0]-1]=0;ans[a[i][1]-1]=0
        else:
	        ans[a[i][0]-1]=0;ans[a[i][1]-1]=1           
    if ans[a[i][0]-1]==3 and ans[a[i][1]-1]==0:
        if a[i][2]%2==0:
            ans[a[i][0]-1]=0
        else:
            ans[a[i][0]-1]=1
    if ans[a[i][0]-1]==0 and ans[a[i][1]-1]==3:
        if a[i][2]%2==0:
            ans[a[i][1]-1]=0
        else:
            ans[a[i][1]-1]=1  
    if ans[a[i][0]-1]==3 and ans[a[i][1]-1]==1:
        if a[i][2]%2==0:
            ans[a[i][0]-1]=0
        else:
            ans[a[i][0]-1]=0
    if ans[a[i][0]-1]==1 and ans[a[i][1]-1]==3:
        if a[i][2]%2==0:
            ans[a[i][1]-1]=0
        else:
            ans[a[i][1]-1]=0  
              
for i in range(times[0]):
    print(ans[i])

Submission status: Runtime Error

Solution code:
class Node:
  def __init__(self, id):
    self.id = id
    self.parent = None
    self.d = 0

  def dist(self):
    if self.parent is None:
      return 0
    else:
      return self.d + self.parent.dist()
    
    
n = int(input())

nodes = dict()
for _ in range(n-1):
  u, v, w = map(int, input().split(' '))
  un = nodes[u] if u in nodes else Node(u)
  vn = nodes[v] if v in nodes else Node(v)
  
  if vn.parent is None:
    vn.d = w
    vn.parent = un
  else:
    un.d = w
    un.parent = vn
  
  nodes[u] = un
  nodes[v] = vn
  
      
for i in range(1, n+1):
  t = nodes[i]
  print(t.dist()%2)

Submission status: Runtime Error

Solution code:
class Node:
  def __init__(self, id):
    self.id = id
    self.parent = None
    self.children = set()
    self.d = 0

  def dist(self):
    if self.parent is None:
      return 0
    else:
      return self.d + self.parent.dist()
    
    
n = int(input())

nodes = dict()
root = 0
for _ in range(n-1):
  u, v, w = map(int, input().split(' '))
  if u not in nodes and v not in nodes:
    un = Node(u)
    vn = Node(v)
    vn.parent = un
    vn.d = w
    nodes[u] = un
    nodes[v] = vn
    if root==0:
      root = u
  elif u in nodes and v not in nodes:
    un = nodes[u]
    vn = Node(v)
    vn.parent = un
    vn.d = w
    nodes[v] = vn
  elif u not in nodes and v in nodes:
    un = Node(u)
    vn = nodes[v]
    un.parent = vn
    un.d = w
    nodes[u] = un
  else:
    un = nodes[u]
    vn = nodes[v]
    if un.parent is None:
      un.parent = vn
      un.d = w
    else:
      vn.parent = un
      vn.d = w

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

Submission status: Runtime Error

Solution code:
class Node:
  def __init__(self, id):
    self.id = id
    self.parent = None
    self.children = set()

n = int(input())

edges = dict()
nodes = dict()
root = 0
for _ in range(n-1):
  u, v, w = map(int, input().split(' '))
  edges[(u, v)] = w
  if u not in nodes and v not in nodes:
    un = Node(u)
    vn = Node(v)
    vn.parent = un
    un.children.add(vn)
    nodes[u] = un
    nodes[v] = vn
    if root==0:
      root = u
  elif u in nodes and v not in nodes:
    un = nodes[u]
    vn = Node(v)
    vn.parent = un
    un.children.add(vn)
    nodes[v] = vn
  elif u not in nodes and v in nodes:
    un = Node(u)
    vn = nodes[v]
    un.parent = vn
    vn.children.add(un)
    nodes[u] = un
  else:
    un = nodes[u]
    vn = nodes[v]
    if un.parent is None:
      un.parent = vn
      vn.children.add(un)
    else:
      vn.parent = un
      un.children.add(vn)

      
for i in range(1, n+1):
  t = nodes[i]
  d = 0
  while t.id!=root:
    p = t.parent
    d += edges[(min(t.id, p.id), max(t.id, p.id))]
    t = p
  print(d%2)

Submission status: Runtime Error

Solution code:
class Node:
  def __init__(self, id):
    self.id = id
    self.parent = None
    self.children = set()

n = int(input())

edges = dict()
nodes = dict()
root = 0
for _ in range(n-1):
  u, v, w = map(int, input().split(' '))
  edges[(u, v)] = w
  if u not in nodes and v not in nodes:
    un = Node(u)
    vn = Node(v)
    vn.parent = un
    un.children.add(vn)
    nodes[u] = un
    nodes[v] = vn
    if root==0:
      root = u
  else:
    if u in nodes:
      un = nodes[u]
      vn = Node(v)
      vn.parent = un
      un.children.add(vn)
      nodes[v] = vn
    else:
      un = Node(u)
      vn = nodes[v]
      un.parent = vn
      vn.children.add(un)
      nodes[u] = un

      
for i in range(1, n+1):
  t = nodes[i]
  d = 0
  while t.id!=root:
    p = t.parent
    d += edges[(min(t.id, p.id), max(t.id, p.id))]
    t = p
  print(d%2)

Submission status: Runtime Error

Solution code:
from collections import defaultdict
n = int(input())
g = defaultdict(list)
for i in range(n - 1):
    u, v, k = [int(x) for x in input().split()]
    u -= 1
    v -= 1
    g[u].append([v, k])
    g[v].append([u, k])
c = [0] + [-1] * (n - 1)

def dfs(u, p):
    for v, k in g[u]:
        if v != p:
            c[v] = c[u] if k % 2 == 0 else c[u] ^ 1
            dfs(v, u)

dfs(0, -1)
for _ in c:
    print(_)

Submission status: Runtime Error

Solution code:
# input data
n=int(input())
u=[]
v=[]
w=[]
for i in range(0,n-1):
	ui,vi,wi = map(int, input().split())
	u.append(ui)
	v.append(vi)
	w.append(wi%2)


# solve
ans=[]
for i in range(0,n+1):
	ans.append(-1)

_neighbors=[]
for i in range(0,n+1):
	_neighbors.append([])

for i in range(0,n-1):
	_neighbors[u[i]].append([v[i], w[i]])
	_neighbors[v[i]].append([u[i], w[i]])

# paint v with c=0,1 and its neighbors
def bfs(u,c):
	if ans[u] != -1:
		return

	ans[u] = c

	for vw in _neighbors[u]:
		if vw[1] == 0:
			bfs(vw[0],c)
		else:
			bfs(vw[0],1-c)

bfs(1,0)

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

Submission status: Runtime Error

Solution code:
# input data
n=int(input())
u=[]
v=[]
w=[]
for i in range(0,n-1):
	ui,vi,wi = map(int, input().split())
	u.append(ui)
	v.append(vi)
	w.append(wi%2)


# solve
ans=[]
for i in range(0,n+1):
	ans.append(-1)

_neighbors=[]
for i in range(0,n+1):
	_neighbors.append([])

for i in range(0,n-1):
	_neighbors[u[i]].append([v[i], w[i]])
	_neighbors[v[i]].append([u[i], w[i]])

# paint v with c=0,1 and its neighbors
def bfs(u,c):
	if ans[u] != -1:
		return

	ans[u] = c

	for vw in _neighbors[u]:
		if vw[1] == 0:
			bfs(vw[0],c)
		else:
			bfs(vw[0],1-c)

bfs(1,0)

print(ans)

Submission status: Runtime Error

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

class Path:
    def __init__(self, next, length, num):
        self.next=next
        self.len=length
        self.num=num

class Node:
    def __init__(self, index):
        self.index = index
        self.paths = []

    def append(self, next, length, num):
        self.paths.append(Path(next, length, num))

nodes = [Node(i) for i in range(N)]
for i in range(N-1):
    nodes[u[i]-1].append(v[i]-1, w[i], i)
    nodes[v[i]-1].append(u[i]-1, w[i], i)

pasts = []
paints = [-1]*N

def rec(pos, color):
    if paints[pos] == -1:
        paints[pos] = color
    for path in nodes[pos].paths:
        # まだ通ってない道を通る
        if path.num not in pasts:
            pasts.append(path.num)
            if path.len % 2 == 1:
                rec(path.next, (color + 1) % 2)
            else:
                rec(path.next, color)
        # さっきの道を戻ってくる

rec(0, 0)

for i in paints:
    print(i)

Submission status: Runtime Error

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())
    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()
    if d[s] != -1:
        for x,w in connect_list[s]:
            d[x] = d[s] + w
            stack.append(x)

for i in range(N):
    print(i%2)

Submission status: Runtime Error

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
#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])
#a[0]=a[0]-1
aa= a[:].reshape(times,3)

Submission status: Runtime Error

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
#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])
#a[0]=a[0]-1
aa= a[:].reshape(times-1,3)

Submission status: Runtime Error

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
#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])
#a[0]=a[0]-1
aa= a[:].reshape(times-1,3)
ans=np.zeros(times+1)+3
ans=np.int16(ans)
for i in range(times-1):
    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,times+1):
    print(ans[i])

Submission status: Runtime Error

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//3
#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])
#a[0]=a[0]-1
aa= a[:].reshape(times-1,3)
ans=np.zeros(times+1)+3
ans=np.int16(ans)
for i in range(times-1):
    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,times+1):
    print(ans[i])

Submission status: Runtime Error

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()//3
#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])
#a[0]=a[0]-1
aa= a[:].reshape(times-1,3)
ans=np.zeros(times+1)+3
ans=np.int16(ans)
for i in range(times-1):
    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,times+1):
    print(ans[i])

Submission status: Runtime Error

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//3
#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])
#a[0]=a[0]-1
aa= a[:].reshape(times-1,3)
ans=np.zeros(times+1)+3
ans=np.int16(ans)
for i in range(times-1):
    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,times+1):
    print(ans[i])

Submission status: Runtime Error

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

times=int(input())

Submission status: Runtime Error

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

times=int(input())
a = np.array(n).flatten()#np.array([int(x) for x in input().split()])

Submission status: Runtime Error

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

times=int(input())
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])
#a[0]=a[0]-1
aa= a[:].reshape(times-1,3)
ans=np.zeros(times+1)+3
ans=np.int16(ans)
for i in range(times-1):
    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,times+1):
    print(ans[i])

Submission status: Runtime Error

Solution code:
import numpy as np

n = [[int(i) for i in input().split()] for i in range(int(input()))]

Submission status: Runtime Error

Solution code:
import numpy as np

n = [[int(i) for i in input().split()] for i in range(int(input()))]

#times=5
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])
#a[0]=a[0]-1
aa= a[:].reshape(times-1,3)
ans=np.zeros(times+1)+3
ans=np.int16(ans)
for i in range(times-1):
    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,times+1):
    print(ans[i])

Submission status: Runtime Error

Solution code:
import numpy as np

n = [[int(i) for i in input().split()] for i in range(int(input()))]

#times=5
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])
#a[0]=a[0]-1
aa= a[:].reshape(times-1,3)
ans=np.zeros(times+1)+3

Submission status: Runtime Error

Solution code:
import numpy as np

times = int(input())
n = [[int(i) for i in input().split()] for i in range(times)] 

#times=5
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])
#a[0]=a[0]-1

Submission status: Runtime Error

Solution code:
times = int(input())
n = [input() for i in range(times)]

Submission status: Runtime Error

Solution code:
times = int(input())  #3
n = [input() for i in range(times)]

Submission status: Runtime Error

Solution code:
import numpy as np
times = int(input())  # 3
n = [input().split() for i in range(times)]
#times=5
a = np.array(n)#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])
#a[0]=a[0]-1

Submission status: Runtime Error

Solution code:
import numpy as np
times = int(input())  # 3
n = [input().split() for i in range(times)]
#times=5
a = np.array(n)#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])
#a[0]=a[0]-1
aa= a[:].reshape(times-1,3)
ans=np.zeros(times+1)+3
ans=np.int16(ans)

Submission status: Runtime Error

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

Solution code:
import numpy as np
times = int(input())  # 3
n = [input().split() for i in range(times)]
#times=5
a = np.array(n)#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])
#a[0]=a[0]-1
aa= a[:].reshape(times-1,3)
ans=np.zeros(times+1)+3
ans=np.int16(ans)
for i in range(times-1):
    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,times+1):
    print(ans[i])

Submission status: Runtime Error

Solution code:
import numpy as np
times = int(input())  # 3
n = [input().split() for i in range(times)]
times=np.concatenate((times,n))
a = np.array(times)#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=np.array([3,1, 2, 2,2, 3, 1])
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]+2):
    print(ans[i])

Submission status: Runtime Error

Solution code:
import numpy as np
times = int(input())  # 3
n = [input().split() for i in range(times)]
times=np.concatenate((times,n))
a = times#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=np.array([3,1, 2, 2,2, 3, 1])
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]+2):
    print(ans[i])

Submission status: Runtime Error

Solution code:
n=int(input());e=[[]for _ in[0]*-~n];q=[(1,0)];f=[-1]*n
while n>1:v,w,c=map(int,input().split());e[v]+=(w,c),;e[w]+=(v,c),;n-=1
while q:
 v,c=q.pop();f[v-1]=c%2
 for w,d in e[v]:q+=(w,c+d),*(f[w-1]<0)
print(*f)

Submission status: Runtime Error

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=np.array([3,1, 2, 2,2, 3, 1])
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]+2):
    print(ans[i])

Submission status: Runtime Error

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=np.array([3,1, 2, 2,2, 3, 1])
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]+2):
    print(ans[i])

Submission status: Runtime Error

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 = []

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

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])+3
ans=np.int16(ans)
for i in range(a[0]):
    if ans[aa[0,i]]==3 and ans[aa[1,i]]==3:
        if aa[2,i]%2==0:
            ans[aa[0,i]]=0
            ans[aa[1,i]]=0
        else:
        	ans[aa[0,i]]=0
            ans[aa[1,i]]=1
    if ans[aa[0,i]]==3 and ans[aa[1,i]]==0:
        if aa[2,i]%2==0:
        	ans[aa[0,i]]=0
        else:
        	ans[aa[0,i]]=1
    if ans[aa[0,i]]==0 and ans[aa[i,1]]==3:
        if aa[2,i]%2==0:
        	ans[aa[1,i]]=0
        else:
        	ans[aa[1,i]]=1  
    if ans[aa[0,i]]==3 and ans[aa[1,i]]==1:
        if aa[2,i]%2==0:
        	ans[aa[0,i]]=0
        else:
        	ans[aa[0,i]]=0
    if ans[aa[0,i]]==1 and ans[aa[1,i]]==3:
        if aa[2,i]%2==0:
        	ans[aa[1,i]]=0
        else:
        	ans[aa[1,i]]=0  
              
for i in range(a[0]):
	print(ans[i])

Submission status: Runtime Error

Solution code:
from collections import deque

N = int(input())
edges = {i: [] for i in range(N)}
for _ in range(M-1):
    x, y, z = map(int, input().split())
    edges[x-1].append((y-1, z%2))
    edges[y-1].append((x-1, z%2))
color = [None]*N

q = deque([(0, 0)])
while q:
    v, c = q.popleft()
    color[v] = c
    for u, w in edges[v]:
        if color[u] is None:
            if w == 0:
                q.append((u, c))
            else:
                q.append((u, c^1))
print(*color, sep='\n')

Submission status: Runtime Error

Solution code:
import numpy as np
a = np.array[int(x) for x in input().split()]
aa= a[1:].reshape(3,a[0])
ans=np.zeros(a[0])+3
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[i,1]]==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(a[0]):
	print(ans[i])

Submission status: Runtime Error

Solution code:
import numpy as np
a = np.array[int(x) for x in input().split()]
aa= a[1:].reshape(3,a[0])
ans=np.zeros(a[0])+3
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[i,1]]==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(ans):
	print(i)