結果

問題 No.2377 SUM AND XOR on Tree
ユーザー i_am_noobi_am_noob
提出日時 2023-07-07 21:38:33
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 433 ms / 4,000 ms
コード長 1,345 bytes
コンパイル時間 2,038 ms
コンパイル使用メモリ 202,556 KB
実行使用メモリ 16,780 KB
最終ジャッジ日時 2023-09-28 22:39:09
合計ジャッジ時間 9,746 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
5,852 KB
testcase_01 AC 3 ms
5,732 KB
testcase_02 AC 3 ms
5,724 KB
testcase_03 AC 3 ms
5,916 KB
testcase_04 AC 3 ms
5,772 KB
testcase_05 AC 3 ms
5,920 KB
testcase_06 AC 3 ms
5,788 KB
testcase_07 AC 4 ms
5,840 KB
testcase_08 AC 333 ms
10,576 KB
testcase_09 AC 287 ms
10,496 KB
testcase_10 AC 390 ms
10,576 KB
testcase_11 AC 353 ms
10,500 KB
testcase_12 AC 416 ms
10,568 KB
testcase_13 AC 403 ms
10,552 KB
testcase_14 AC 329 ms
10,300 KB
testcase_15 AC 418 ms
10,588 KB
testcase_16 AC 433 ms
10,380 KB
testcase_17 AC 398 ms
10,632 KB
testcase_18 AC 3 ms
6,004 KB
testcase_19 AC 3 ms
6,012 KB
testcase_20 AC 3 ms
5,784 KB
testcase_21 AC 3 ms
5,728 KB
testcase_22 AC 4 ms
5,804 KB
testcase_23 AC 334 ms
10,512 KB
testcase_24 AC 332 ms
10,500 KB
testcase_25 AC 328 ms
10,680 KB
testcase_26 AC 119 ms
16,640 KB
testcase_27 AC 121 ms
16,652 KB
testcase_28 AC 123 ms
16,780 KB
testcase_29 AC 90 ms
10,476 KB
testcase_30 AC 91 ms
10,408 KB
testcase_31 AC 90 ms
10,484 KB
testcase_32 AC 97 ms
10,396 KB
testcase_33 AC 96 ms
10,468 KB
testcase_34 AC 96 ms
10,408 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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";
}
0