# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
87093 | someone_aa | Deblo (COCI18_deblo) | C++17 | 252 ms | 23556 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 |
---|---|---|---|---|
Fetching results... |