Problem p03044 (python)

Fixed submission:
N = int(input())
G = [[] for i in range(N)]
for i in range(N-1):
  u, v, w = list(map(int, input().split()))
  u -= 1
  v -= 1
  w = w % 2
  G[u].append([v, w])
  G[v].append([u, w])
  
res = [-1] * N
"""
def dfs(u, p, c):
  res[u] = c
  for n in G[u]:
    if n[0] == p or res[n[0]] != -1:
      continue
    if n[1] == 1:
      dfs(n[0], u, 1-c)
    else:
      dfs(n[0], u, c)
"""
from collections import deque
q = deque()
# 再帰はメモリ的に厳しい？？
def search(q):
  while q:
    u, p, c = q.popleft()
    if res[u] == -1:
      for n in G[u]:
        if n[0] == p or res[n[0]] != -1:
          continue
        if n[1] == 1:
          q.append((n[0], u, 1-c))
        else:
          q.append((n[0], u, c))
    res[u] = c
  
  
def main():
  q.append((0, -1, 1))
  search(q)
  for i in res:
    print(i)
  
  
if __name__ == "__main__":
  main()

Buggy submission (verdict: Runtime Error):
N = int(input())
G = [[] for i in range(N)]
for i in range(N-1):
  u, v, w = list(map(int, input().split()))
  u -= 1
  v -= 1
  w = w % 2
  G[u].append([v, w])
  G[v].append([u, w])
  
res = [-1] * N
def dfs(u, p, c):
  res[u] = c
  for n in G[u]:
    if n[0] == p or res[n[0]] != -1:
      continue
    if n[1] == 1:
      dfs(n[0], u, 1-c)
    else:
      dfs(n[0], u, c)
  
def main():
  dfs(0, -1, 1)
  for i in res:
    print(i)
  
  
if __name__ == "__main__":
  main()

Buggy submission (verdict: Runtime Error):
N = int(input())
G = [[] for i in range(N)]
for i in range(N-1):
  u, v, w = list(map(int, input().split()))
  u -= 1
  v -= 1
  w = w % 2
  G[u].append([v, w])
  G[v].append([u, w])
  
res = [-1] * N
def dfs(u, p, c):
  if res[u] == -1:
    res[u] = c
    for n in G[u]:
      if n[0] == p:
        continue
      if n[1] == 1:
        dfs(n[0], u, 1-c)
      else:
        dfs(n[0], u, c)
  
def main():
  dfs(0, -1, 1)
  for i in res:
    print(i)
  
  
if __name__ == "__main__":
  main()

Buggy submission (verdict: Runtime Error):
N = int(input())
G = [[] for i in range(N)]
for i in range(N-1):
  u, v, w = list(map(int, input().split()))
  u -= 1
  v -= 1
  w = w % 2
  G[u].append([v, w])
  G[v].append([u, w])
  
res = [-1] * N
def dfs(u, p, c):
  if res[u] != -1:
    return 0
  res[u] = c
  for n in G[u]:
    if n[0] == p:
      continue
    if n[1] == 1:
      dfs(n[0], u, 1-c)
    else:
      dfs(n[0], u, c)
  
def main():
  dfs(0, -1, 1)
  for i in res:
    print(i)
  
  
if __name__ == "__main__":
  main()

Buggy submission (verdict: Runtime Error):
# 再起の回数？
N = int(input())
G = [[] for i in range(N)]
for i in range(N-1):
  u, v, w = list(map(int, input().split()))
  u -= 1
  v -= 1
  w = w % 2
  G[u].append([v, w])
  G[v].append([u, w])
  
res = [0] * N
def dfs(u, p, c):
  res[u] = c
  for n in G[u]:
    if n[0] == p:
      continue
    if n[1] == 1:
      dfs(n[0], u, 1-c)
    else:
      dfs(n[0], u, c)
  
def main():
  dfs(0, -1, 1)
  for i in res:
    print(i)
  
  
if __name__ == "__main__":
  main()

Buggy submission (verdict: Runtime Error):
N = int(input())
G = {i:[] for i in range(N)}
for i in range(N-1):
  u, v, w = list(map(int, input().split()))
  u -= 1
  v -= 1
  w = w % 2
  G[u].append([v, w])
  G[v].append([u, w])
res = [0] * N

def dfs(u, p, c):
  res[u] = c
  for n in G[u]:
    if n[0] == p:
      continue
    if n[1]:
      dfs(n[0], u, 1-c)
    else:
      dfs(n[0], u, c)
  
def main():
  dfs(0, -1, 1)
  for i in res:
    print(i)
  
  
if __name__ == "__main__":
  main()

Buggy submission (verdict: Runtime Error):
N = int(input())
G = {i:[] for i in range(N)}
for i in range(N-1):
  u, v, w = list(map(int, input().split()))
  u -= 1
  v -= 1
  w = w % 2
  G[u].append([v, w])
  G[v].append([u, w])
res = [0] * N

def dfs(u, p, c):
  res[u] = c
  for n in G[u]:
    if n[0] == p:
      continue
    if n[1]:
      dfs(n[0], u, (c+1)%2)
    else:
      dfs(n[0], u, c)
  
def main():
  dfs(0, -1, 0)
  for i in res:
    print(i)
  
  
if __name__ == "__main__":
  main()

Buggy submission (verdict: Runtime Error):
N = int(input())
G = {i:[] for i in range(N)}
for i in range(N-1):
  u, v, w = list(map(int, input().split()))
  u -= 1
  v -= 1
  w = w % 2
  G[u].append([v, w])
  G[v].append([u, w])
res = [0] * N

def dfs(u, p, c):
  res[u] = c
  for n in G[u]:
    if n[0] == p:
      continue
    if n[1]:
      dfs(n[0], u, 1-c)
    else:
      dfs(n[0], u, c)
  
def main():
  dfs(0, -1, 1)
  for i in res:
    print(i)
  
  
if __name__ == "__main__":
  main()