rcpl

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

View the Project on GitHub ruthen71/rcpl

:heavy_check_mark: verify/aoj_grl/aoj_grl_2_a.test.cpp

Depends on

Code

#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_2_A"

#include <bits/stdc++.h>

#include "graph/kruskal.hpp"
#include "graph/read_graph.hpp"

int main() {
    int N, M;
    std::cin >> N >> M;
    auto G = read_graph<long long>(N, M, true, false, 0);
    auto [cost, es_set] = kruskal<long long>(G);
    std::cout << cost << '\n';
    return 0;
}
#line 1 "verify/aoj_grl/aoj_grl_2_a.test.cpp"
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_2_A"

#include <bits/stdc++.h>

#line 2 "graph/kruskal.hpp"

#line 2 "data_structure/unionfind.hpp"

struct UnionFind {
    int n;
    std::vector<int> parents;

    UnionFind() {}
    UnionFind(int n) : n(n), parents(n, -1) {}

    int leader(int x) { return parents[x] < 0 ? x : parents[x] = leader(parents[x]); }

    bool merge(int x, int y) {
        x = leader(x), y = leader(y);
        if (x == y) return false;
        if (parents[x] > parents[y]) std::swap(x, y);
        parents[x] += parents[y];
        parents[y] = x;
        return true;
    }

    bool same(int x, int y) { return leader(x) == leader(y); }

    int size(int x) { return -parents[leader(x)]; }

    std::vector<std::vector<int>> groups() {
        std::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]]++;
        }
        std::vector<std::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(std::remove_if(result.begin(), result.end(), [&](const std::vector<int>& v) { return v.empty(); }), result.end());
        return result;
    }

    void init(int n) { parents.assign(n, -1); }  // reset
};
#line 2 "graph/get_edges.hpp"

#line 2 "graph/graph_template.hpp"

#line 4 "graph/graph_template.hpp"
template <class T> struct Edge {
    int from, to;
    T cost;
    int id;

    Edge() = default;
    Edge(int from, int to, T cost = 1, int id = -1) : from(from), to(to), cost(cost), id(id) {}

    friend std::ostream &operator<<(std::ostream &os, const Edge<T> &e) {
        // output format: "{ id : from -> to, cost }"
        return os << "{ " << e.id << " : " << e.from << " -> " << e.to << ", " << e.cost << " }";
    }
};

template <class T> using Edges = std::vector<Edge<T>>;
template <class T> using Graph = std::vector<std::vector<Edge<T>>>;
#line 4 "graph/get_edges.hpp"

template <class T> Edges<T> get_edges(Graph<T> &G) {
    int N = (int)G.size(), M = 0;
    for (int i = 0; i < N; i++) {
        for (auto &&e : G[i]) {
            M = std::max(M, e.id + 1);
        }
    }
    Edges<T> es(M);
    for (int i = N - 1; i >= 0; i--) {
        for (auto &&e : G[i]) {
            es[e.id] = e;
        }
    }
    return es;
}
#line 6 "graph/kruskal.hpp"

template <class T> std::pair<T, Edges<T>> kruskal(Graph<T> &G) {
    auto es = get_edges<T>(G);
    std::sort(es.begin(), es.end(), [](Edge<T> a, Edge<T> b) { return a.cost < b.cost; });
    int N = (int)G.size();
    UnionFind uf(N);
    T ret = 0;
    Edges<T> es_ret;
    es_ret.reserve(N - 1);
    for (auto &&e : es) {
        if (!uf.same(e.from, e.to)) {
            ret += e.cost;
            uf.merge(e.from, e.to);
            es_ret.push_back(e);
        }
    }
    return {ret, es_ret};
}
#line 2 "graph/read_graph.hpp"

#line 4 "graph/read_graph.hpp"

template <class T> Graph<T> read_graph(const int n, const int m, const bool weight = false, const bool directed = false, const int offset = 1) {
    Graph<T> g(n);
    for (int i = 0; i < m; i++) {
        int a, b;
        std::cin >> a >> b;
        a -= offset, b -= offset;
        if (weight) {
            T c;
            std::cin >> c;
            if (!directed) g[b].push_back(Edge(b, a, c, i));
            g[a].push_back(Edge(a, b, c, i));
        } else {
            // c = 1
            if (!directed) g[b].push_back(Edge(b, a, T(1), i));
            g[a].push_back(Edge(a, b, T(1), i));
        }
    }
    return g;
}

template <class T> Graph<T> read_parent(const int n, const bool weight = false, const bool directed = false, const int offset = 1) {
    Graph<T> g(n);
    for (int i = 1; i < n; i++) {
        int p;
        std::cin >> p;
        p -= offset;
        if (weight) {
            T c;
            std::cin >> c;
            if (!directed) g[i].push_back(Edge(i, p, c, i - 1));
            g[p].push_back(Edge(p, i, c, i - 1));
        } else {
            // c = 1
            if (!directed) g[i].push_back(Edge(i, p, T(1), i - 1));
            g[p].push_back(Edge(p, i, T(1), i - 1));
        }
    }
    return g;
}

std::tuple<Graph<int>, std::vector<std::vector<int>>, std::vector<std::pair<int, int>>> read_grid(const int h, const int w, std::string rel = ".#") {
    std::vector<std::string> s(h);
    std::vector id(h, std::vector<int>(w, -1));
    std::vector<std::pair<int, int>> loc;
    int n = 0;
    for (int i = 0; i < h; i++) {
        std::cin >> s[i];
        for (int j = 0; j < w; j++) {
            if (s[i][j] == rel[1]) {
                id[i][j] = n++;
                loc.emplace_back(i, j);
            }
        }
    }
    int m = 0;
    Graph<int> g(n);
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            if (s[i][j] == rel[1]) {
                if (i + 1 < h and s[i + 1][j] == rel[1]) {
                    g[id[i][j]].push_back(Edge(id[i][j], id[i + 1][j], 1, m));
                    g[id[i + 1][j]].push_back(Edge(id[i + 1][j], id[i][j], 1, m++));
                }
                if (j + 1 < w and s[i][j + 1] == rel[1]) {
                    g[id[i][j]].push_back(Edge(id[i][j], id[i][j + 1], 1, m));
                    g[id[i][j + 1]].push_back(Edge(id[i][j + 1], id[i][j], 1, m++));
                }
            }
        }
    }
    return {g, id, loc};
}
#line 7 "verify/aoj_grl/aoj_grl_2_a.test.cpp"

int main() {
    int N, M;
    std::cin >> N >> M;
    auto G = read_graph<long long>(N, M, true, false, 0);
    auto [cost, es_set] = kruskal<long long>(G);
    std::cout << cost << '\n';
    return 0;
}
Back to top page