Problem p03044 (python)

Fixed submission:
import sys
sys.setrecursionlimit(100**4)

class Edge():
	def __init__(self, v, w):
		self._v = v
		self._w = w
	def v(self):
		return self._v
	def w(self):
		return self._w

def dfs(v, g, colors, color = 0):
	colors[v] = color
	if not len(g[v]):
		return
	for nv in g[v]:
		if colors[nv.v()] != -1:
			continue
		nc = color if nv.w() % 2 == 0 else 1 - color
		dfs(nv.v(), g, colors, nc)

def resolve():
	n = int(input())
	colors = [-1]*n
	g = [list() for _ in range(n)]
	for _ in range(n-1):
		u, v, w = map(int, input().split())
		g[v-1].append(Edge(u-1, w))
		g[u-1].append(Edge(v-1, w))
	for i in range(n):
		if colors[i] != -1:
			continue
		dfs(i, g, colors)
	for c in colors:
		print(c)
resolve()

Buggy submission (verdict: Runtime Error):
def coloring(v, p, vs, cs, color):
	cs[v] = color
	for e in vs[v]:
		if e[0] == p:
			continue
		nc = color if e[1] % 2 == 0 else 1 - color
		coloring(e[0], v, vs, cs, nc)

def resolve():
	n = int(input())
	vs = [[] for _ in range(n)]
	cs = [None]*n
	for i in range(n-1):
		u, v, w = map(int, input().split())
		vs[u-1].append([v-1, w])
		vs[v-1].append([u-1, w])
	coloring(0, None, vs, cs, 1)
	for i in cs:
		print(i)
resolve()

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(100**6)


class Edge():
	def __init__(self, v, w):
		self._v = v
		self._w = w
	def v(self):
		return self._v
	def w(self):
		return self._w

def dfs(v, g, colors, color = 0):
	colors[v] = color
	if not len(g[v]):
		return
	for nv in g[v]:
		if colors[nv.v()] != -1:
			continue
		nc = color if nv.w() % 2 == 0 else 1 - color
		dfs(nv.v(), g, colors, nc)

def resolve():
	n = int(input())
	colors = [-1]*n
	g = [list() for _ in range(n)]
	for _ in range(n-1):
		u, v, w = map(int, input().split())
		g[v-1].append(Edge(u-1, w))
		g[u-1].append(Edge(v-1, w))
	for i in range(n):
		if colors[i] != -1:
			continue
		dfs(i, g, colors)
	for c in colors:
		print(c)
resolve()

Buggy submission (verdict: Runtime Error):
class Edge():
	def __init__(self, v, w):
		self._v = v
		self._w = w
	def v(self):
		return self._v
	def w(self):
		return self._w

def dfs(v, g, colors, color = 0):
	colors[v] = color
	if not len(g[v]):
		return
	for nv in g[v]:
		if colors[nv.v()] != -1:
			continue
		nc = color if nv.w() % 2 == 0 else 1 - color
		dfs(nv.v(), g, colors, nc)

def resolve():
	n = int(input())
	colors = [-1]*n
	g = [list() for _ in range(n)]
	for _ in range(n-1):
		u, v, w = map(int, input().split())
		g[v-1].append(Edge(u-1, w))
		g[u-1].append(Edge(v-1, w))
	for i in range(n):
		if colors[i] != -1:
			continue
		dfs(i, g, colors)
	for c in colors:
		print(c)
resolve()

Buggy submission (verdict: Runtime Error):
class Edge():
	def __init__(self, v, w):
		self._v = v
		self._w = w
	def v(self):
		return self._v
	def w(self):
		return self._w

def dfs(v, g, colors, color = 0):
	colors[v] = color
	if not len(g[v]):
		return
	for nv in g[v]:
		if colors[nv.v()] != -1:
			continue
		nc = color if nv.w() % 2 == 0 else 1 - color
		dfs(nv.v(), g, colors, nc)

def resolve():
	n = int(input())
	colors = [-1]*n
	g = [list() for _ in range(n)]
	for _ in range(n-1):
		u, v, w = map(int, input().split())
		g[v-1].append(Edge(u-1, w))
		g[u-1].append(Edge(v-1, w))
	dfs(0, g, colors)
	for c in colors:
		print(c)
resolve()

