This documentation is automatically generated by online-judge-tools/verification-helper
View the Project on GitHub kmyk/competitive-programming-library
#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_12_A" #include "../graph/kruskal.hpp" #include "../utils/macros.hpp" #include <cstdio> #include <vector> using namespace std; int main() { // input int n; scanf("%d", &n); vector<tuple<int, int, long long> > edges; REP (x, n) { REP (y, n) { long long a; scanf("%lld", &a); if (a != -1) { edges.emplace_back(x, y, a); } } } // solve vector<int> mst = compute_minimum_spanning_tree(n, edges); long long answer = 0; for (int i : mst) { answer += get<2>(edges[i]); } // output printf("%lld\n", answer); return 0; }
#line 1 "graph/kruskal.aoj.test.cpp" #define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_12_A" #line 2 "data_structure/union_find_tree.hpp" #include <vector> /** * @brief Union-Find Tree * @docs data_structure/union_find_tree.md * @note union-by-size + path-compression */ struct union_find_tree { std::vector<int> data; union_find_tree() = default; explicit union_find_tree(std::size_t n) : data(n, -1) {} bool is_root(int i) { return data[i] < 0; } int find_root(int i) { return is_root(i) ? i : (data[i] = find_root(data[i])); } int tree_size(int i) { return - data[find_root(i)]; } int unite_trees(int i, int j) { i = find_root(i); j = find_root(j); if (i != j) { if (tree_size(i) < tree_size(j)) std::swap(i, j); data[i] += data[j]; data[j] = i; } return i; } bool is_same(int i, int j) { return find_root(i) == find_root(j); } }; #line 2 "utils/macros.hpp" #define REP(i, n) for (int i = 0; (i) < (int)(n); ++ (i)) #define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++ (i)) #define REP_R(i, n) for (int i = (int)(n) - 1; (i) >= 0; -- (i)) #define REP3R(i, m, n) for (int i = (int)(n) - 1; (i) >= (int)(m); -- (i)) #define ALL(x) std::begin(x), std::end(x) #line 4 "graph/kruskal.hpp" #include <algorithm> #include <numeric> #include <tuple> #line 8 "graph/kruskal.hpp" /** * @brief minimum spanning tree / 最小全域木 (Kruskal's method) * @note $O(E \log E)$ * @note it becomes a forest if the given graph is not connected * @return a list of indices of edges */ template <typename T> std::vector<int> compute_minimum_spanning_tree(int n, std::vector<std::tuple<int, int, T> > edges) { std::vector<int> order(edges.size()); std::iota(ALL(order), 0); std::sort(ALL(order), [&](int i, int j) { return std::make_pair(std::get<2>(edges[i]), i) < std::make_pair(std::get<2>(edges[j]), j); }); std::vector<int> tree; union_find_tree uft(n); for (int i : order) { int x = std::get<0>(edges[i]); int y = std::get<1>(edges[i]); if (not uft.is_same(x, y)) { uft.unite_trees(x, y); tree.push_back(i); } } return tree; } #line 3 "graph/kruskal.aoj.test.cpp" #line 5 "graph/kruskal.aoj.test.cpp" #include <cstdio> #line 7 "graph/kruskal.aoj.test.cpp" using namespace std; int main() { // input int n; scanf("%d", &n); vector<tuple<int, int, long long> > edges; REP (x, n) { REP (y, n) { long long a; scanf("%lld", &a); if (a != -1) { edges.emplace_back(x, y, a); } } } // solve vector<int> mst = compute_minimum_spanning_tree(n, edges); long long answer = 0; for (int i : mst) { answer += get<2>(edges[i]); } // output printf("%lld\n", answer); return 0; }