0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-23 20:28:48 +00:00

T203629 [$€₱2021 普及组] 赚钱

R61428370
This commit is contained in:
Baoshuo Ren 2021-10-31 16:47:06 +08:00 committed by Baoshuo Ren
parent f990dedacb
commit 05364a3261
Signed by: baoshuo
GPG Key ID: 70F90A673FB1AB68

34
Luogu/T203629/T203629.cpp Normal file
View File

@ -0,0 +1,34 @@
#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;
}