結果
| 問題 |
No.2377 SUM AND XOR on Tree
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-07-07 22:27:14 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 713 ms / 4,000 ms |
| コード長 | 1,379 bytes |
| コンパイル時間 | 2,640 ms |
| コンパイル使用メモリ | 223,788 KB |
| 最終ジャッジ日時 | 2025-02-15 07:47:32 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 33 |
ソースコード
#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;
}