結果

問題 No.1154 シュークリームゲーム(Hard)
ユーザー suisensuisen
提出日時 2023-06-05 15:23:01
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,693 bytes
コンパイル時間 1,003 ms
コンパイル使用メモリ 97,636 KB
実行使用メモリ 5,272 KB
最終ジャッジ日時 2023-08-28 09:34:38
合計ジャッジ時間 3,168 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 2 ms
4,376 KB
testcase_35 AC 2 ms
4,376 KB
testcase_36 AC 2 ms
4,376 KB
testcase_37 AC 3 ms
4,380 KB
testcase_38 AC 2 ms
4,376 KB
testcase_39 AC 1 ms
4,380 KB
testcase_40 AC 2 ms
4,376 KB
testcase_41 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <set>

#include <atcoder/dsu>

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int n;
    std::cin >> n;

    std::vector<std::pair<long long, bool>> sum(n);

    for (int i = 0; i < n; ++i) {
        std::cin >> sum[i].first;
        sum[i].second = true;
    }

    std::vector<std::vector<int>> g(n);
    for (int i = 0; i < n - 1; ++i) {
        int u, v;
        std::cin >> u >> v;
        --u, --v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    std::vector<int> p(n, -1);
    auto dfs = [&](auto dfs, int u) -> void {
        for (int v : g[u]) if (v != p[u]) {
            p[v] = u;
            dfs(dfs, v);
        }
    };
    dfs(dfs, 0);

    std::vector<long long> ans(n);

    auto compare = [&sum](int i, int j) {
        long long dij = sum[i].first - sum[j].first;
        return dij ? dij > 0 : i > j;
    };
    std::set<int, decltype(compare)> st { compare };

    for (int i = 1; i < n; ++i) {
        st.insert(i);
    }

    atcoder::dsu uf(n);

    std::vector<int> root(n);
    for (int i = 0; i < n; ++i) {
        root[i] = i;
    }

    while (not st.empty()) {
        const int u = [&] {
            auto it = st.begin();
            const int u = *it;
            st.erase(it);
            return u;
        }();
        const int v = root[uf.leader(p[u])];

        if (v) st.erase(v);

        if (sum[v].second) {
            sum[v].first -= sum[u].first;
        } else {
            sum[v].first += sum[u].first;
        }
        sum[v].second ^= sum[u].second;

        root[uf.merge(u, v)] = v;

        if (v) st.insert(v);
    }

    std::cout << sum[0].first << '\n';
}
0