Submission #87093

# Submission time Handle Problem Language Result Execution time Memory
87093 2018-11-29T13:24:28 Z someone_aa Deblo (COCI18_deblo) C++17
90 / 90
252 ms 23556 KB
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
using namespace std;
const int maxn = 100100;
ll arr[maxn], value[maxn], n;
vector<int>g[maxn];
ll dp[maxn][2];
ll result, cnt;

void dfs(int node, int p) {
    ll sum_even = 0LL, sum_odd = 0LL;
    for(int i:g[node]) {
        if(i != p) {
            dfs(i, node);
            if(value[node] == 0) {
                dp[node][0] += dp[i][0];
                dp[node][1] += dp[i][1];
            }
            else if(value[node] == 1){
                dp[node][0] += dp[i][1];
                dp[node][1] += dp[i][0];
            }
            sum_even += dp[i][0];
            sum_odd += dp[i][1];
        }
    }

    ll temp = 0LL;
    if(g[node].size() == 1 && node != 1) {
        if(value[node] == 1) {
            dp[node][1] = 1LL;
        }
        else {
            dp[node][0] = 1LL;
        }
    }
    else {
        if(value[node] == 0) {
            for(int i:g[node]) {
                if(i!=p) {
                    temp += 1LL * dp[i][0] * (sum_odd - dp[i][1]);
                }
            }
            dp[node][0]++;
        }
        else if(value[node] == 1) {
            ll tempb = 0LL;
            for(int i:g[node]) {
                if(i!=p) {
                    tempb += 1LL * dp[i][0] * (sum_even - dp[i][0]);
                    tempb += 1LL * dp[i][1] * (sum_odd - dp[i][1]);
                }
            }
            tempb /= 2LL;
            dp[node][1] ++;
            temp += tempb;
        }
    }
    temp += dp[node][1];
    cnt += temp;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++) {
        cin>>arr[i];
    }

    int u, v;
    for(int i=0;i<n-1;i++) {
        cin>>u>>v;
        g[u].pb(v);
        g[v].pb(u);
    }
    for(int bit=0;bit<22;bit++) {
        cnt = 0LL;
        for(int i=1;i<=n;i++) {
            if(arr[i]&(1<<bit)) {
                value[i] = 1;
            }
            else {
                value[i] = 0;
            }
            dp[i][0] = dp[i][1] = 0LL;
        }
        dfs(1, -1);
        result += 1LL * cnt * (1LL<<bit);
    }
    cout<<result<<"\n";
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2680 KB Output is correct
2 Correct 5 ms 2832 KB Output is correct
3 Correct 4 ms 2832 KB Output is correct
4 Correct 6 ms 2848 KB Output is correct
5 Correct 6 ms 2904 KB Output is correct
6 Correct 149 ms 21920 KB Output is correct
7 Correct 150 ms 23556 KB Output is correct
8 Correct 164 ms 23556 KB Output is correct
9 Correct 149 ms 23556 KB Output is correct
10 Correct 252 ms 23556 KB Output is correct