結果

問題 No.2377 SUM AND XOR on Tree
ユーザー poyonpoyon
提出日時 2023-06-12 21:18:27
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 945 ms / 4,000 ms
コード長 2,589 bytes
コンパイル時間 9,095 ms
コンパイル使用メモリ 318,328 KB
実行使用メモリ 32,824 KB
最終ジャッジ日時 2023-09-21 05:18:52
合計ジャッジ時間 24,687 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 935 ms
19,000 KB
testcase_09 AC 937 ms
18,872 KB
testcase_10 AC 932 ms
19,032 KB
testcase_11 AC 945 ms
19,068 KB
testcase_12 AC 938 ms
18,948 KB
testcase_13 AC 886 ms
19,172 KB
testcase_14 AC 852 ms
18,084 KB
testcase_15 AC 908 ms
18,680 KB
testcase_16 AC 905 ms
18,420 KB
testcase_17 AC 909 ms
18,684 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 891 ms
18,928 KB
testcase_24 AC 911 ms
18,928 KB
testcase_25 AC 934 ms
18,952 KB
testcase_26 AC 471 ms
32,652 KB
testcase_27 AC 470 ms
32,824 KB
testcase_28 AC 475 ms
32,680 KB
testcase_29 AC 336 ms
19,224 KB
testcase_30 AC 339 ms
19,160 KB
testcase_31 AC 331 ms
19,148 KB
testcase_32 AC 425 ms
18,764 KB
testcase_33 AC 432 ms
18,808 KB
testcase_34 AC 424 ms
18,768 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.readEoln();

    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.readEoln();

        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.readEoln();
        }
    }

    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