結果

問題 No.2642 Don't cut line!
ユーザー t98slidert98slider
提出日時 2024-03-11 00:25:32
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,385 bytes
コンパイル時間 2,541 ms
コンパイル使用メモリ 215,288 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-03-11 00:25:41
合計ジャッジ時間 6,939 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 51 ms
6,676 KB
testcase_02 AC 52 ms
6,676 KB
testcase_03 AC 52 ms
6,676 KB
testcase_04 AC 52 ms
6,676 KB
testcase_05 AC 52 ms
6,676 KB
testcase_06 AC 49 ms
6,676 KB
testcase_07 AC 48 ms
6,676 KB
testcase_08 AC 47 ms
6,676 KB
testcase_09 AC 60 ms
6,676 KB
testcase_10 AC 48 ms
6,676 KB
testcase_11 AC 47 ms
6,676 KB
testcase_12 AC 49 ms
6,676 KB
testcase_13 AC 47 ms
6,676 KB
testcase_14 AC 48 ms
6,676 KB
testcase_15 AC 48 ms
6,676 KB
testcase_16 AC 49 ms
6,676 KB
testcase_17 AC 40 ms
6,676 KB
testcase_18 AC 43 ms
6,676 KB
testcase_19 AC 33 ms
6,676 KB
testcase_20 AC 37 ms
6,676 KB
testcase_21 RE -
testcase_22 AC 37 ms
6,676 KB
testcase_23 AC 49 ms
6,676 KB
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 AC 32 ms
6,676 KB
testcase_28 AC 46 ms
6,676 KB
testcase_29 RE -
testcase_30 RE -
testcase_31 AC 40 ms
6,676 KB
testcase_32 RE -
testcase_33 AC 2 ms
6,676 KB
testcase_34 AC 2 ms
6,676 KB
testcase_35 AC 2 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

template <class T> struct Kruskal_dsu {
    public:
    Kruskal_dsu() : _n(0) {}
    Kruskal_dsu(int n) : _n(n), num_component(n), parent_or_size(n, -1), 
                         dat(n, std::numeric_limits<T>::max()) {}

    bool merge(int u, int v, T w) {
        assert(0 <= u && u < _n);
        assert(0 <= v && v < _n);
        int x = leader(u), y = leader(v);
        if (x == y) return false;
        if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y);
        parent_or_size[x] += parent_or_size[y];
        parent_or_size[y] = x;
        dat[y] = w;
        num_component--;
        return true;
    }

    bool same(int u, int v) {
        assert(0 <= u && u < _n);
        assert(0 <= v && v < _n);
        return leader(v) == leader(u);
    }

    int leader(int v) {
        assert(0 <= v && v < _n);
        while(parent_or_size[v] >= 0) v = parent_or_size[v];
        return v;
    }

    int size() {
        return num_component;
    }

    int size(int v) {
        assert(0 <= v && v < _n);
        return -parent_or_size[leader(v)];
    }

    T max_edge(int u, int v){
        T ans = 0;
        while(u != v){
            if (dat[u] > dat[v]) std::swap(u, v);
            ans = dat[u], u = parent_or_size[u];
            if(u < 0) return -1;
        }
        return ans;
    }

    private:
    int _n, num_component;
    // root node: -1 * component size

    // otherwise: parent

    std::vector<int> parent_or_size;
    std::vector<T> dat;
};
using T = tuple<int,int,int,int>;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll C, c = 0;
    int N, K, ans = 0, id = 0;
    cin >> N >> K >> C;
    vector<T> edge(K);
    for(auto &&[u, v, w, p] : edge){
        cin >> u >> v >> w >> p;
        u--, v--;
        swap(w, u);
    }
    sort(edge.begin(), edge.end());
    Kruskal_dsu<int> uf(N);
    vector<bool> used(N);
    for(auto &&[w, v, u, p] : edge){
        if(uf.merge(u, v, w)){
            used[id] = true;
            c += w;
            ans = max(ans, p);
        }
        id++;
    }
    if(c > C || uf.size() != 1){
        cout << -1 << '\n';
        return 0;
    }
    id = 0;
    for(auto &&[w, v, u, p] : edge){
        if(used[id++]) continue;
        if(c - uf.max_edge(u, v) + w <= C) ans = max(ans, p);
    }
    cout << ans << '\n';
}
0