#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 - 1) / 2];
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_);
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--;
| ^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Incorrect |
2 ms |
6648 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Incorrect |
2 ms |
6648 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Incorrect |
2 ms |
6648 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |