EPS/11-03-24/a.py
2024-03-15 14:06:32 +01:00

58 lines
1.2 KiB
Python

import sys
from types import GeneratorType
def bootstrap(f, stack=[]):
def wrappedfunc(*args, **kwargs):
if stack:
return f(*args, **kwargs)
to = f(*args, **kwargs)
while True:
if type(to) is GeneratorType:
stack.append(to)
to = next(to)
else:
stack.pop()
if not stack:
break
to = stack[-1].send(to)
return to
return wrappedfunc
global vus
vus = set()
@bootstrap
def dfs(voisins, r, cc):
cc.add(r)
vus.add(r)
for v in voisins[r]:
if v not in vus:
yield dfs(voisins, v, cc)
yield cc
def main():
[n, k] = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
couples = [[int(i) for i in input().split()] for _ in range(k)]
couples += [(j, i) for (i, j) in couples]
# On créé les set() d'arêtes
voisins = {
i: set() for i in range(1, n+1)
}
for (i, j) in couples:
voisins[i].add(j)
voisins[j].add(i)
ccs = [dfs(voisins, i, set()) for i in range(1, n+1) if i not in vus]
return sum([min([a[i-1] for i in cc]) for cc in ccs])
print(main())