結果

問題 No.2377 SUM AND XOR on Tree
ユーザー 👑 hitonanodehitonanode
提出日時 2023-07-14 08:59:43
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 366 ms / 4,000 ms
コード長 1,166 bytes
コンパイル時間 1,093 ms
コンパイル使用メモリ 98,244 KB
実行使用メモリ 12,564 KB
最終ジャッジ日時 2023-10-13 21:41:16
合計ジャッジ時間 7,613 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 1 ms
4,352 KB
testcase_08 AC 360 ms
9,072 KB
testcase_09 AC 366 ms
9,160 KB
testcase_10 AC 331 ms
9,136 KB
testcase_11 AC 315 ms
9,136 KB
testcase_12 AC 349 ms
9,020 KB
testcase_13 AC 221 ms
9,064 KB
testcase_14 AC 208 ms
8,864 KB
testcase_15 AC 231 ms
8,872 KB
testcase_16 AC 205 ms
8,760 KB
testcase_17 AC 222 ms
8,768 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 1 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 2 ms
4,352 KB
testcase_23 AC 228 ms
9,072 KB
testcase_24 AC 211 ms
8,956 KB
testcase_25 AC 212 ms
9,068 KB
testcase_26 AC 94 ms
12,564 KB
testcase_27 AC 92 ms
12,436 KB
testcase_28 AC 93 ms
12,500 KB
testcase_29 AC 73 ms
9,496 KB
testcase_30 AC 74 ms
9,628 KB
testcase_31 AC 73 ms
9,456 KB
testcase_32 AC 79 ms
8,760 KB
testcase_33 AC 80 ms
8,760 KB
testcase_34 AC 79 ms
8,868 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
using namespace std;

#include <atcoder/modint>
using mint = atcoder::modint998244353;

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);

    int N;
    cin >> N;
    vector<vector<int>> to(N);
    for (int e = 1; e < N; ++e) {
        int a, b;
        cin >> a >> b;
        --a, --b;
        to.at(a).push_back(b);
        to.at(b).push_back(a);
    }
    vector<int> A(N);
    for (auto &x : A) cin >> x;

    mint ret = 0;
    for (int d = 29; d >= 0; --d) {
        auto rec = [&](auto &&self, int now, int prv) -> pair<mint, mint> {
            mint dp0(0), dp1(0);

            if (A.at(now) & (1 << d)) dp1 = 1;
            else dp0 = 1;

            for (int nxt : to.at(now)) {
                if (nxt == prv) continue;
                const auto [ch0, ch1] = self(self, nxt, now);
                mint dp0nxt = dp0 * (ch0 + ch1) + dp1 * ch1;
                mint dp1nxt = dp0 * ch1 + dp1 * (ch0 + ch1);
                dp0 = dp0nxt;
                dp1 = dp1nxt;
            }
            return {dp0, dp1};
        };
        ret = ret * 2 + rec(rec, 0, -1).second;
    }
    cout << ret.val() << '\n';
}
0