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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| #include<cstdio> #include<algorithm> using namespace std; #define re register #define int long long const int MAXn = 5e3; const int MAXm = 2e5;
template <class T> inline void read(T &a) { register char c;while (c = getchar(), c < '0' || c > '9');register T x(c - '0');while (c = getchar(), c >= '0' && c <= '9') {x = (x << 1) + (x << 3) + (c ^ 48);}a = x; }
int n, m; int fa[MAXn + 10]; int anc(int x) { return fa[x] = fa[x] == x ? x : anc(fa[x]); } void Merge(int x, int y) { if (anc(x) != anc(y)) { fa[anc(x)] = y; } } bool SameAnc(int x, int y) { return anc(x) == anc(y); } void Init(int top) { for (re int i = 1; i <= top; ++i) { fa[i] = i; } }
struct Edge { int u, v, w; Edge():u(0), v(0), w(0){} Edge(int u_, int v_, int w_):u(u_), v(v_), w(w_){} inline bool operator<(Edge x) { return this->w < x.w; } }edge[MAXm + 10]; int Kruskal() { int ans = 0; sort(edge + 1, edge + 1 + m); Init(n); for (re int i = 1; i <= m; ++i) { if (!SameAnc(edge[i].u, edge[i].v)) { ans += edge[i].w; Merge(edge[i].u, edge[i].v); } } return ans; }
int ans; signed main() { read(n), read(m); for (re int i = 1, u, v, w; i <= m; ++i) { read(u), read(v), read(w); edge[i] = Edge(u, v, w); } ans = Kruskal(); for (re int i = 1; i < n; ++i) { if (!SameAnc(i, i + 1)) { printf("orz\n"); return 0; } } printf("%d\n", ans); }
|