Buggy submission (verdict: Runtime Error):
def dfs(v, g, colors, color = 0):
	colors[v] = color
	if not len(g[v]):
		return
	for nv in g[v]:
		if colors[nv.v()] != -1:
			continue
		nc = color if nv.w() % 2 == 0 else 1 - color
		dfs(nv.v(), g, colors, nc)

def resolve():
	n = int(input())
	colors = [-1]*n
	g = [list() for _ in range(n)]
	for _ in range(n-1):
		u, v, w = map(int, input().split())
		g[v-1].append(Edge(u-1, w))
		g[u-1].append(Edge(v-1, w))
	dfs(0, g, colors)
	for c in colors:
		print(c)
resolve()

Buggy submission (verdict: Runtime Error):
class Edge():
	def __init__(self, v, w):
		self._v = v
		self._w = w
	def v(self):
		return self._v
	def w(self):
		return self._w

def dfs(v, g, colors, color = 0):
	colors[v] = color
	for nv in g[v]:
		if colors[nv.v()] != -1:
			continue
		nc = color if nv.w() % 2 == 0 else 1 - color
		dfs(nv.v(), g, colors, nc)

def resolve():
	n = int(input())
	colors = [-1]*n
	g = [list() for _ in range(n)]
	for _ in range(n-1):
		u, v, w = map(int, input().split())
		g[v-1].append(Edge(u-1, w))
		g[u-1].append(Edge(v-1, w))
	dfs(0, g, colors)
	for c in colors:
		print(c)
resolve()

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(100**6)

def dfs(v, p, g, color, c = 0):
	color[v] = c
	for nv in g[v]:
		if nv[0] == p:
			continue
		nc = c if nv[1] % 2 == 0 else 1 - c
		dfs(nv[0], v, g, color, nc)

def resolve():
	n = int(input())
	color = [-1]*n
	g = [[] for _ in range(n)]
	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])
	dfs(0, -1, g, color)
	for i in color:
		print(i)
resolve()

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(100**6)

def dfs(v, p, g, color, c = 0):
	color[v] = c
	for nv in g[v]:
		if nv[0] == p:
			continue
		nc = c if nv[1] % 2 == 0 else 1 - c
		dfs(nv[0], v, g, color, nc)

def resolve():
	n = int(input())
	color = [-1]*n
	g = [[] for _ in range(n)]
	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])
	for v in range(n):
		dfs(v, -1, g, color)
	for i in color:
		print(i)
resolve()

Buggy submission (verdict: Runtime Error):

def dfs(v, p, g, color, c = 0):
	color[v] = c
	for nv in g[v]:
		if nv[0] == p:
			continue
		nc = c if nv[1] % 2 == 0 else 1 - c
		dfs(nv[0], v, g, color, nc)

def resolve():
	n = int(input())
	color = [-1]*n
	g = [[] for _ in range(n)]
	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])
	for v in range(n):
		dfs(v, -1, g, color)
	for i in color:
		print(i)
resolve()

Buggy submission (verdict: Runtime Error):
def dfs(v, g, color, paint=0):
	color[v] = paint
	for i in range(len(g)):
		if i == v:
			continue
		if color[i] != -1:
			continue
		if g[v][i]:
			# np = paint if g[v][i] % 2 == 0 else 1 - paint
			dfs(i, g, color, paint)

def resolve():
	n = int(input())
	color = [-1]*n
	g = [[-1]*n for _ in range(n)]
	for _ in range(n-1):
		u, v, w = map(int, input().split())
		g[u-1][v-1] = w
		g[v-1][u-1] = w
	for v in range(n):
		if color[v] > -1:
			continue
		dfs(v, g, color)

	for i in color:
		print(i)

resolve()

Buggy submission (verdict: Runtime Error):
def dfs(v, g, color, paint=0):
	color[v] = paint
	for i in range(len(g)):
		if i == v:
			continue
		if color[i] != -1:
			continue
		if g[v][i]:
			np = paint if g[v][i] % 2 == 0 else 1 - paint
			dfs(i, g, color, np)

def resolve():
	n = int(input())
	color = [-1]*n
	g = [[-1]*n for _ in range(n)]
	for _ in range(n-1):
		u, v, w = map(int, input().split())
		g[u-1][v-1] = w
		g[v-1][u-1] = w
	for v in range(n):
		if color[v] > -1:
			continue
		dfs(v, g, color)

	for i in color:
		print(i)

resolve()