結果

問題 No.2377 SUM AND XOR on Tree
ユーザー pointNpointN
提出日時 2023-07-12 02:57:18
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 548 ms / 4,000 ms
コード長 1,718 bytes
コンパイル時間 1,575 ms
コンパイル使用メモリ 136,632 KB
実行使用メモリ 22,696 KB
最終ジャッジ日時 2023-10-11 17:19:24
合計ジャッジ時間 14,908 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 1 ms
4,352 KB
testcase_05 AC 2 ms
4,352 KB
testcase_06 AC 2 ms
4,352 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 517 ms
11,692 KB
testcase_09 AC 534 ms
11,808 KB
testcase_10 AC 525 ms
11,740 KB
testcase_11 AC 519 ms
11,784 KB
testcase_12 AC 535 ms
11,688 KB
testcase_13 AC 548 ms
11,872 KB
testcase_14 AC 493 ms
11,588 KB
testcase_15 AC 507 ms
11,868 KB
testcase_16 AC 485 ms
11,684 KB
testcase_17 AC 510 ms
11,608 KB
testcase_18 AC 2 ms
4,352 KB
testcase_19 AC 2 ms
4,352 KB
testcase_20 AC 2 ms
4,352 KB
testcase_21 AC 2 ms
4,352 KB
testcase_22 AC 2 ms
4,352 KB
testcase_23 AC 477 ms
11,740 KB
testcase_24 AC 443 ms
11,824 KB
testcase_25 AC 448 ms
11,744 KB
testcase_26 AC 258 ms
22,564 KB
testcase_27 AC 263 ms
22,696 KB
testcase_28 AC 251 ms
22,644 KB
testcase_29 AC 176 ms
12,108 KB
testcase_30 AC 177 ms
12,040 KB
testcase_31 AC 177 ms
12,024 KB
testcase_32 AC 191 ms
11,624 KB
testcase_33 AC 198 ms
11,740 KB
testcase_34 AC 195 ms
11,688 KB
権限があれば一括ダウンロードができます

ソースコード

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