From 05364a326110d1c7e8b56e39cd257dc5a8086ac4 Mon Sep 17 00:00:00 2001 From: Ren Baoshuo Date: Sun, 31 Oct 2021 16:47:06 +0800 Subject: [PATCH] =?UTF-8?q?T203629=20[$=E2=82=AC=E2=82=B12021=20=E6=99=AE?= =?UTF-8?q?=E5=8F=8A=E7=BB=84]=20=E8=B5=9A=E9=92=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit R61428370 --- Luogu/T203629/T203629.cpp | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Luogu/T203629/T203629.cpp diff --git a/Luogu/T203629/T203629.cpp b/Luogu/T203629/T203629.cpp new file mode 100644 index 00000000..c38b76c9 --- /dev/null +++ b/Luogu/T203629/T203629.cpp @@ -0,0 +1,34 @@ +#include + +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; +}