Problem p03143 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10**6)

N, M = map(int, input().split())
*X, = map(int, input().split())
E = []
for i in range(M):
    a, b, y = map(int, input().split())
    E.append((y, a-1, b-1))
E.sort()

*p, = range(N)
Z = [0]*M
A = X[:]
C = [-1]*N
def root(x):
    if x == p[x]:
        return x
    p[x] = y = root(p[x])
    return y

def unite(x, y):
    px = root(x); py = root(y)
    if px == py:
        return 0
    if px < py:
        p[py] = px
        A[px] += A[py]
    else:
        p[px] = py
        A[py] += A[px]
    return 1

G = [None for i in range(M)]

ans = 0
for i, (y, a, b) in enumerate(E):
    pa = root(a); pb = root(b)
    ca = C[pa]; cb = C[pb]
    if unite(a, b):
        pa = root(a)
        Z[i] = 1
        C[pa] = i
        #print(">", i, pa, pb, A[pa], y)
        if A[pa] < y:
            G[i] = []
            if ca != -1:
                G[i].append(ca)
            if cb != -1:
                G[i].append(cb)
    else:
        if A[pa] < y:
            G[i] = []
            if ca != -1:
                G[i].append(ca)
            C[pa] = i

sx = sum(X)
#print(Z)

if N > 1:
    from collections import deque
    que = deque()
    if sx < E[C[0]][0]:
        que.append(C[0])
    #print(G, C)
    while que:
        v = que.popleft()
        #print(v)
        if G[v] is not None:
            ans += 1
            for w in G[v]:
                que.append(w)
print(ans)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**6)
N, M = map(int, input().split())
*X, = map(int, input().split())
E = []
for i in range(M):
    a, b, y = map(int, input().split())
    E.append((y, a-1, b-1))
E.sort()

*p, = range(N)
Y = [0]*M; Z = [0]*M
A = X[:]
B = [0]*M
C = [-1]*M
def root(x):
    if x == p[x]:
        return x
    p[x] = y = root(p[x])
    return y

def unite(x, y):
    px = root(x); py = root(y)
    if px == py:
        return 0
    if px < py:
        p[py] = px
        A[px] += A[py]
    else:
        p[px] = py
        A[py] += A[px]
    return 1

G = [None for i in range(M)]

ans = 0
for i, (y, a, b) in enumerate(E):
    pa = root(a); pb = root(b)
    ca = C[pa]; cb = C[pb]
    if unite(a, b):
        pa = root(a)
        Z[i] = 1
        B[i] = A[pa]
        C[pa] = i
        #print(">", i, pa, pb, A[pa], y)
        if A[pa] < y:
            G[i] = []
            if ca != -1:
                G[i].append(ca)
            if cb != -1:
                G[i].append(cb)
    else:
        if A[pa] < y:
            G[i] = []
            if ca != -1:
                G[i].append(ca)
            C[pa] = i

sx = sum(X)
#print(Z)

from collections import deque
que = deque()
if sx < E[C[0]][0]:
    que.append(C[0])
#print(G, C)
while que:
    v = que.popleft()
    #print(v)
    if G[v] is not None:
        ans += 1
        for w in G[v]:
            que.append(w)
print(ans)

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())
*X, = map(int, input().split())
E = []
for i in range(M):
    a, b, y = map(int, input().split())
    E.append((y, a-1, b-1))
E.sort()

*p, = range(N)
Y = [0]*N; Z = [0]*M
A = X[:]
B = [0]*M
C = [-1]*N
def root(x):
    if x == p[x]:
        return x
    p[x] = y = root(p[x])
    return y

def unite(x, y):
    px = root(x); py = root(y)
    if px == py:
        return 0
    if px < py:
        p[py] = px
        A[px] += A[py]
    else:
        p[px] = py
        A[py] += A[px]
    return 1

G = [None for i in range(M)]

ans = 0
for i, (y, a, b) in enumerate(E):
    pa = root(a); pb = root(b)
    ca = C[pa]; cb = C[pb]
    if unite(a, b):
        pa = root(a)
        Z[i] = 1
        B[i] = A[pa]
        C[pa] = i
        #print(">", i, pa, pb, A[pa], y)
        if A[pa] < y:
            G[i] = []
            if ca != -1:
                G[i].append(ca)
            if cb != -1:
                G[i].append(cb)
    else:
        if A[pa] < y:
            G[i] = []
            if ca != -1:
                G[i].append(ca)
            C[pa] = i

sx = sum(X)
#print(Z)

from collections import deque
que = deque()
if sx < E[C[0]][0]:
    que.append(C[0])
#print(G, C)
while que:
    v = que.popleft()
    #print(v)
    if G[v] is not None:
        ans += 1
        for w in G[v]:
            que.append(w)
print(ans)