Fu_L's Library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub Fu-L/cp-library

:heavy_check_mark: verify/aizu_online_judge/grl/minimum_spanning_tree.test.cpp

Depends on

Code

#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_2_A"
#include "../../../src/template/template.hpp"
#include "../../../src/graph/graph_template.hpp"
#include "../../../src/graph/kruskal.hpp"
int main(void) {
    int n, m;
    cin >> n >> m;
    Edges<int> e(m);
    rep(i, 0, m) {
        cin >> e[i].from >> e[i].to >> e[i].cost;
        e[i].idx = i;
    }
    cout << kruskal(n, e).first << '\n';
}
#line 1 "verify/aizu_online_judge/grl/minimum_spanning_tree.test.cpp"
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_2_A"
#line 2 "src/template/template.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<long long, long long>;
#define rep(i, a, b) for(long long i = (a); i < (b); ++i)
#define rrep(i, a, b) for(long long i = (a); i >= (b); --i)
constexpr long long inf = 4e18;
struct SetupIO {
    SetupIO() {
        ios::sync_with_stdio(0);
        cin.tie(0);
        cout << fixed << setprecision(30);
    }
} setup_io;
#line 3 "src/graph/graph_template.hpp"
template <typename T>
struct Edge {
    int from, to;
    T cost;
    int idx;
    Edge()
        : from(-1), to(-1), cost(-1), idx(-1) {}
    Edge(const int from, const int to, const T& cost = 1, const int idx = -1)
        : from(from), to(to), cost(cost), idx(idx) {}
    operator int() const {
        return to;
    }
};
template <typename T>
struct Graph {
    Graph(const int N)
        : n(N), es(0), g(N) {}
    int size() const {
        return n;
    }
    int edge_size() const {
        return es;
    }
    void add_edge(const int from, const int to, const T& cost = 1) {
        assert(0 <= from and from < n);
        assert(0 <= to and to < n);
        g[from].emplace_back(from, to, cost, es);
        g[to].emplace_back(to, from, cost, es++);
    }
    void add_directed_edge(const int from, const int to, const T& cost = 1) {
        assert(0 <= from and from < n);
        assert(0 <= to and to < n);
        g[from].emplace_back(from, to, cost, es++);
    }
    inline vector<Edge<T>>& operator[](const int& k) {
        assert(0 <= k and k < n);
        return g[k];
    }
    inline const vector<Edge<T>>& operator[](const int& k) const {
        assert(0 <= k and k < n);
        return g[k];
    }

   private:
    int n, es;
    vector<vector<Edge<T>>> g;
};
template <typename T>
using Edges = vector<Edge<T>>;
#line 3 "src/data_structure/union_find.hpp"
struct UnionFind {
    UnionFind(const int N)
        : n(N), data(N, -1) {}
    int merge(const int a, const int b) {
        assert(0 <= a and a < n);
        assert(0 <= b and b < n);
        int x = leader(a), y = leader(b);
        if(x == y) return x;
        if(-data[x] < -data[y]) swap(x, y);
        data[x] += data[y];
        data[y] = x;
        return x;
    }
    bool same(const int a, const int b) {
        assert(0 <= a and a < n);
        assert(0 <= b and b < n);
        return leader(a) == leader(b);
    }
    int leader(const int a) {
        assert(0 <= a and a < n);
        if(data[a] < 0) return a;
        return data[a] = leader(data[a]);
    }
    int size(const int a) {
        assert(0 <= a and a < n);
        return -data[leader(a)];
    }
    vector<vector<int>> groups() {
        vector<int> leader_buf(n), group_size(n);
        for(int i = 0; i < n; ++i) {
            leader_buf[i] = leader(i);
            ++group_size[leader_buf[i]];
        }
        vector<vector<int>> result(n);
        for(int i = 0; i < n; ++i) {
            result[i].reserve(group_size[i]);
        }
        for(int i = 0; i < n; ++i) {
            result[leader_buf[i]].push_back(i);
        }
        result.erase(remove_if(result.begin(), result.end(), [&](const vector<int>& v) { return v.empty(); }), result.end());
        return result;
    }

   private:
    int n;
    vector<int> data;
};
#line 5 "src/graph/kruskal.hpp"
template <typename T>
pair<T, Edges<T>> kruskal(const int n, Edges<T> es) {
    sort(es.begin(), es.end(), [&](const Edge<T>& a, const Edge<T>& b) { return a.cost < b.cost; });
    UnionFind uf(n);
    T cost = 0;
    Edges<T> res;
    res.reserve(n - 1);
    for(const Edge<T>& e : es) {
        if(uf.same(e.from, e.to)) continue;
        cost += e.cost;
        uf.merge(e.from, e.to);
        res.emplace_back(e);
    }
    return {cost, res};
}
#line 5 "verify/aizu_online_judge/grl/minimum_spanning_tree.test.cpp"
int main(void) {
    int n, m;
    cin >> n >> m;
    Edges<int> e(m);
    rep(i, 0, m) {
        cin >> e[i].from >> e[i].to >> e[i].cost;
        e[i].idx = i;
    }
    cout << kruskal(n, e).first << '\n';
}
Back to top page