結果

問題 No.2377 SUM AND XOR on Tree
ユーザー ぷらぷら
提出日時 2023-07-07 22:06:43
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 669 ms / 4,000 ms
コード長 1,487 bytes
コンパイル時間 2,117 ms
コンパイル使用メモリ 203,212 KB
実行使用メモリ 19,656 KB
最終ジャッジ日時 2023-09-28 23:24:55
合計ジャッジ時間 15,015 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 660 ms
10,176 KB
testcase_09 AC 654 ms
10,196 KB
testcase_10 AC 669 ms
10,224 KB
testcase_11 AC 663 ms
10,220 KB
testcase_12 AC 664 ms
10,184 KB
testcase_13 AC 646 ms
10,412 KB
testcase_14 AC 604 ms
9,888 KB
testcase_15 AC 653 ms
10,376 KB
testcase_16 AC 614 ms
9,832 KB
testcase_17 AC 624 ms
10,240 KB
testcase_18 AC 2 ms
4,380 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 643 ms
10,164 KB
testcase_24 AC 646 ms
10,156 KB
testcase_25 AC 657 ms
10,240 KB
testcase_26 AC 216 ms
19,348 KB
testcase_27 AC 216 ms
19,656 KB
testcase_28 AC 213 ms
19,428 KB
testcase_29 AC 160 ms
10,524 KB
testcase_30 AC 160 ms
10,484 KB
testcase_31 AC 160 ms
10,500 KB
testcase_32 AC 177 ms
10,164 KB
testcase_33 AC 179 ms
10,116 KB
testcase_34 AC 177 ms
10,152 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

constexpr int mod = 998244353;

void mpl(int &x,int y) {
    x += y;
    if(x >= mod) x -= mod;
}

int f[100100];
int dp[100100][2];

void dfs(int n,int p,vector<vector<int>>&ki) {
    dp[n][f[n]] = 1;
    for(int i:ki[n]) {
        if(i == p) {
            continue;
        }
        dfs(i,n,ki);
        vector<int>tmp(2);
        for(int j = 0; j < 2; j++) {
            for(int k = 0; k < 2; k++) {
                if(k == 0) {
                    mpl(tmp[j],1ll*dp[n][j]*dp[i][k]%mod);
                }
                else {
                    mpl(tmp[j^1],1ll*dp[n][j]*dp[i][k]%mod);
                    mpl(tmp[j],1ll*dp[n][j]*dp[i][k]%mod);
                }
            }
        }
        dp[n][0] = tmp[0];
        dp[n][1] = tmp[1];
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N;
    cin >> N;
    vector<vector<int>>ki(N);
    for(int i = 0; i < N-1; i++) {
        int u,v;
        cin >> u >> v;
        u--;
        v--;
        ki[u].push_back(v);
        ki[v].push_back(u);
    }
    vector<int>A(N);
    for(int i = 0; i < N; i++) {
        cin >> A[i];
    }
    int ans = 0;
    for(int i = 0; i < 30; i++) {
        for(int j = 0; j < N; j++) {
            f[j] = 1 & (A[j] >> i);
            for(int k = 0; k < 2; k++) {
                dp[j][k] = 0;
            }
        }
        dfs(0,-1,ki);
        mpl(ans,(1ll << i)*dp[0][1]%mod);
    }
    cout << ans << "\n";
}
0