結果
問題 |
No.2377 SUM AND XOR on Tree
|
ユーザー |
![]() |
提出日時 | 2023-07-10 19:23:16 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 924 ms / 4,000 ms |
コード長 | 1,025 bytes |
コンパイル時間 | 4,236 ms |
コンパイル使用メモリ | 258,488 KB |
最終ジャッジ日時 | 2025-02-15 09:36:46 |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 33 |
ソースコード
#include <stdio.h> #include <atcoder/all> #include <bits/stdc++.h> using namespace std; using namespace atcoder; using mint = modint998244353; #define rep(i,n) for (int i = 0; i < (n); ++i) #define Inf32 1000000001 #define Inf64 4000000000000000001 int n; vector<vector<int>> E; vector<int> b; vector<vector<mint>> dp; void dfs(int cv,int pv){ vector<mint> tdp(2,0); tdp[b[cv]] = 1; rep(i,E[cv].size()){ int to = E[cv][i]; if(to==pv)continue; dfs(to,cv); vector<mint> ndp(2,0); rep(j,2){ rep(k,2){ ndp[j^k] += tdp[j] * dp[to][k]; if(k==1){ ndp[j] += tdp[j] * dp[to][k]; } } } swap(tdp,ndp); } dp[cv] = tdp; } int main(){ cin>>n; E.resize(n); rep(i,n-1){ int a,b; cin>>a>>b; a--,b--; E[a].push_back(b); E[b].push_back(a); } mint ans = 0; vector<int> a(n); rep(i,n)cin>>a[i]; b.resize(n); rep(i,30){ rep(j,n)b[j] = (a[j]>>i)&1; dp.assign(n,vector<mint>(2,0)); dfs(0,-1); ans += dp[0][1] * mint(2).pow(i); } cout<<ans.val()<<endl; return 0; }