Problem p03044 (python)

Fixed submission:
N = int(input())
connect_list = [[] for i in range(N)]
for i in range(N-1):
    u,v,w = map(int,input().split())
    u -= 1
    v -= 1 
    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()
    for x,w in connect_list[s]:
        if d[x] == -1:
            d[x] = d[s] + w
            stack.append(x)

for i in range(N):
    print(d[i]%2)

Buggy submission (verdict: Runtime Error):
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)

Buggy submission (verdict: Runtime Error):
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)

Buggy submission (verdict: Runtime Error):
import numpy as np
N = int(input())
uvw = []
for i in range(N-1):
    u,v,w = map(int,input().split())
    uvw.append([u,v,w])

uvw = np.array(uvw)
uvw = uvw[np.argsort(uvw[:,0])]

# 初期の木の根とランクを設定（初めはみんな根なので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

# 木の根を探索する関数
# 根が自身でない場合は再帰的にたどる
# x,yが同じ集合に属するかの判定はroot(par[x])==root(par[y])か否か
def root(x):
    if par[x] == x:
        return x,d
    else:
        d += dist[]
        par[x], = root(par[x])
        return par[x],d

# 繋げる操作を指定された時に動かす関数
def unite(u,v,w):
    x = par[u]
    y = par[v]
    # rankが高い方に低い方を繋げる
    if rank[x] < rank[y]:
        par[x] = y
        dist[u] += w+dist[v]
    else:
        par[y] = x
        dist[v] += w+dist[u]
        # 同じだった場合xのランクが一つ増える扱いになる
        if rank[x] == rank[y]:
            rank[x] += 1

# 実際にクエリを受け取って動かす
for i in range(N-1):
    u,v,w = uvw[i,0],uvw[i,1],uvw[i,2]
    u = u-1
    v = v-1
    unite(u,v,w)

for i in range(N):
    ,d = root(i)
    if d % 2 == 0:
        print(0)
    else:
        print(1)

Buggy submission (verdict: Runtime Error):
N = int(input())
uvw = []
for i in range(N-1):
    u,v,w = map(int,input().split())
    uvw.append([u,v,w])

uvw = np.array(uvw)
uvw = uvw[np.argsort(uvw[:,0])]

# 初期の木の根とランクを設定（初めはみんな根なので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

# 木の根を探索する関数
# 根が自身でない場合は再帰的にたどる
# x,yが同じ集合に属するかの判定はroot(par[x])==root(par[y])か否か
def root(x):
    if par[x] == x:
        return x
    else:
        par[x] = root(par[x])
        return par[x]

# 繋げる操作を指定された時に動かす関数
def unite(u,v,w):
    x = par[u]
    y = par[v]
    # rankが高い方に低い方を繋げる
    if rank[x] < rank[y]:
        par[x] = y
        dist[u] += w+dist[v]
    else:
        par[y] = x
        dist[v] += w+dist[u]
        # 同じだった場合xのランクが一つ増える扱いになる
        if rank[x] == rank[y]:
            rank[x] += 1

# 実際にクエリを受け取って動かす
for i in range(N-1):
    u,v,w = uvw[i,0],uvw[i,1],uvw[i,2]
    u = u-1
    v = v-1
    unite(u,v,w)

for i in range(N):
    if dist[i] % 2 == 0:
        print(0)
    else:
        print(1)