Problem p03440 (python)

Fixed submission:
import sys
from collections import deque, defaultdict
import heapq

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, M = lr()
A = lr()

# Aでxの根を求める
def find(A,x):
    p = A[x]
    if p == x:
        return x
    a = find(A,p)
    A[x] = a
    return a

# Aでxとyの属する集合の併合
def union(A, x, y):
    if find(A,x) > find(A,y):
        bx, by = find(A,y), find(A,x)
    else:
        bx, by = find(A,x), find(A,y)
    # 根をbxに統一
    A[by] = bx

edges = [[] for _ in range(N)]
V = [i for i in range(N)]
for _ in range(M):
    x, y = lr()
    if find(V, x) != find(V, y):
        union(V, x, y)
    edges[x].append(y)
    edges[y].append(x)

for i in range(N):
    V[i] = find(V, i)

if M == N - 1:
    print(0); exit()
elif N < 2 * (N-M-1):
    print('Impossible'); exit()

root_point = defaultdict(list)
for i in range(N):
    r = V[i]
    heapq.heappush(root_point[r], A[i])

#各グループの最小値以外で使うコストの数
answer = 0
candidates = []
for p_list in root_point:
    answer += heapq.heappop(root_point[p_list])
    candidates.extend(root_point[p_list])

remain = 2 * (N-M-1) - len(set(V))
candidates.sort()
answer += sum(candidates[:remain])
print(answer)

Buggy submission (verdict: Runtime Error):
import sys
from collections import deque, defaultdict
import heapq

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, M = lr()
A = lr()

# Aでxの根を求める
def find(A,x):
    p = A[x]
    if p == x:
        return x
    a = find(A,p)
    A[x] = a
    return a

# Aでxとyの属する集合の併合
def union(A, x, y):
    if find(A,x) > find(A,y):
        bx, by = find(A,y), find(A,x)
    else:
        bx, by = find(A,x), find(A,y)
    # 根をbxに統一
    A[by] = bx

edges = [[] for _ in range(N)]
V = [i for i in range(N)]
for _ in range(M):
    x, y = lr()
    if find(V, x) != find(V, y):
        union(V, x, y)
    edges[x].append(y)
    edges[y].append(x)

for i in range(N):
    V[i] = find(V, i)

dig_0 = [i for i in range(N) if len(edges[i]) == 0]
l0 = len(dig_0)
if N == 1:
    print(0); exit()
elif l0 > N//2:
    print('Impossible'); exit()

# 孤立していない頂点の根
roots = [V[i] for i in range(N) if i not in dig_0]
root_point = defaultdict(list)
for i in range(N):
    if i in dig_0:
        continue
    r = V[i]
    heapq.heappush(root_point[r], A[i])

#各グループの最小値以外で使うコストの数
remain = (l0 + len(set(roots)) - 1) * 2 - (l0 + len(set(roots)))
answer = 0
for i in dig_0:
    answer += A[i]

candidates = deque()
for po in root_point:
    answer += heapq.heappop(root_point[po])
    for i in range(min(remain, len(root_point[po]))):
        candidates.append(heapq.heappop(root_point[po]))

if len(candidates) < remain:
    print('Impossible'); exit()
for i in range(remain):
    answer += heapq.heappop(candidates)
print(answer)
# 43

Buggy submission (verdict: Runtime Error):
import sys
from collections import deque, defaultdict
import heapq

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, M = lr()
A = lr()

# Aでxの根を求める
def find(A,x):
    p = A[x]
    if p == x:
        return x
    a = find(A,p)
    A[x] = a
    return a

# Aでxとyの属する集合の併合
def union(A, x, y):
    if find(A,x) > find(A,y):
        bx, by = find(A,y), find(A,x)
    else:
        bx, by = find(A,x), find(A,y)
    # 根をbxに統一
    A[by] = bx

edges = [[] for _ in range(N)]
V = [i for i in range(N)]
for _ in range(M):
    x, y = lr()
    if find(V, x) != find(V, y):
        union(V, x, y)
    edges[x].append(y)
    edges[y].append(x)

for i in range(N):
    V[i] = find(V, i)

dig_0 = [i for i in range(N) if len(edges[i]) == 0]
l0 = len(dig_0)
if N == 1:
    print(0); exit()
elif l0 > N//2:
    print('Impossible'); exit()

# 孤立していない頂点の根
roots = [V[i] for i in range(N) if i not in dig_0]
root_point = defaultdict(list)
for i in range(N):
    if i in dig_0:
        continue
    r = V[i]
    heapq.heappush(root_point[r], A[i])

#各グループの最小値以外で使うコストの数
remain = (l0 + len(set(roots)) - 1) * 2 - (l0 + len(set(roots)))
answer = 0
for i in dig_0:
    answer += A[i]

candidates = deque()
for po in root_point:
    answer += heapq.heappop(root_point[po])
    for i in range(min(remain, len(root_point[po]))):
        candidates.append(heapq.heappop(root_point[po]))

for i in range(remain):
    answer += heapq.heappop(candidates)
print(answer)
# 43