#include <stdio.h>
#include <stdlib.h>
#define N 100000
#define L 22
int *ej[N], eo[N];
void append(int i, int j) {
int o = eo[i]++;
if (o >= 2 && (o & o - 1) == 0)
ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]);
ej[i][o] = j;
}
int aa[N], kk[N][L], sz[N]; long long ans;
void dfs(int p, int i) {
int o, l;
for (l = 0; l < L; l++)
kk[i][l] = (aa[i] & 1 << l) == 0 ? 0 : 1;
sz[i] = 1;
ans += aa[i];
for (o = eo[i]; o--; ) {
int j = ej[i][o];
if (j != p) {
dfs(i, j);
for (l = 0; l < L; l++)
ans += (long long) kk[i][l] * (sz[j] - kk[j][l]) + (long long) (sz[i] - kk[i][l]) * kk[j][l] << l;
for (l = 0; l < L; l++)
kk[i][l] += (aa[i] & 1 << l) == 0 ? kk[j][l] : sz[j] - kk[j][l];
sz[i] += sz[j];
}
}
}
int main() {
int n, h, i, j;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d", &aa[i]);
ej[i] = (int *) malloc(2 * sizeof *ej[i]);
}
for (h = 0; h < n - 1; h++) {
scanf("%d%d", &i, &j), i--, j--;
append(i, j), append(j, i);
}
dfs(-1, 0);
printf("%lld\n", ans);
return 0;
}
Compilation message
deblo.c: In function 'append':
deblo.c:12:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
12 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
deblo.c: In function 'dfs':
deblo.c:32:54: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
32 | ans += (long long) kk[i][l] * (sz[j] - kk[j][l]) + (long long) (sz[i] - kk[i][l]) * kk[j][l] << l;
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
deblo.c: In function 'main':
deblo.c:43:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
43 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
deblo.c:45:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
45 | scanf("%d", &aa[i]);
| ^~~~~~~~~~~~~~~~~~~
deblo.c:49:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
49 | scanf("%d%d", &i, &j), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
59 ms |
23660 KB |
Output is correct |
7 |
Correct |
58 ms |
23588 KB |
Output is correct |
8 |
Correct |
52 ms |
17332 KB |
Output is correct |
9 |
Correct |
59 ms |
16660 KB |
Output is correct |
10 |
Correct |
73 ms |
15920 KB |
Output is correct |