0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-12-26 19:11:59 +00:00
OI-codes/Luogu/T203629/T203629.cpp
2021-10-31 16:47:06 +08:00

35 lines
699 B
C++

#include <bits/stdc++.h>
using namespace std;
int n, m, u[5000005], v[5000005], w[5000005], fa[5000005];
long long ans;
int find(int x) {
return fa[x] == x ? x : fa[x] = find(fa[x]);
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) {
scanf("%d%d", u + i, v + i);
}
for (int i = 1; i <= n; i++) {
scanf("%d", w + i);
fa[i] = i;
}
for (int i = 1; i <= m; i++) {
int fu = find(u[i]);
int fv = find(v[i]);
fa[fv] = fu;
w[fu] = max(w[fu], w[fv]);
}
for (int i = 1; i <= n; i++) {
if (fa[i] == i) {
ans += w[i];
}
}
printf("%lld\n", ans);
return 0;
}