Submission #305500

# Submission time Handle Problem Language Result Execution time Memory
305500 2020-09-23T11:12:48 Z phathnv Deblo (COCI18_deblo) C++11
90 / 90
161 ms 33120 KB
#include <bits/stdc++.h>

#define mp make_pair
#define X first
#define Y second
#define taskname "Deblo"

using namespace std;

typedef long long ll;
typedef pair <int, int> ii;

const int N = 1e5 + 1;
const int logV = 22;

int n, a[N];
vector <int> adj[N];

int cnt[N][logV][2];
ll res[logV];

void readInput(){
    scanf("%d", &n);
    for(int i = 1; i <= n; i++)
        scanf("%d", &a[i]);
    for(int i = 1; i < n; i++){
        int u, v;
        scanf("%d %d", &u, &v);
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
}

void dfs(int u, int p){
    for(int i = 0; i < logV; i++){
        int bit = (a[u] >> i) & 1;
        cnt[u][i][bit]++;
        if (bit)
            res[i]++;
    }
    for(int v : adj[u])
        if (v != p){
            dfs(v, u);
            for(int i = 0; i < logV; i++){
                res[i] += (ll) cnt[u][i][1] * cnt[v][i][0];
                res[i] += (ll) cnt[u][i][0] * cnt[v][i][1];

                int bit = (a[u] >> i) & 1;
                cnt[u][i][0] += cnt[v][i][0 ^ bit];
                cnt[u][i][1] += cnt[v][i][1 ^ bit];
            }
        }
}

void solve(){
    dfs(1, -1);
    ll ans = 0;
    for(int i = 0; i < logV; i++)
        ans += (ll) res[i] * (1 << i);
    cout << ans;
}

int main(){
    //freopen(taskname".inp", "r", stdin);
    //freopen(taskname".out", "w", stdout);
    readInput();
    solve();
    return 0;
}

Compilation message

deblo.cpp: In function 'void readInput()':
deblo.cpp:23:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   23 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
deblo.cpp:25:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   25 |         scanf("%d", &a[i]);
      |         ~~~~~^~~~~~~~~~~~~
deblo.cpp:28:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   28 |         scanf("%d %d", &u, &v);
      |         ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2688 KB Output is correct
2 Correct 2 ms 2688 KB Output is correct
3 Correct 3 ms 2688 KB Output is correct
4 Correct 3 ms 2944 KB Output is correct
5 Correct 3 ms 2944 KB Output is correct
6 Correct 93 ms 33120 KB Output is correct
7 Correct 95 ms 33016 KB Output is correct
8 Correct 99 ms 26776 KB Output is correct
9 Correct 100 ms 26104 KB Output is correct
10 Correct 161 ms 25336 KB Output is correct