結果

問題 No.2377 SUM AND XOR on Tree
ユーザー KowerKoint2010KowerKoint2010
提出日時 2023-06-29 08:43:35
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 258 ms / 4,000 ms
コード長 1,653 bytes
コンパイル時間 2,182 ms
コンパイル使用メモリ 213,656 KB
実行使用メモリ 18,068 KB
最終ジャッジ日時 2023-09-21 05:19:54
合計ジャッジ時間 8,658 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 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 257 ms
16,312 KB
testcase_09 AC 258 ms
16,056 KB
testcase_10 AC 255 ms
16,156 KB
testcase_11 AC 251 ms
16,452 KB
testcase_12 AC 256 ms
16,156 KB
testcase_13 AC 252 ms
15,972 KB
testcase_14 AC 240 ms
15,728 KB
testcase_15 AC 248 ms
15,944 KB
testcase_16 AC 245 ms
15,724 KB
testcase_17 AC 251 ms
15,860 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 2 ms
4,376 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 224 ms
16,256 KB
testcase_24 AC 233 ms
16,236 KB
testcase_25 AC 258 ms
16,228 KB
testcase_26 AC 182 ms
17,800 KB
testcase_27 AC 183 ms
17,888 KB
testcase_28 AC 184 ms
18,068 KB
testcase_29 AC 162 ms
15,856 KB
testcase_30 AC 162 ms
15,824 KB
testcase_31 AC 163 ms
15,676 KB
testcase_32 AC 192 ms
15,956 KB
testcase_33 AC 185 ms
16,104 KB
testcase_34 AC 188 ms
15,980 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main() {
    constexpr ll mod = 998244353;
    int n; cin >> n;
    vector<vector<int>> tr(n);
    for(int i = 0; i < n-1; i++) {
        int u, v; cin >> u >> v; u--; v--;
        tr[u].push_back(v);
        tr[v].push_back(u);
    }
    vector<int> postorder;
    vector<int> seen(n);
    vector<vector<int>> child(n);
    {
        stack<int> stk;
        stk.push(~0);
        stk.push(0);
        while(!stk.empty()) {
            int u = stk.top(); stk.pop();
            if(u >= 0) {
                seen[u] = 1;
                for(int v : tr[u]) {
                    if(seen[v]) continue;
                    child[u].push_back(v);
                    stk.push(~v);
                    stk.push(v);
                }
            } else {
                u = ~u;
                postorder.push_back(u);
            }
        }
    }
    vector<int> a(n);
    for(int i = 0; i < n; i++) cin >> a[i];
    ll ans = 0;
    for(int k = 0; k < 30; k++) {
        // dp[i][j]:=頂点jを根をする部分木で、根を含む連結成分のxorがi
        vector<vector<ll>> dp(2, vector<ll>(n));
        for(int i = 0; i < n; i++) {
            int u = postorder[i];
            dp[a[u]>>k&1][u] = 1;
            for(int v : child[u]) {
                tie(dp[0][u], dp[1][u]) = make_tuple(
                    (dp[0][u]*dp[0][v] + dp[0][u]*dp[1][v] + dp[1][u]*dp[1][v]) % mod,
                    (dp[0][u]*dp[1][v] + dp[1][u]*dp[0][v] + dp[1][u]*dp[1][v]) % mod
                );
            }
        }
        ans += dp[1][0] << k;
    }
    cout << ans % mod << endl;
}
0