| # | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 | 
|---|---|---|---|---|---|---|---|
| 701730 | scottchou | Deblo (COCI18_deblo) | C++17 | 339 ms | 18776 KiB | 
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
int const N = 1e5 + 5;
typedef long long LL;
int a[N];
vector<int> graph[N];
LL cnt0[N], cnt1[N];
void dfs(int x, int tw, int fa = -1){
    cnt0[x] = 0;
    cnt1[x] = 0;
    for(auto i : graph[x]){
        if(i == fa)
            continue;
        dfs(i, tw, x);
        cnt0[x] += cnt0[i];
        cnt1[x] += cnt1[i];
    }
    if(a[x] & (1 << tw)){
        swap(cnt0[x], cnt1[x]);
        cnt1[x]++;
    }else{
        cnt0[x]++;
    }
}
LL ans = 0;
void dfs2(int x, int tw, LL up0 = 0, LL up1 = 0, int fa = -1){
    ans += cnt1[x] * (1 << tw);
    if(a[x] & (1 << tw))
        swap(up0, up1);
    ans += up1 * (1 << tw);
    for(auto i : graph[x]){
        if(i == fa)
            continue;
        if(a[x] & (1 << tw)){
            dfs2(i, tw, up0 + cnt0[x] - cnt1[i], up1 + cnt1[x] - cnt0[i], x);
        }else{
            dfs2(i, tw, up0 + cnt0[x] - cnt0[i], up1 + cnt1[x] - cnt1[i], x);
        }
    }
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    int u, v;
    LL sum = 0;
    for(int i = 1; i <= n; i++){
        cin >> a[i];
        sum += a[i];
    }
    for(int i = 0; i < n - 1; i++){
        cin >> u >> v;
        graph[u].push_back(v);
        graph[v].push_back(u);        
    }
    for(int i = 0; i <= 22; i++){
        dfs(1, i);
        dfs2(1, i);
    }
    cout << (ans - sum) / 2 + sum << '\n';
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|---|---|---|---|
| Fetching results... | ||||
