답안 #95407

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
95407 2019-01-31T16:23:04 Z dalgerok Deblo (COCI18_deblo) C++14
90 / 90
186 ms 17144 KB
#include<bits/stdc++.h>
#define int long long
using namespace std;



const int N = 1e5 + 5;




int n, dp[N][2], a[N], b[N], ans, cur;
vector < int > g[N];

void dfs(int v, int pr = -1){
    if(b[v]){
        cur += 1;
    }
    dp[v][0] = dp[v][1] = 0;
    dp[v][b[v]] += 1;
    for(int to : g[v]){
        if(to != pr){
            dfs(to, v);
            cur += dp[v][0] * dp[to][1];
            cur += dp[v][1] * dp[to][0];
            dp[v][0] += dp[to][b[v]];
            dp[v][1] += dp[to][b[v] ^ 1];
        }
    }
}
 main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> a[i];
    }
    for(int i = 1; i < n; i++){
        int x, y;
        cin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    for(int i = 0; i < 22; i++){
        for(int j = 1; j <= n; j++){
            b[j] = ((a[j] >> i) & 1);
        }
        cur = 0;
        dfs(1);
        ans += (1 << i) * cur;
    }
    cout << ans;
}

Compilation message

deblo.cpp:31:7: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  main(){
       ^
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2680 KB Output is correct
2 Correct 3 ms 2680 KB Output is correct
3 Correct 4 ms 2680 KB Output is correct
4 Correct 4 ms 2808 KB Output is correct
5 Correct 4 ms 2808 KB Output is correct
6 Correct 97 ms 17144 KB Output is correct
7 Correct 87 ms 17016 KB Output is correct
8 Correct 101 ms 12380 KB Output is correct
9 Correct 105 ms 11896 KB Output is correct
10 Correct 186 ms 11424 KB Output is correct