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 <stdio.h>
#include <stdlib.h>
#define N 200000
int *ej[N], eo[N], 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 ta[N], tb[N];
void dfs1(int i) {
static int time;
int o;
ta[i] = time++;
for (o = eo[i]; o--; ) {
int j = ej[i][o];
dfs1(j);
}
tb[i] = time;
}
int ft[N];
void update(int i, int n) {
while (i < n) {
ft[i]++;
i |= i + 1;
}
}
int query(int i) {
int x = 0;
while (i >= 0) {
x += ft[i];
i &= i + 1, i--;
}
return x;
}
int ans[N];
void dfs2(int i) {
int o;
update(ta[i], n);
ans[i] -= query(tb[i] - 1) - query(ta[i] - 1);
for (o = eo[i]; o--; ) {
int j = ej[i][o];
dfs2(j);
}
ans[i] += query(tb[i] - 1) - query(ta[i] - 1);
}
int main() {
int p, i;
scanf("%d", &n);
for (i = 0; i < n; i++)
ej[i] = (int *) malloc(2 * sizeof *ej[i]), eo[i] = 0;
for (i = 1; i < n; i++) {
scanf("%d", &p), p--;
append(p, i);
}
dfs1(0);
for (i = 0; i < n; i++)
free(ej[i]);
for (i = 0; i < n; i++)
ej[i] = (int *) malloc(2 * sizeof *ej[i]), eo[i] = 0;
for (i = 1; i < n; i++) {
scanf("%d", &p), p--;
append(p, i);
}
dfs2(0);
for (i = 0; i < n; i++)
printf("%d ", ans[i]);
printf("\n");
return 0;
}
Compilation message (stderr)
Main.c: In function 'append':
Main.c:11:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
11 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
Main.c: In function 'main':
Main.c:68:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
68 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
Main.c:72:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
72 | scanf("%d", &p), p--;
| ^~~~~~~~~~~~~~~
Main.c:81:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
81 | scanf("%d", &p), p--;
| ^~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |