Problem p03044 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10**7)
N = int(input())
L = [-1 for i in range(N)]
flg = [0 for i in range(N)]
M = [[] for i in range(N)]
for i in range(N-1):
  u, v, w = map(int, input().split())
  M[u-1].append([v-1, w%2])
  M[v-1].append([u-1, w%2])
L[0] = 0
def dist(m, d):
  L[m] = d % 2
  for i in M[m]:
    if L[i[0]] == -1:
      dist(i[0], i[1] + d)
dist(0, 0)
for i in range(N):
  print(L[i])

Buggy submission (verdict: Runtime Error):
N = int(input())
L = [-1 for i in range(N)]
flg = [0 for i in range(N)]
M = [[] for i in range(N)]
for i in range(N-1):
  u, v, w = map(int, input().split())
  M[u-1].append([v-1, w%2])
  M[v-1].append([u-1, w%2])
L[0] = 0
def dist(m, d):
  L[m] = d % 2
  for i in M[m]:
    if L[i[0]] == -1:
      dist(i[0], i[1] + d)
dist(0, 0)
for i in range(N):
  print(L[i])

Buggy submission (verdict: Runtime Error):
N = int(input())
L = [-1 for i in range(N)]
flg = [0 for i in range(N)]
D = {}
S = []
A = []
u, v, w = map(int, input().split())
if w % 2 == 0:
  D[u], D[v] = 0, 0
else:
  D[u], D[v] = 0, 1
for i in range(N - 2):
  u, v, w = map(int, input().split())
  if u in D:
    D[v] = (D[u] + w) % 2
  elif v in D:
    D[u] = (D[v] + w) % 2
  else:
    if w % 2 == 0:
      S.append([u, v])
    else:
      A.append([u, v])
for i in range(len(S)):
  if S[i][0] in D:
    D[S[i][1]] = D[S[i][0]]
  elif S[i][1] in D:
    D[S[i][0]] = D[S[i][1]]
for i in range(len(A)):
  if A[i][0] in D:
    D[A[i][1]] = (D[A[i][0]] + 1) % 2
  elif A[i][1] in D:
    D[A[i][0]] = (D[A[i][1]] + 1) % 2
for i in range(len(S)):
  if S[i][0] in D:
    D[S[i][1]] = D[S[i][0]]
  elif S[i][1] in D:
    D[S[i][0]] = D[S[i][1]]
for i in range(1, N+1):
  print(D[i])

Buggy submission (verdict: Runtime Error):
N = int(input())
L = [-1 for i in range(N)]
flg = [0 for i in range(N)]
D = {}
S = []
A = []
u, v, w = map(int, input().split())
if w % 2 == 0:
  D[u], D[v] = 0, 0
else:
  D[u], D[v] = 0, 1
for i in range(N - 2):
  u, v, w = map(int, input().split())
  if u in D:
    D[v] = (D[u] + w) % 2
  elif v in D:
    D[u] = (D[v] + w) % 2
  else:
    if w % 2 == 0:
      S.append([u, v])
    else:
      A.append([u, v])
for i in range(len(S)):
  if S[i][0] in D and S[i][1] not in D:
    D[S[i][1]] = D[S[i][0]]
  elif S[i][1] in D and S[i][0] not in D:
    D[S[i][0]] = D[S[i][1]]
for i in range(len(A)):
  if A[i][0] in D and A[i][1] not in D:
    D[A[i][1]] = (D[A[i][0]] + 1) % 2
  elif A[i][1] in D and A[i][0] not in D:
    D[A[i][0]] = (D[A[i][1]] + 1) % 2
for i in range(1, N+1):
  print(D[i])
print(S, A)

Buggy submission (verdict: Runtime Error):
N = int(input())
L = [-1 for i in range(N)]
flg = [0 for i in range(N)]
D = {}
S = []
A = []
u, v, w = map(int, input().split())
if w % 2 == 0:
  D[u], D[v] = 0, 0
else:
  D[u], D[v] = 0, 1
for i in range(N - 2):
  u, v, w = map(int, input().split())
  if u in D:
    D[v] = (D[u] + w) % 2
  elif v in D:
    D[u] = (D[v] + w) % 2
  else:
    if w % 2 == 0:
      S.append([u, v])
    else:
      A.append([u, v])
for i in range(len(S)):
  if S[i][0] in D:
    D[S[i][1]] = D[S[i][0]]
  elif S[i][1] in D:
    D[S[i][0]] = D[S[i][1]]
for i in range(len(A)):
  if A[i][0] in D:
    D[A[i][1]] = (D[A[i][0]] + 1) % 2
  elif A[i][1] in D:
    D[A[i][0]] = (D[A[i][1]] + 1) % 2
for i in range(1, N+1):
  print(D[i])