結果

問題 No.2377 SUM AND XOR on Tree
ユーザー pointN
提出日時 2023-07-12 02:57:18
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 346 ms / 4,000 ms
コード長 1,718 bytes
コンパイル時間 1,268 ms
コンパイル使用メモリ 133,476 KB
最終ジャッジ日時 2025-02-15 09:55:13
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <cmath>
#include <iomanip>
#include <stack>
#include <queue>
#include <numeric>
#include <map>
#include <unordered_map>
#include <set>
#include <fstream>
#include <chrono>
#include <random>
#include <bitset>
//#include <atcoder/all>
#define rep(i,n) for(int i=0;i<(n);i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) ((int)(x).size())
#define pb push_back
using ll = long long;
using namespace std;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
ll gcd(ll a, ll b) {return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) {return a/gcd(a,b)*b;}

const ll mod = 998244353;

void dfs(int n, int p, vector<ll>& A, int s, vector<vector<int>>& G, vector<vector<ll>>& dp){
  rep(i,sz(G[n])){
    int x = G[n][i];
    if(x==p) continue;
    dfs(x,n,A,s,G,dp);
  }
  dp[0][n] = ((A[n]>>s)&1)==0;
  dp[1][n] = 1-dp[0][n];
  rep(i,sz(G[n])){
    int x = G[n][i];
    if(x==p) continue;
    ll t0 = dp[0][n];
    ll t1 = dp[1][n];
    dp[0][n] = (t0 * (dp[0][x] + dp[1][x]) + t1 * (dp[1][x])) % mod;
    dp[1][n] = (t0 * (dp[1][x]) + t1 * (dp[0][x] + dp[1][x])) % mod;
  }
}

int main(){
  int N; cin >> N;
  vector<vector<int>> G(N);
  rep(i,N-1){
    int a,b; cin >> a >> b; a--; b--;
    G[a].pb(b);
    G[b].pb(a);
  }
  vector<ll> A(N);
  rep(i,N) cin >> A[i];
  ll ans = 0;
  rep(s,30){
    vector<vector<ll>> dp(2,vector<ll>(N,0));
    dfs(0,-1,A,s,G,dp);
    ans += dp[1][0] * (1LL<<s) % mod;
    ans %= mod;
  }
  cout << ans % mod << '\n';
  return 0;
};
0