#include <stdio.h>
#include <stdlib.h>
#define N 200000
int min(int a, int b) { return a < b ? a : b; }
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 qu[N], pp[N], dd[N], rr[N];
void dfs1(int p, int i, int d) {
int o;
qu[d] = i;
pp[i] = p, dd[i] = d, rr[i] = d == 0 ? -1 : qu[d / 2 + 1];
for (o = eo[i]; o--; ) {
int j = ej[i][o];
dfs1(i, j, d + 1);
}
}
int cc[N], kk[N], jj[N], qq[N], d_;
void dfs2(int i) {
int o, j_, u, v;
kk[i] = 1;
j_ = -1;
for (o = eo[i]; o--; ) {
int j = ej[i][o];
dfs2(j);
if (qq[j] != -1) {
if (j_ == -1)
j_ = j;
else if (dd[qq[j_]] < dd[qq[j]]) {
for (u = j_, v = j; u != qq[j_]; u = jj[u], v = jj[v])
kk[v] += kk[u];
kk[v] += kk[u];
j_ = j;
} else {
for (u = j_, v = j; v != qq[j]; u = jj[u], v = jj[v])
kk[u] += kk[v];
kk[u] += kk[v];
}
}
}
jj[i] = j_, qq[i] = j_ == -1 ? i : qq[j_];
while (cc[i]--) {
d_ = min(d_, dd[qq[i]]);
if (--kk[qq[i]] == 0)
qq[i] = qq[i] == i ? -1 : pp[qq[i]];
}
}
int main() {
int n, m, i, j;
scanf("%d%d", &n, &m);
for (i = 0; i < n; i++)
ej[i] = (int *) malloc(2 * sizeof *ej[i]);
for (j = 1; j < n; j++) {
scanf("%d", &i), i--;
append(i, j);
}
dfs1(-1, 0, 0);
while (m--) {
scanf("%d", &i), i--;
if (i == 0) {
printf("1\n");
return 0;
}
cc[rr[i]]++;
}
d_ = n;
dfs2(0);
printf("%d\n", d_ + 1);
return 0;
}
Compilation message
mokesciai.c: In function 'append':
mokesciai.c:13:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
13 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
mokesciai.c: In function 'main':
mokesciai.c:69:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
69 | scanf("%d%d", &n, &m);
| ^~~~~~~~~~~~~~~~~~~~~
mokesciai.c:73:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
73 | scanf("%d", &i), i--;
| ^~~~~~~~~~~~~~~
mokesciai.c:78:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
78 | scanf("%d", &i), i--;
| ^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
2 ms |
7260 KB |
Output is correct |
4 |
Correct |
2 ms |
7260 KB |
Output is correct |
5 |
Correct |
2 ms |
7100 KB |
Output is correct |
6 |
Correct |
38 ms |
35108 KB |
Output is correct |
7 |
Correct |
37 ms |
35156 KB |
Output is correct |
8 |
Correct |
1 ms |
7256 KB |
Output is correct |
9 |
Correct |
51 ms |
36176 KB |
Output is correct |
10 |
Correct |
57 ms |
36524 KB |
Output is correct |
11 |
Correct |
46 ms |
35724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
35108 KB |
Output is correct |
2 |
Correct |
37 ms |
35156 KB |
Output is correct |
3 |
Correct |
1 ms |
7256 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6996 KB |
Output is correct |
6 |
Correct |
1 ms |
7004 KB |
Output is correct |
7 |
Correct |
1 ms |
6584 KB |
Output is correct |
8 |
Correct |
1 ms |
6836 KB |
Output is correct |
9 |
Correct |
1 ms |
6748 KB |
Output is correct |
10 |
Correct |
30 ms |
24272 KB |
Output is correct |
11 |
Correct |
30 ms |
24256 KB |
Output is correct |
12 |
Correct |
31 ms |
16464 KB |
Output is correct |
13 |
Correct |
32 ms |
25684 KB |
Output is correct |
14 |
Correct |
22 ms |
16220 KB |
Output is correct |
15 |
Correct |
37 ms |
16700 KB |
Output is correct |
16 |
Correct |
32 ms |
25684 KB |
Output is correct |
17 |
Correct |
39 ms |
16476 KB |
Output is correct |
18 |
Correct |
32 ms |
27660 KB |
Output is correct |
19 |
Correct |
28 ms |
17500 KB |
Output is correct |
20 |
Correct |
29 ms |
16724 KB |
Output is correct |
21 |
Correct |
28 ms |
16732 KB |
Output is correct |
22 |
Correct |
28 ms |
16724 KB |
Output is correct |
23 |
Correct |
26 ms |
16476 KB |
Output is correct |
24 |
Correct |
27 ms |
16436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
2 ms |
7260 KB |
Output is correct |
4 |
Correct |
2 ms |
7260 KB |
Output is correct |
5 |
Correct |
2 ms |
7100 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
6996 KB |
Output is correct |
8 |
Correct |
1 ms |
7004 KB |
Output is correct |
9 |
Correct |
1 ms |
6584 KB |
Output is correct |
10 |
Correct |
1 ms |
6836 KB |
Output is correct |
11 |
Correct |
1 ms |
6748 KB |
Output is correct |
12 |
Correct |
1 ms |
7256 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
1 ms |
7004 KB |
Output is correct |
19 |
Correct |
1 ms |
7004 KB |
Output is correct |
20 |
Correct |
2 ms |
7000 KB |
Output is correct |
21 |
Correct |
1 ms |
6748 KB |
Output is correct |
22 |
Correct |
1 ms |
6748 KB |
Output is correct |
23 |
Correct |
2 ms |
6820 KB |
Output is correct |
24 |
Correct |
2 ms |
6748 KB |
Output is correct |
25 |
Correct |
2 ms |
6748 KB |
Output is correct |
26 |
Correct |
2 ms |
6748 KB |
Output is correct |
27 |
Correct |
2 ms |
7004 KB |
Output is correct |
28 |
Correct |
2 ms |
7004 KB |
Output is correct |
29 |
Correct |
1 ms |
6748 KB |
Output is correct |
30 |
Correct |
2 ms |
7004 KB |
Output is correct |
31 |
Correct |
2 ms |
6748 KB |
Output is correct |
32 |
Correct |
1 ms |
6748 KB |
Output is correct |
33 |
Correct |
1 ms |
6744 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
7004 KB |
Output is correct |
7 |
Correct |
1 ms |
7004 KB |
Output is correct |
8 |
Correct |
2 ms |
7000 KB |
Output is correct |
9 |
Correct |
1 ms |
6748 KB |
Output is correct |
10 |
Correct |
1 ms |
6748 KB |
Output is correct |
11 |
Correct |
2 ms |
6820 KB |
Output is correct |
12 |
Correct |
2 ms |
6748 KB |
Output is correct |
13 |
Correct |
2 ms |
6748 KB |
Output is correct |
14 |
Correct |
2 ms |
6748 KB |
Output is correct |
15 |
Correct |
2 ms |
7004 KB |
Output is correct |
16 |
Correct |
2 ms |
7004 KB |
Output is correct |
17 |
Correct |
1 ms |
6748 KB |
Output is correct |
18 |
Correct |
2 ms |
7004 KB |
Output is correct |
19 |
Correct |
2 ms |
6748 KB |
Output is correct |
20 |
Correct |
1 ms |
6748 KB |
Output is correct |
21 |
Correct |
1 ms |
6744 KB |
Output is correct |
22 |
Correct |
1 ms |
6492 KB |
Output is correct |
23 |
Correct |
1 ms |
6492 KB |
Output is correct |
24 |
Correct |
2 ms |
7260 KB |
Output is correct |
25 |
Correct |
2 ms |
7260 KB |
Output is correct |
26 |
Correct |
2 ms |
7100 KB |
Output is correct |
27 |
Correct |
1 ms |
6492 KB |
Output is correct |
28 |
Correct |
2 ms |
6996 KB |
Output is correct |
29 |
Correct |
1 ms |
7004 KB |
Output is correct |
30 |
Correct |
1 ms |
6584 KB |
Output is correct |
31 |
Correct |
1 ms |
6836 KB |
Output is correct |
32 |
Correct |
1 ms |
6748 KB |
Output is correct |
33 |
Correct |
38 ms |
35108 KB |
Output is correct |
34 |
Correct |
37 ms |
35156 KB |
Output is correct |
35 |
Correct |
1 ms |
7256 KB |
Output is correct |
36 |
Correct |
30 ms |
24272 KB |
Output is correct |
37 |
Correct |
30 ms |
24256 KB |
Output is correct |
38 |
Correct |
31 ms |
16464 KB |
Output is correct |
39 |
Correct |
32 ms |
25684 KB |
Output is correct |
40 |
Correct |
22 ms |
16220 KB |
Output is correct |
41 |
Correct |
37 ms |
16700 KB |
Output is correct |
42 |
Correct |
32 ms |
25684 KB |
Output is correct |
43 |
Correct |
39 ms |
16476 KB |
Output is correct |
44 |
Correct |
32 ms |
27660 KB |
Output is correct |
45 |
Correct |
28 ms |
17500 KB |
Output is correct |
46 |
Correct |
29 ms |
16724 KB |
Output is correct |
47 |
Correct |
28 ms |
16732 KB |
Output is correct |
48 |
Correct |
28 ms |
16724 KB |
Output is correct |
49 |
Correct |
26 ms |
16476 KB |
Output is correct |
50 |
Correct |
27 ms |
16436 KB |
Output is correct |
51 |
Correct |
51 ms |
36176 KB |
Output is correct |
52 |
Correct |
57 ms |
36524 KB |
Output is correct |
53 |
Correct |
46 ms |
35724 KB |
Output is correct |
54 |
Correct |
33 ms |
24560 KB |
Output is correct |
55 |
Correct |
33 ms |
24508 KB |
Output is correct |
56 |
Correct |
37 ms |
26704 KB |
Output is correct |
57 |
Correct |
30 ms |
16948 KB |
Output is correct |
58 |
Correct |
30 ms |
16688 KB |
Output is correct |
59 |
Correct |
41 ms |
20908 KB |
Output is correct |
60 |
Correct |
43 ms |
17236 KB |
Output is correct |
61 |
Correct |
49 ms |
26908 KB |
Output is correct |
62 |
Correct |
46 ms |
17020 KB |
Output is correct |
63 |
Correct |
38 ms |
28448 KB |
Output is correct |
64 |
Correct |
33 ms |
18000 KB |
Output is correct |
65 |
Correct |
34 ms |
16916 KB |
Output is correct |
66 |
Correct |
38 ms |
17308 KB |
Output is correct |
67 |
Correct |
37 ms |
17400 KB |
Output is correct |
68 |
Correct |
38 ms |
16972 KB |
Output is correct |
69 |
Correct |
39 ms |
17236 KB |
Output is correct |
70 |
Correct |
19 ms |
12380 KB |
Output is correct |
71 |
Correct |
40 ms |
23672 KB |
Output is correct |