Problem p03044 (python)

Fixed submission:
N = int(input())
u, v, w = [0]*(N-1), [0]*(N-1), [0]*(N-1)
n = [-1] * N
# n[][0] 色　n[][1] 連結先一覧リスト n[][2] mod2 
for i in range(N):
    n[i] = [0]*4
    n[i][1] = []
    n[i][2] = []

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

stack = [u[0]-1]

while True:
    num = stack.pop()
    n[num][3] = 1
    for i in range(len(n[num][1])):
        if n[n[num][1][i]][3] == 0:
            stack.append(n[num][1][i])
            if n[num][2][i] == 0:
                n[n[num][1][i]][0] = n[num][0]
            else:
                n[n[num][1][i]][0] = -1 * n[num][0] + 1
    if len(stack) == 0:
        break

for i in n:
    print(i[0])

Buggy submission (verdict: Runtime Error):
N = int(input())
u, v, w = [0]*(N-1), [0]*(N-1), [0]*(N-1)
n = [-1] * N
# n[][0] 色　n[][1] 連結先一覧リスト n[][2] mod2 
for i in range(N):
    n[i] = [0]*4
    n[i][1] = []
    n[i][2] = []

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

# n[][3] 探索済フラグ
def dfs(num):
    n[num][3] = 1
    for i in range(len(n[num][1])):
        if n[n[num][1][i]][3] == 0:
            if n[num][2][i] == 0:
                n[n[num][1][i]][0] = n[num][0]
                dfs(n[num][1][i])
            else:
                n[n[num][1][i]][0] = -1*n[num][0] + 1
                dfs(n[num][1][i]) 

dfs(u[0]-1)

for i in n:
    print(i[0])

Buggy submission (verdict: Runtime Error):
N = int(input())
u, v, w = [0]*(N-1), [0]*(N-1), [0]*(N-1)
n = [-1] * N 
# n[][0] 色　n[][1] 連結先一覧リスト(連結先奇数なら+ 偶数なら-がかかる1追加された値)
for i in range(N):
    n[i] = [0]*3
    n[i][1] = []


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

# n[][2] 探索済フラグ
def dfs(num):
    n[num][2] = 1
    for item in n[num][1]:
        if n[abs(item)-1][2] == 0:
            if item < 0:
                item = -1*item
                n[item-1][0] = n[num][0]
                dfs(item-1)
            else:
                n[item-1][0] = -1 * n[num][0] +1
                dfs(item-1)

dfs(u[0]-1)

for i in n:
    print(i[0])

Buggy submission (verdict: Runtime Error):
N = int(input())
u, v, w = [0]*(N-1), [0]*(N-1), [0]*(N-1)
n = [-1] * N 
# n[][0] 色　n[][1] 連結先一覧リスト(連結先奇数なら+ 偶数なら-がかかる1追加された値)
for i in range(N):
    n[i] = [0]*3
    n[i][1] = []


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

# n[][2] 探索済フラグ
def dfs(num):
    n[num][2] = 1
    for item in n[num][1]:
        if n[abs(item)-1][2] == 0:
            if item < 0:
                item = -1*item
                n[item-1][0] = n[num][0]
                dfs(item-1)
            else:
                n[item-1][0] = -1 * n[num][0] +1
                dfs(item-1)

dfs(0)

for i in n:
    print(i[0])

Buggy submission (verdict: Runtime Error):
N = int(input())
u, v, w = [0]*(N-1), [0]*(N-1), [0]*(N-1)
n = [-1] * N 
# n[][0] 色　n[][1] 連結先一覧リスト(連結先奇数なら+ 偶数なら-がかかる1追加された値)
for i in range(N):
    n[i] = [0]*2
    n[i][1] = []

visited = []

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

def dfs(num):
    visited.append(num)
    for item in n[num][1]:
        if not (abs(item)-1 in visited):
            if item < 0:
                item = -1*item
                n[item-1][0] = n[num][0]
                dfs(item-1)
            else:
                n[item-1][0] = -1 * n[num][0] +1
                dfs(item-1)

dfs(0)

for i in n:
    print(i[0])