結果

問題 No.2642 Don't cut line!
ユーザー t98slidert98slider
提出日時 2024-03-11 00:18:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 50 ms / 4,000 ms
コード長 2,243 bytes
コンパイル時間 2,510 ms
コンパイル使用メモリ 212,940 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-03-11 00:18:37
合計ジャッジ時間 7,202 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 46 ms
6,676 KB
testcase_02 AC 46 ms
6,676 KB
testcase_03 AC 46 ms
6,676 KB
testcase_04 AC 47 ms
6,676 KB
testcase_05 AC 46 ms
6,676 KB
testcase_06 AC 48 ms
6,676 KB
testcase_07 AC 48 ms
6,676 KB
testcase_08 AC 47 ms
6,676 KB
testcase_09 AC 46 ms
6,676 KB
testcase_10 AC 48 ms
6,676 KB
testcase_11 AC 46 ms
6,676 KB
testcase_12 AC 48 ms
6,676 KB
testcase_13 AC 46 ms
6,676 KB
testcase_14 AC 48 ms
6,676 KB
testcase_15 AC 48 ms
6,676 KB
testcase_16 AC 48 ms
6,676 KB
testcase_17 AC 34 ms
6,676 KB
testcase_18 AC 37 ms
6,676 KB
testcase_19 AC 28 ms
6,676 KB
testcase_20 AC 36 ms
6,676 KB
testcase_21 AC 42 ms
6,676 KB
testcase_22 AC 36 ms
6,676 KB
testcase_23 AC 50 ms
6,676 KB
testcase_24 AC 28 ms
6,676 KB
testcase_25 AC 31 ms
6,676 KB
testcase_26 AC 45 ms
6,676 KB
testcase_27 AC 33 ms
6,676 KB
testcase_28 AC 46 ms
6,676 KB
testcase_29 AC 41 ms
6,676 KB
testcase_30 AC 34 ms
6,676 KB
testcase_31 AC 39 ms
6,676 KB
testcase_32 AC 37 ms
6,676 KB
testcase_33 AC 2 ms
6,676 KB
testcase_34 AC 1 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 N, K, C, c = 0;
    cin >> N >> K >> C;
    vector<T> edge(K);
    for(auto &&[u, v, w, p] : edge){
        cin >> u >> v >> w >> p;
        u--, v--;
    }
    sort(edge.begin(), edge.end(), [&](T lhs, T rhs){return get<2>(lhs) < get<2>(rhs);});
    Kruskal_dsu<int> uf(N);
    int ans = 0;
    for(auto &&[u, v, w, p] : edge){
        if(uf.merge(u, v, w)) c += w;
    }
    if(c > C || uf.size() != 1){
        cout << -1 << '\n';
        return 0;
    }
    for(auto &&[u, v, w, p] : edge){
        if(c - uf.max_edge(u, v) + w <= C) ans = max(ans, p);
    }
    cout << ans << '\n';
}
0