#include <stdio.h>
#include <stdlib.h>
#define N 200001
#define N_ (1 << 18) /* N_ = pow2(ceil(log2(N + 1))) */
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 dd[N], pp[N], qq[N], ta[N], tb[N];
int dfs1(int i, int d) {
int o, s, k_, j_;
dd[i] = d;
s = 1, k_ = 0, j_ = -1;
for (o = eo[i]; o--; ) {
int j = ej[i][o], k = dfs1(j, d + 1);
s += k;
if (k_ < k)
k_ = k, j_ = j;
}
qq[i] = j_;
return s;
}
void dfs2(int i, int q) {
static int time;
int o, j_;
ta[i] = time++;
j_ = qq[i], qq[i] = q;
if (j_ != -1)
dfs2(j_, q);
for (o = eo[i]; o--; ) {
int j = ej[i][o];
if (j != j_)
dfs2(j, j);
}
tb[i] = time;
}
int ft[N];
void ft_update(int i, int n) {
while (i < n) {
ft[i] ^= 1;
i |= i + 1;
}
}
int ft_query(int i) {
int x = 0;
while (i >= 0) {
x ^= ft[i];
i &= i + 1, i--;
}
return x;
}
int st[N_ * 2], n_;
void pul(int i) {
st[i] = st[i << 1 | 0] | st[i << 1 | 1];
}
void st_update(int i, int x) {
st[i += n_] = x;
while (i > 1)
pul(i >>= 1);
}
int st_query(int r) {
int l = 0;
for (l += n_, r += n_; l <= r; l >>= 1, r >>= 1)
if ((r & 1) == 0) {
if (st[r]) {
while (r < n_)
r = st[r << 1 | 1] ? r << 1 | 1 : r << 1 | 0;
return r - n_;
}
r--;
}
return -1;
}
int main() {
static int pp[N], kk[N];
int n, i, j, a, flip;
scanf("%d", &n), n++;
for (i = 0; i < n; i++)
ej[i] = (int *) malloc(2 * sizeof *ej[i]);
pp[0] = -1;
for (i = 1; i < n; i++) {
scanf("%d", &pp[i]);
append(pp[i], i);
}
dfs1(0, 0), dfs2(0, 0);
ft_update(0, n), ft_update(1, n);
n_ = 1;
while (n_ <= n)
n_ <<= 1;
for (i = 1; i < n; i++) {
j = i;
while (j != -1) {
a = st_query(ta[j] - 1);
if (a >= ta[qq[j]]) {
ft_update(a + 1, n), ft_update(ta[j] + 1, n);
printf("%d\n", dd[i] - dd[j] + ta[j] - a - 1);
break;
}
ft_update(ta[qq[j]], n), ft_update(ta[j] + 1, n);
j = qq[j];
flip = 0;
if (pp[j] != -1) {
if ((dd[i] - dd[j]) % 2 == 0) {
if (kk[pp[j]]++ == 0) {
st_update(ta[pp[j]], 1);
flip = ft_query(ta[pp[j]]);
}
} else {
if (--kk[pp[j]] == 0) {
st_update(ta[pp[j]], 0);
flip = !ft_query(ta[pp[j]] + 1);
}
}
}
if (!flip) {
printf("%d\n", dd[i] - dd[j]);
break;
}
j = pp[j];
}
}
return 0;
}
Compilation message
Main.c: In function 'append':
Main.c:12:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
12 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
Main.c: In function 'main':
Main.c:102:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
102 | scanf("%d", &n), n++;
| ^~~~~~~~~~~~~~~
Main.c:107:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
107 | scanf("%d", &pp[i]);
| ^~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6900 KB |
Output is correct |
2 |
Correct |
2 ms |
6492 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
6580 KB |
Output is correct |
5 |
Correct |
2 ms |
6492 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6900 KB |
Output is correct |
2 |
Correct |
2 ms |
6492 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
6580 KB |
Output is correct |
5 |
Correct |
2 ms |
6492 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
6 ms |
7004 KB |
Output is correct |
9 |
Correct |
5 ms |
7772 KB |
Output is correct |
10 |
Correct |
4 ms |
7004 KB |
Output is correct |
11 |
Correct |
4 ms |
7004 KB |
Output is correct |
12 |
Correct |
4 ms |
7004 KB |
Output is correct |
13 |
Correct |
5 ms |
7004 KB |
Output is correct |
14 |
Correct |
4 ms |
7172 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6900 KB |
Output is correct |
2 |
Correct |
2 ms |
6492 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
6580 KB |
Output is correct |
5 |
Correct |
2 ms |
6492 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
6 ms |
7004 KB |
Output is correct |
9 |
Correct |
5 ms |
7772 KB |
Output is correct |
10 |
Correct |
4 ms |
7004 KB |
Output is correct |
11 |
Correct |
4 ms |
7004 KB |
Output is correct |
12 |
Correct |
4 ms |
7004 KB |
Output is correct |
13 |
Correct |
5 ms |
7004 KB |
Output is correct |
14 |
Correct |
4 ms |
7172 KB |
Output is correct |
15 |
Correct |
52 ms |
12348 KB |
Output is correct |
16 |
Correct |
53 ms |
19796 KB |
Output is correct |
17 |
Correct |
26 ms |
11356 KB |
Output is correct |
18 |
Correct |
30 ms |
11356 KB |
Output is correct |
19 |
Correct |
35 ms |
11808 KB |
Output is correct |
20 |
Correct |
43 ms |
11836 KB |
Output is correct |
21 |
Correct |
44 ms |
14348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
97 ms |
16888 KB |
Output is correct |
2 |
Correct |
73 ms |
16980 KB |
Output is correct |
3 |
Correct |
58 ms |
14420 KB |
Output is correct |
4 |
Correct |
80 ms |
16520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6900 KB |
Output is correct |
2 |
Correct |
2 ms |
6492 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
6580 KB |
Output is correct |
5 |
Correct |
2 ms |
6492 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
6 ms |
7004 KB |
Output is correct |
9 |
Correct |
5 ms |
7772 KB |
Output is correct |
10 |
Correct |
4 ms |
7004 KB |
Output is correct |
11 |
Correct |
4 ms |
7004 KB |
Output is correct |
12 |
Correct |
4 ms |
7004 KB |
Output is correct |
13 |
Correct |
5 ms |
7004 KB |
Output is correct |
14 |
Correct |
4 ms |
7172 KB |
Output is correct |
15 |
Correct |
52 ms |
12348 KB |
Output is correct |
16 |
Correct |
53 ms |
19796 KB |
Output is correct |
17 |
Correct |
26 ms |
11356 KB |
Output is correct |
18 |
Correct |
30 ms |
11356 KB |
Output is correct |
19 |
Correct |
35 ms |
11808 KB |
Output is correct |
20 |
Correct |
43 ms |
11836 KB |
Output is correct |
21 |
Correct |
44 ms |
14348 KB |
Output is correct |
22 |
Correct |
97 ms |
16888 KB |
Output is correct |
23 |
Correct |
73 ms |
16980 KB |
Output is correct |
24 |
Correct |
58 ms |
14420 KB |
Output is correct |
25 |
Correct |
80 ms |
16520 KB |
Output is correct |
26 |
Correct |
99 ms |
18032 KB |
Output is correct |
27 |
Correct |
96 ms |
33668 KB |
Output is correct |
28 |
Correct |
59 ms |
17240 KB |
Output is correct |
29 |
Correct |
61 ms |
16028 KB |
Output is correct |
30 |
Correct |
74 ms |
16564 KB |
Output is correct |
31 |
Correct |
109 ms |
17492 KB |
Output is correct |
32 |
Correct |
106 ms |
23380 KB |
Output is correct |