結果

問題 No.2642 Don't cut line!
ユーザー てんぷらてんぷら
提出日時 2024-02-19 22:15:14
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 157 ms / 4,000 ms
コード長 3,037 bytes
コンパイル時間 5,753 ms
コンパイル使用メモリ 319,320 KB
実行使用メモリ 35,328 KB
最終ジャッジ日時 2024-02-20 12:46:55
合計ジャッジ時間 10,209 ms
ジャッジサーバーID
(参考情報)
judge16 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 144 ms
35,072 KB
testcase_02 AC 143 ms
35,328 KB
testcase_03 AC 151 ms
34,048 KB
testcase_04 AC 141 ms
34,048 KB
testcase_05 AC 145 ms
34,560 KB
testcase_06 AC 55 ms
6,676 KB
testcase_07 AC 56 ms
6,676 KB
testcase_08 AC 56 ms
6,676 KB
testcase_09 AC 56 ms
6,676 KB
testcase_10 AC 56 ms
6,676 KB
testcase_11 AC 55 ms
6,676 KB
testcase_12 AC 57 ms
6,676 KB
testcase_13 AC 55 ms
6,676 KB
testcase_14 AC 57 ms
6,676 KB
testcase_15 AC 58 ms
6,676 KB
testcase_16 AC 80 ms
11,520 KB
testcase_17 AC 115 ms
30,336 KB
testcase_18 AC 129 ms
31,744 KB
testcase_19 AC 87 ms
24,704 KB
testcase_20 AC 88 ms
13,056 KB
testcase_21 AC 45 ms
6,676 KB
testcase_22 AC 55 ms
9,472 KB
testcase_23 AC 157 ms
34,048 KB
testcase_24 AC 62 ms
17,024 KB
testcase_25 AC 66 ms
15,616 KB
testcase_26 AC 67 ms
9,216 KB
testcase_27 AC 85 ms
23,680 KB
testcase_28 AC 140 ms
31,616 KB
testcase_29 AC 72 ms
11,136 KB
testcase_30 AC 67 ms
14,208 KB
testcase_31 AC 118 ms
27,136 KB
testcase_32 AC 71 ms
13,184 KB
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 <atcoder/all>
#include <bits/stdc++.h>
using ll = long long;
using ull = unsigned long long;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for(int i = (int)(m); i < (int)(n); i++)
using namespace std;
using namespace atcoder;
using mint = modint998244353;
const int inf = 1000000007;
const ll longinf = 1ll << 60;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n, m;
    cin >> n >> m;
    ll c;
    cin >> c;
    vector<int> l(m), r(m), w(m), p(m);
    rep(i, m) {
        cin >> l[i] >> r[i] >> w[i] >> p[i];
        --l[i];
        --r[i];
    }
    vector<int> ord(m);
    rep(i, m) ord[i] = i;
    sort(ord.begin(), ord.end(), [&](int x, int y) {
        return w[x] < w[y];
    });
    dsu uf(n);
    vector<vector<pair<int, int>>> g(n);
    vector<int> used(m);
    ll cost = 0;
    for(auto i : ord) {
        if(uf.same(l[i], r[i])) {
            continue;
        }
        used[i] = 1;
        uf.merge(l[i], r[i]);
        g[l[i]].push_back({r[i], w[i]});
        g[r[i]].push_back({l[i], w[i]});
        cost += w[i];
    }
    if(cost > c) {
        cout << -1 << endl;
        return 0;
    }
    vector<int> parent(n), rank(n);
    auto dfs = [&](auto &&self, int x, int p) -> void {
        parent[x] = p;
        for(auto to : g[x]) {
            if(to.first == p)
                continue;
            rank[to.first] = rank[x] + 1;
            self(self, to.first, x);
        }
    };
    dfs(dfs, 0, -1);
    vector nxt(n, vector<pair<int, int>>(20));
    rep(i, n) {
        nxt[i][0] = {-1, -1};
        for(auto to : g[i]) {
            if(to.first == parent[i]) {
                nxt[i][0] = to;
            }
        }
    }
    rep(b, 19) {
        rep(i, n) {
            if(nxt[i][b].first == -1) {
                nxt[i][b + 1] = nxt[i][b];
            } else {
                auto nnxt = nxt[nxt[i][b].first][b];
                nxt[i][b + 1] = {nnxt.first, max(nnxt.second, nxt[i][b].second)};
            }
        }
    }
    auto query = [&](int l, int r) -> int {
        int ma = 0;
        if(rank[l] < rank[r])
            swap(l, r);
        for(int b = 19; b >= 0; --b) {
            if(rank[l] - (1 << b) < rank[r])
                continue;
            ma = max(ma, nxt[l][b].second);
            l = nxt[l][b].first;
        }
        if(l == r) {
            return ma;
        }
        for(int b = 19; b >= 0; --b) {
            if(nxt[l][b].first == nxt[r][b].first)
                continue;
            ma = max(ma, nxt[l][b].second);
            ma = max(ma, nxt[r][b].second);
            l = nxt[l][b].first;
            r = nxt[r][b].first;
        }
        return max({ma, nxt[l][0].second, nxt[r][0].second});
    };
    int ans = 0;
    rep(i, m) {
        if(used[i]) {
            ans = max(ans, p[i]);
            continue;
        }
        int ret = query(l[i], r[i]);
        if(cost - ret + w[i] <= c) {
            ans = max(ans, p[i]);
        }
    }
    cout << ans << endl;
    return 0;
}
0