結果

問題 No.2377 SUM AND XOR on Tree
ユーザー KKT89KKT89
提出日時 2023-07-07 22:27:14
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 726 ms / 4,000 ms
コード長 1,379 bytes
コンパイル時間 2,851 ms
コンパイル使用メモリ 219,248 KB
実行使用メモリ 22,184 KB
最終ジャッジ日時 2023-09-28 23:53:01
合計ジャッジ時間 15,901 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 670 ms
11,476 KB
testcase_09 AC 726 ms
11,412 KB
testcase_10 AC 705 ms
11,552 KB
testcase_11 AC 711 ms
11,604 KB
testcase_12 AC 708 ms
11,456 KB
testcase_13 AC 717 ms
11,300 KB
testcase_14 AC 660 ms
11,256 KB
testcase_15 AC 681 ms
11,452 KB
testcase_16 AC 653 ms
11,208 KB
testcase_17 AC 660 ms
11,332 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 686 ms
11,412 KB
testcase_24 AC 653 ms
11,460 KB
testcase_25 AC 680 ms
11,600 KB
testcase_26 AC 257 ms
22,184 KB
testcase_27 AC 261 ms
22,156 KB
testcase_28 AC 260 ms
22,132 KB
testcase_29 AC 174 ms
11,692 KB
testcase_30 AC 174 ms
11,884 KB
testcase_31 AC 180 ms
11,780 KB
testcase_32 AC 197 ms
11,304 KB
testcase_33 AC 190 ms
11,412 KB
testcase_34 AC 193 ms
11,228 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}

constexpr ll mod = 998244353;

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n; cin >> n;
    vector<vector<int>> g(n);
    for (int i = 0; i < n - 1; ++i) {
        int x,y; cin >> x >> y;
        x--; y--;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    vector<int> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    ll res = 0;
    for (int i = 0; i < 30; ++i) {
        vector<vector<ll>> dp(2, vector<ll>(n));
        auto dfs = [&](auto dfs, int s, int p) -> void {
            vector<ll> d(2);
            d[0] = 1;
            for (int t : g[s]) {
                if (t == p) continue;
                dfs(dfs, t, s);
                ll d0 = d[0], d1 = d[1];
                d[0] = (d0*dp[0][t] + d1*dp[1][t] + d0*dp[1][t]) % mod;
                d[1] = (d1*dp[0][t] + d0*dp[1][t] + d1*dp[1][t]) % mod;
            }
            int u = 0;
            if ((1<<i)&a[s]) u = 1;
            dp[0][s] = d[u];
            dp[1][s] = d[1-u];
        };
        dfs(dfs, 0, -1);
        res += (1LL<<i)*dp[1][0]%mod;
    }
    cout << res%mod << endl;
}
0