結果

問題 No.2377 SUM AND XOR on Tree
ユーザー SSRSSSRS
提出日時 2023-07-07 21:47:35
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 289 ms / 4,000 ms
コード長 1,207 bytes
コンパイル時間 1,782 ms
コンパイル使用メモリ 178,752 KB
実行使用メモリ 17,636 KB
最終ジャッジ日時 2023-09-28 22:51:37
合計ジャッジ時間 8,849 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 287 ms
16,124 KB
testcase_09 AC 284 ms
16,060 KB
testcase_10 AC 282 ms
16,248 KB
testcase_11 AC 284 ms
16,268 KB
testcase_12 AC 287 ms
16,120 KB
testcase_13 AC 280 ms
16,164 KB
testcase_14 AC 281 ms
15,596 KB
testcase_15 AC 279 ms
16,164 KB
testcase_16 AC 283 ms
15,916 KB
testcase_17 AC 277 ms
15,920 KB
testcase_18 AC 2 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,376 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 260 ms
16,244 KB
testcase_24 AC 258 ms
16,228 KB
testcase_25 AC 289 ms
16,248 KB
testcase_26 AC 181 ms
17,636 KB
testcase_27 AC 182 ms
17,588 KB
testcase_28 AC 180 ms
17,504 KB
testcase_29 AC 163 ms
15,704 KB
testcase_30 AC 166 ms
15,656 KB
testcase_31 AC 164 ms
15,708 KB
testcase_32 AC 180 ms
16,304 KB
testcase_33 AC 181 ms
16,108 KB
testcase_34 AC 180 ms
16,280 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const long long MOD = 998244353;
int main(){
  int N;
  cin >> N;
  vector<vector<int>> E(N);
  for (int i = 0; i < N - 1; i++){
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    E[u].push_back(v);
    E[v].push_back(u);
  }
  vector<int> p(N, -1);
  vector<vector<int>> c(N);
  vector<int> b;
  queue<int> Q;
  Q.push(0);
  while (!Q.empty()){
    int v = Q.front();
    Q.pop();
    b.push_back(v);
    for (int w : E[v]){
      if (w != p[v]){
        p[w] = v;
        c[v].push_back(w);
        Q.push(w);
      }
    }
  }
  reverse(b.begin(), b.end());
  vector<int> A(N);
  for (int i = 0; i < N; i++){
    cin >> A[i];
  }
  long long ans = 0;
  for (int i = 0; i < 30; i++){
    vector<vector<long long>> dp(2, vector<long long>(N, 0));
    for (int v : b){
      dp[A[v] >> i & 1][v] = 1;
      for (int w : c[v]){
        long long x = (dp[0][v] * dp[0][w] + dp[0][v] * dp[1][w] + dp[1][v] * dp[1][w]) % MOD;
        long long y = (dp[1][v] * dp[0][w] + dp[1][v] * dp[1][w] + dp[0][v] * dp[1][w]) % MOD;
        dp[0][v] = x;
        dp[1][v] = y;
      }
    }
    ans += (1 << i) * dp[1][0] % MOD;
    ans %= MOD;
  }
  cout << ans << endl;
}
0