結果

問題 No.1639 最小通信路
ユーザー kimiyukikimiyuki
提出日時 2021-08-06 22:04:49
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 30 ms / 2,000 ms
コード長 2,482 bytes
コンパイル時間 7,239 ms
コンパイル使用メモリ 417,124 KB
実行使用メモリ 5,620 KB
最終ジャッジ日時 2023-10-17 03:27:33
合計ジャッジ時間 8,836 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 8 ms
4,392 KB
testcase_03 AC 30 ms
5,620 KB
testcase_04 AC 30 ms
5,620 KB
testcase_05 AC 3 ms
4,348 KB
testcase_06 AC 4 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 6 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 3 ms
4,348 KB
testcase_11 AC 5 ms
4,348 KB
testcase_12 AC 3 ms
4,348 KB
testcase_13 AC 8 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 5 ms
4,348 KB
testcase_17 AC 6 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 3 ms
4,348 KB
testcase_20 AC 7 ms
4,348 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 3 ms
4,348 KB
testcase_23 AC 4 ms
4,348 KB
testcase_24 AC 3 ms
4,348 KB
testcase_25 AC 4 ms
4,348 KB
testcase_26 AC 3 ms
4,348 KB
testcase_27 AC 2 ms
4,348 KB
testcase_28 AC 2 ms
4,348 KB
testcase_29 AC 9 ms
4,348 KB
testcase_30 AC 15 ms
4,660 KB
testcase_31 AC 4 ms
4,348 KB
testcase_32 AC 11 ms
4,472 KB
testcase_33 AC 3 ms
4,348 KB
testcase_34 AC 10 ms
4,364 KB
testcase_35 AC 23 ms
5,196 KB
testcase_36 AC 10 ms
4,348 KB
testcase_37 AC 11 ms
4,528 KB
testcase_38 AC 2 ms
4,348 KB
testcase_39 AC 4 ms
4,348 KB
testcase_40 AC 2 ms
4,348 KB
testcase_41 AC 3 ms
4,348 KB
testcase_42 AC 14 ms
4,604 KB
testcase_43 AC 3 ms
4,348 KB
testcase_44 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.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)
using namespace std;
using namespace boost::multiprecision;

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); }
};

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;
}

cpp_int solve(int n, const std::vector<vector<cpp_int>> &c) {
    vector<tuple<int, int, cpp_int>> edges;
    REP (i, n) {
        REP (j, i) {
            edges.emplace_back(i, j, c[i][j]);
        }
    }
    auto mst = compute_minimum_spanning_tree(n, edges);
    return get<2>(edges[mst.back()]);
}

// generated by oj-template v4.8.0 (https://github.com/online-judge-tools/template-generator)
int main() {
    int n;
    std::cin >> n;
    vector<vector<cpp_int>> c(n, vector<cpp_int>(n));
    REP (i, n * (n - 1) / 2) {
        int a, b; cpp_int cc;
        cin >> a >> b >> cc;
        -- a;
        -- b;
        c[a][b] = cc;
        c[b][a] = cc;
    }
    auto ans = solve(n, c);
    std::cout << ans << '\n';
    return 0;
}
0