From a9ea25dbd1465604dc3a33cb056eb8c6bc7c4cc4 Mon Sep 17 00:00:00 2001 From: Ren Baoshuo Date: Sat, 2 Oct 2021 09:14:08 +0800 Subject: [PATCH] =?UTF-8?q?H1004.=20=E3=80=90=E6=A8=A1=E6=9D=BF=E3=80=91?= =?UTF-8?q?=E6=9C=80=E5=B0=8F=E7=94=9F=E6=88=90=E6=A0=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://hydro.ac/record/6157b1eead4d340581846fba --- Hydro/H1004/H1004.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 Hydro/H1004/H1004.cpp diff --git a/Hydro/H1004/H1004.cpp b/Hydro/H1004/H1004.cpp new file mode 100644 index 00000000..fca73220 --- /dev/null +++ b/Hydro/H1004/H1004.cpp @@ -0,0 +1,41 @@ +#include + +using namespace std; + +int n, m, fa[100005], cnt; +long long res; + +struct node { + int u, v; + long long w; + + bool operator<(const node x) const { + return w < x.w; + } +} g[200005]; + +int find(int x) { + return fa[x] = fa[x] != x ? find(fa[x]) : fa[x]; +} + +int main() { + cin >> n >> m; + for (int i = 0; i < m; i++) { + cin >> g[i].u >> g[i].v >> g[i].w; + } + sort(g, g + m); + for (int i = 1; i <= n; i++) { + fa[i] = i; + } + for (int i = 0; i < m; i++) { + g[i].u = find(g[i].u); + g[i].v = find(g[i].v); + if (g[i].u != g[i].v) { + fa[g[i].u] = g[i].v; + res += g[i].w; + cnt++; + } + } + cout << res << endl; + return 0; +}