結果
問題 | No.2377 SUM AND XOR on Tree |
ユーザー |
![]() |
提出日時 | 2023-07-07 23:00:24 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 370 ms / 4,000 ms |
コード長 | 1,619 bytes |
コンパイル時間 | 4,097 ms |
コンパイル使用メモリ | 254,608 KB |
最終ジャッジ日時 | 2025-02-15 08:13:30 |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 33 |
ソースコード
#include<bits/stdc++.h> #include<atcoder/all> using namespace std; using namespace atcoder; typedef modint998244353 mint; typedef long long ll; #define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++) #define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--) template <typename T> bool chmin(T &a, const T &b) { if (a <= b) return false; a = b; return true; } template <typename T> bool chmax(T &a, const T &b) { if (a >= b) return false; a = b; return true; } int main(){ int n; cin >> n; vector ikeru(n, vector<int>(0)); rep(i,0,n-1){ int u, v; cin >> u >> v; u--; v--; ikeru[u].push_back(v); ikeru[v].push_back(u); } mint ans = 0; vector<int> a(n); rep(i,0,n)cin>>a[i]; rep(num,0,30){ vector<int> b(n); rep(i,0,n){ if (a[i]>>num&1) b[i] = 1; } vector<int> mada = {~0, 0}; vector<int> tansaku(n); tansaku[0] = 1; vector<mint> dp0(n); vector<mint> dp1(n); while(!mada.empty()){ int i = mada.back(); mada.pop_back(); if (i >= 0){ for(int j: ikeru[i]){ if (tansaku[j] == 0){ tansaku[j] = 1; mada.push_back(~j); mada.push_back(j); } } }else{ i = ~i; mint tmp0 = 0; mint tmp1 = 0; if (b[i] == 0) tmp0++; else tmp1++; for (int j: ikeru[i]){ if (tansaku[j] == 2){ mint ntmp0 = tmp0 * dp1[j] + tmp1 * dp1[j] + tmp0 * dp0[j]; mint ntmp1 = tmp1 * dp1[j] + tmp0 * dp1[j] + tmp1 * dp0[j]; tmp0 = ntmp0; tmp1 = ntmp1; } } dp0[i] = tmp0; dp1[i] = tmp1; tansaku[i] = 2; } } ans += mint(2).pow(num) * dp1[0]; } cout << ans.val() << endl; }