Submission #750486

# Submission time Handle Problem Language Result Execution time Memory
750486 2023-05-29T14:50:07 Z Trunkty Deblo (COCI18_deblo) C++14
90 / 90
276 ms 19420 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll

int n,ans;
int arr[100005];
vector<int> roads[100005];
int dp[100005][2];

void dfs(int x, int p, int j){
    dp[x][0] = 0;
    dp[x][1] = 0;
    for(int i:roads[x]){
        if(i!=p){
            dfs(i,x,j);
            dp[x][0] += dp[i][0];
            dp[x][1] += dp[i][1];
        }
    }
    int tot=0;
    if(arr[x]&(1LL<<j)){
        tot += (1LL<<j)*(dp[x][0]*dp[x][0]+dp[x][1]*dp[x][1]);
        for(int i:roads[x]){
            if(i!=p){
                tot -= (1LL<<j)*(dp[i][0]*dp[i][0]+dp[i][1]*dp[i][1]);
            }
        }
        tot /= 2LL;
    }
    else{
        tot += (1LL<<j)*(dp[x][0]*dp[x][1]);
        for(int i:roads[x]){
            if(i!=p){
                tot -= (1LL<<j)*(dp[i][0]*dp[i][1]);
            }
        }
    }
    ans += tot;
    if(arr[x]&(1LL<<j)){
        ans += (1LL<<j)*(dp[x][0]+1LL);
        swap(dp[x][0],dp[x][1]);
        dp[x][1]++;
    }
    else{
        ans += (1LL<<j)*dp[x][1];
        dp[x][0]++;
    }
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n;
    for(int i=1;i<=n;i++){
        cin >> arr[i];
    }
    for(int i=1;i<=n-1;i++){
        int a,b;
        cin >> a >> b;
        roads[a].push_back(b);
        roads[b].push_back(a);
    }
    for(int j=0;j<=25;j++){
        dfs(1,-1,j);
    }
    cout << ans << "\n";
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 3 ms 2692 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2772 KB Output is correct
5 Correct 3 ms 2700 KB Output is correct
6 Correct 147 ms 19420 KB Output is correct
7 Correct 123 ms 19276 KB Output is correct
8 Correct 115 ms 12200 KB Output is correct
9 Correct 142 ms 11368 KB Output is correct
10 Correct 276 ms 10568 KB Output is correct