結果

問題 No.2377 SUM AND XOR on Tree
ユーザー poyonpoyon
提出日時 2023-06-13 00:41:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 982 ms / 4,000 ms
コード長 2,685 bytes
コンパイル時間 7,628 ms
コンパイル使用メモリ 319,220 KB
実行使用メモリ 32,804 KB
最終ジャッジ日時 2023-09-21 05:19:32
合計ジャッジ時間 25,764 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,384 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 2 ms
4,384 KB
testcase_08 AC 981 ms
19,008 KB
testcase_09 AC 982 ms
19,008 KB
testcase_10 AC 977 ms
19,072 KB
testcase_11 AC 944 ms
18,976 KB
testcase_12 AC 974 ms
19,060 KB
testcase_13 AC 965 ms
18,808 KB
testcase_14 AC 891 ms
18,192 KB
testcase_15 AC 959 ms
18,844 KB
testcase_16 AC 936 ms
18,468 KB
testcase_17 AC 959 ms
18,752 KB
testcase_18 AC 3 ms
4,384 KB
testcase_19 AC 2 ms
4,384 KB
testcase_20 AC 2 ms
4,384 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,384 KB
testcase_23 AC 933 ms
18,936 KB
testcase_24 AC 957 ms
19,104 KB
testcase_25 AC 980 ms
19,080 KB
testcase_26 AC 429 ms
32,728 KB
testcase_27 AC 428 ms
32,804 KB
testcase_28 AC 430 ms
32,724 KB
testcase_29 AC 326 ms
19,232 KB
testcase_30 AC 330 ms
19,220 KB
testcase_31 AC 328 ms
19,192 KB
testcase_32 AC 406 ms
18,812 KB
testcase_33 AC 412 ms
18,796 KB
testcase_34 AC 413 ms
18,788 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// Validate Input by testlib.h

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

#if __has_include("testlib.h")
    #include "testlib.h"
#else
    #include "/opt/testlib/testlib.h"
#endif

#include <atcoder/modint>
#include <atcoder/dsu>
using namespace atcoder;

using mint = modint998244353;

// clang-format on
int main(int argc, char* argv[]) {
    // -------------------- Validate Input --------------------
    // Ref: https://github.com/MikeMirzayanov/testlib/blob/master/validators/undirected-tree-validator.cpp
    registerValidation(argc, argv);

    int N = inf.readInt(2, (int)1e5, "N");
    inf.readChar('\n');

    dsu uf(N);
    set<pair<int, int>> edges;
    vector<vector<int>> G(N);
    for (int i = 0; i < N - 1; i++) {
        int u = inf.readInt(1, N, "u_i");
        inf.readSpace();
        int v = inf.readInt(1, N, "v_i");
        inf.readChar('\n');

        ensuref(u != v, "Tree can't contain loops");
        ensuref(edges.count(make_pair(u, v)) == 0, "Tree can't contain multiple edges between a pair of vertices");

        edges.insert(make_pair(u, v));
        edges.insert(make_pair(v, u));

        ensuref(not uf.same(u - 1, v - 1), "Tree can't contain cycles");
        uf.merge(u - 1, v - 1);

        G[u - 1].push_back(v - 1);
        G[v - 1].push_back(u - 1);
    }
    ensuref(uf.size(0) == N, "Tree must be a connected graph");

    vector<int> A(N);
    const int MAX_A = (1 << 30) - 1;
    for (int i = 0; i < N; i++) {
        A[i] = inf.readInt(0, MAX_A, "A_i");
        if (i < N - 1) {
            inf.readSpace();
        } else {
            inf.readChar('\n');
        }
    }

    inf.readEof();

    // -------------------- Solve --------------------
    mint ans = 0;
    for (int b = 0; b < 30; b++) {
        auto dfs = [&](auto&& self, int u, int p) -> vector<mint> {
            vector<mint> dp(2);
            dp[(A[u] >> b) & 1]++;
            for (int v : G[u]) {
                if (v == p) { continue; }
                auto dp_v = self(self, v, u);

                vector<mint> dp_new(2);

                // 辺 (u, v) を削除する
                dp_new[0] += dp[0] * dp_v[1];
                dp_new[1] += dp[1] * dp_v[1];

                // 辺 (u, v) を削除しない(繋ぐ)
                dp_new[0] += dp[0] * dp_v[0];
                dp_new[1] += dp[0] * dp_v[1];
                dp_new[1] += dp[1] * dp_v[0];
                dp_new[0] += dp[1] * dp_v[1];

                swap(dp, dp_new);
            }
            return dp;
        };
        auto dp = dfs(dfs, 0, -1);
        ans += (1 << b) * dp[1];
    }
    cout << ans.val() << '\n';

    return 0;
}
0