-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLuogu_P_1546.cpp
37 lines (37 loc) · 931 Bytes
/
Luogu_P_1546.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <bits/stdc++.h>
using namespace std;
using ui = unsigned int;
struct edge {
ui u, v, w;
bool friend operator<(const edge a, const edge b) { return a.w < b.w; }
};
inline ui find(vector<ui>& fa, const ui x) {
return fa[x] == x ? x : fa[x] = find(fa, fa[x]);
}
inline void link(vector<ui>& fa, const ui x, const ui y) {
fa[find(fa, x)] = find(fa, y);
}
int main(void) {
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
ui n;
cin >> n;
vector<edge> e;
for (ui i = 0; i < n; i++)
for (ui j = 0; j < n; j++) {
ui x;
cin >> x;
if (j < i) e.push_back({i, j, x});
}
sort(e.begin(), e.end());
ui ans = 0, tot = 1;
vector<ui> fa(n);
for (ui i = 0; i < n; i++) fa[i] = i;
for (vector<edge>::reference i : e)
if (find(fa, i.u) != find(fa, i.v)) {
link(fa, i.u, i.v);
ans += i.w, tot++;
if (tot >= n) break;
}
cout << ans;
return 0;
}