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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
| #include <inttypes.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h>
typedef int32_t int32; typedef size_t usize;
typedef struct { int32* father; } UnionFind;
typedef struct { int32 a; int32 b; int32 weight; } Edge;
int edge_cmp(const void* a, const void* b); void read_data(usize* node_cnt, usize* edge_cnt, Edge** const edges); bool kruskal(const usize node_cnt, const usize edge_cnt, Edge* const edges, int32* const result); UnionFind* create_union_find(const usize size); void free_union_find(UnionFind* const self); int32 find_root(UnionFind* const self, const int32 a); bool connected(UnionFind* const self, const int32 a, const int32 b); void merge(UnionFind* const self, const int32 a, const int32 b);
int main() { usize node_cnt = 0; usize edge_cnt = 0; Edge* edges = NULL;
read_data(&node_cnt, &edge_cnt, &edges);
int32 result = -1; const bool succeeded = kruskal(node_cnt, edge_cnt, edges, &result);
if (succeeded) { printf("%" PRId32 "\n", result); } else { printf("Not connected.\n"); }
free(edges);
return 0; }
int edge_cmp(const void* a, const void* b) { const Edge* edge_a = (const Edge*) a; const Edge* edge_b = (const Edge*) b; if (edge_a->weight < edge_b->weight) { return -1; } else if (edge_a->weight > edge_b->weight) { return 1; } else { return 0; } }
void read_data(usize* node_cnt, usize* edge_cnt, Edge** const edges) { const int input_res = scanf("%zu%zu", node_cnt, edge_cnt); if (input_res != 2) { exit(1); }
*edges = (Edge*) malloc(*edge_cnt * sizeof(Edge)); if (*edges == NULL) { exit(1); }
for (usize i = 0; i < *edge_cnt; i++) { int32 a, b, weight; const int input_res = scanf("%" PRId32 "%" PRId32 "%" PRId32, &a, &b, &weight); if (input_res != 3) { exit(1); } (*edges)[i] = (Edge){ .a = a - 1, .b = b - 1, .weight = weight, }; } }
bool kruskal(const usize node_cnt, const usize edge_cnt, Edge* const edges, int32* const result) { qsort(edges, edge_cnt, sizeof(Edge), edge_cmp);
UnionFind* const set = create_union_find(node_cnt);
int32 total = 0; int32 handled = 0; for (usize i = 0; i < edge_cnt; i++) { const Edge* const edge = &edges[i]; if (connected(set, edge->a, edge->b)) { continue; } merge(set, edge->a, edge->b); total += edge->weight; handled++; if (handled == (int32) node_cnt - 1) { *result = total; free_union_find(set); return true; } }
free_union_find(set); return false; }
UnionFind* create_union_find(const usize size) { UnionFind* const self = (UnionFind* const) malloc(sizeof(UnionFind)); if (self == NULL) { exit(1); }
self->father = (int32*) malloc(size * sizeof(int32)); if (self->father == NULL) { exit(1); }
for (usize i = 0; i < size; i++) { self->father[i] = (int32) i; }
return self; }
void free_union_find(UnionFind* const self) { free(self->father); free(self); }
int32 find_root(UnionFind* const self, const int32 a) { if (self->father[a] == a) { return a; } const int32 temp = find_root(self, self->father[a]); self->father[a] = temp; return temp; }
bool connected(UnionFind* const self, const int32 a, const int32 b) { return find_root(self, a) == find_root(self, b); }
void merge(UnionFind* const self, const int32 a, const int32 b) { const int32 root_a = find_root(self, a); const int32 root_b = find_root(self, b); self->father[root_a] = root_b; }
|