Problem p03044 (python)

Fixed submission:
N=int(input())
from collections import defaultdict
branch=defaultdict(dict)
for i in range(N-1):
    u,v,w=map(int,input().split())
    branch[u-1][v-1]=w
    branch[v-1][u-1]=w
dist=[float('inf')]*N
dist[0]=0
check={0}
while len(check)>0:
    now=check.pop()
    for nex,w in branch[now].items():
        if dist[nex]>dist[now]+w:
            dist[nex]=dist[now]+w
            check|={nex}
color=[d%2 for d in dist]
for i in range(N):
    print(color[i])

Buggy submission (verdict: Runtime Error):
N=int(input())
root=[-1]*N
color=[-1]*N
from collections import defaultdict
tree=dict()
branch=defaultdict(set)
for i in range(N):
    tree[i]={i}
def search(i):
    if root[i]>=0:
        root[i]=search(root[i])
        return root[i],cnt
    return i
def makecolor(u,v):
    if color[u]==color[v]==-1:
        color[u]=0
        color[v]=1
    elif color[u]==-1:
        color[v]=(color[u]+1)%2
    elif color[v]==-1:
        color[u]=(color[v]+1)%2
    else:
        if color[u]==color[v]:
            if len(tree[u])<=len(tree[v]):
                u,v=v,u
            for x in tree[v]:
                color[x]=(color[x]+1)%2
for i in range(N-1):
    u,v,w=map(int,input().split())
    if w%2==0:
        continue
    u-=1
    v-=1
    branch[u]|={v}
    branch[v]|={u}
    ru=search(u)
    rv=search(v)
    if ru==rv:
        continue
    
    if root[ru]<=root[rv]:
        root[rv]+=root[ru]
        root[ru]=rv
        makecolor(rv,ru)
        tree[rv]|=tree[ru]
    else:
        root[ru]+=root[rv]
        root[rv]=ru
        makecolor(ru,rv)
        tree[ru]|=tree[rv]
color=[i if i!=-1 else 1 for i in color]
for i in range(N):
    print(color[i])

Buggy submission (verdict: Runtime Error):
N=int(input())
root=[-1]*N
color=[-1]*N
from collections import defaultdict
tree=dict()
branch=defaultdict(set)
for i in range(N):
    tree[i]={i}
def search(i):
    if root[i]>=0:
        root[i]=search(root[i])
        return root[i],cnt
    return i
def makecolor(u,v):
    if color[u]==color[v]==-1:
        color[u]=0
        color[v]=1
    elif color[u]==-1:
        color[v]=(color[u]+1)%2
    elif color[v]==-1:
        color[u]=(color[v]+1)%2
    else:
        if color[u]==color[v]:
            if len(tree[u])<=len(tree[v]):
                u,v=v,u
            for x in tree[v]:
                color[x]=(color[x]+1)%2
for i in range(N-1):
    u,v,w=map(int,input().split())
    if w%2==0:
        continue
    u-=1
    v-=1
    branch[u]|={v}
    branch[v]|={u}
    ru=search(u)
    rv=search(v)
    if ru==rv:
        continue
    
    if root[ru]<=root[rv]:
        root[rv]+=root[ru]
        root[ru]=rv
        makecolor(rv,ru)
        tree[rv]|=tree[ru]
    else:
        root[ru]+=root[rv]
        root[rv]=ru
        makecolor(ru,rv)
        tree[ru]|=tree[rv]
color=[i if i!=-1 else 1 for i in color]
for i in range(N):
    print(color[i])

Buggy submission (verdict: Runtime Error):
N=int(input())
root=[-1]*N
color=[-1]*N
from collections import defaultdict
tree=dict()
for i in range(N):
    tree[i]={i}
def search(i,cnt):
    if root[i]>=0:
        cnt+=1
        root[i]=search(root[i])
        return root[i],cnt
    return i,cnt
def makecolor(u,v):
    if color[u]==color[v]==-1:
        color[u]=0
        color[v]=1
    elif color[u]=-1:
        color[v]=(color[u]+1)%2
    elif color[v]=-1:
        color[u]=(color[v]+1)%2
    else:
        if color[u]==color[v]:
            for x in tree[v]:
                color[x]=(color[x]+1)%2
for i in range(N-1):
    u,v,w=map(int,input().split())
    if w%2==0:
        continue
    u-=1
    v-=1
    branch[u]|={v}
    branch[v]|={u}
    ru=search(u)
    rv=search(v)
    if ru==rv:
        continue
    
    if root[ru]<=root[rv]:
        root[rv]+=root[ru]
        root[ru]=rv
        makecolor(rv,ru)
        tree[rv]|=tree[ru]
    else:
        root[ru]+=root[rv]
        root[rv]=ru
        makecolor(ru,rv)
        tree[ru]|=tree[rv]
color=[i if i!=-1 else 1 for i in color]
for i in range(N):
    print(color[i])