結果
問題 | No.2377 SUM AND XOR on Tree |
ユーザー |
|
提出日時 | 2023-07-07 21:38:33 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 278 ms / 4,000 ms |
コード長 | 1,345 bytes |
コンパイル時間 | 1,891 ms |
コンパイル使用メモリ | 196,332 KB |
最終ジャッジ日時 | 2025-02-15 06:57:09 |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 33 |
ソースコード
#include<bits/stdc++.h> using namespace std; using ll=long long; using pii=pair<int,int>; #define all(a) a.begin(),a.end() #define pb push_back #define sz(a) ((int)a.size()) const int N=100005,mod=998244353; int add(int x, int y){x+=y; if(x>=mod) x-=mod; return x;} int sub(int x, int y){x-=y; if(x<0) x+=mod; return x;} int mul(int x, int y){return ((ll)x)*y%mod;} int Pow(int x, ll y=mod-2){int res=1; for(; y; x=mul(x,x),y>>=1) if(y&1) res=mul(res,x); return res;} int n,a[N],b[N],dp[N][2]; vector<int> adj[N]; void dfs(int u, int fa){ for(auto v: adj[u]) if(v!=fa) dfs(v,u); dp[u][0]=dp[u][1]=0; dp[u][b[u]]=1; for(auto v: adj[u]) if(v!=fa){ int nw[2]{}; nw[0]=add(mul(dp[u][0],dp[v][0]),mul(dp[u][1],dp[v][1])); nw[1]=add(mul(dp[u][0],dp[v][1]),mul(dp[u][1],dp[v][0])); for(int i: {0,1}) nw[i]=add(nw[i],mul(dp[u][i],dp[v][1])); for(int i: {0,1}) dp[u][i]=nw[i]; } } signed main(){ ios_base::sync_with_stdio(0),cin.tie(0); cin >> n; for(int i=0; i<n-1; ++i){ int u,v; cin >> u >> v; u--,v--; adj[u].pb(v),adj[v].pb(u); } for(int i=0; i<n; ++i) cin >> a[i]; int res=0; for(int j=0; j<30; ++j){ for(int i=0; i<n; ++i) b[i]=a[i]>>j&1; dfs(0,-1); res=add(res,mul(1<<j,dp[0][1])); } cout << res << "\n"; }