#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 200000
#define L 18 /* L = ceil(log2(N)) */
#define N_ (N + N * (L + 1))
int min(int a, int b) { return a < b ? a : b; }
int *ej[N], eo[N], *ej_[N], eo_[N], *fi_[N], fo_[N], n, k;
int aa[N];
void append(int **ej, int *eo, 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;
}
void add_edge(int i, int j) {
append(ej_, eo_, i, j), append(fi_, fo_, j, i);
}
int dd[N], pp[N][L + 1], qu[N_], cnt;
void dfs0(int p, int i, int d) {
int o, l, q;
qu[cnt++] = i;
dd[i] = d, pp[i][0] = q = p;
add_edge(k + i * (L + 1) + 0, aa[i]);
for (l = 1; l <= L; l++)
if (q == -1) {
pp[i][l] = -1;
add_edge(k + i * (L + 1) + l, k + i * (L + 1) + l - 1);
} else {
pp[i][l] = pp[q][l - 1];
add_edge(k + i * (L + 1) + l, k + i * (L + 1) + l - 1);
add_edge(k + i * (L + 1) + l, k + q * (L + 1) + l - 1);
q = pp[i][l];
}
for (o = eo[i]; o--; ) {
int j = ej[i][o];
if (j != p)
dfs0(i, j, d + 1);
}
}
int cc[N_]; char out[N_];
void dfs1(int i) {
int o;
if (cc[i])
return;
cc[i] = -1;
for (o = eo_[i]; o--; ) {
int j = ej_[i][o];
dfs1(j);
}
qu[--cnt] = i;
}
void dfs2(int j, int c) {
int o;
if (cc[j] != -1) {
if (cc[j] != c)
out[cc[j]] = 1;
return;
}
cc[j] = c;
for (o = fo_[j]; o--; ) {
int i = fi_[j][o];
dfs2(i, c);
}
}
void add_path(int i, int j, int u) {
int l, tmp;
if (dd[i] < dd[j])
tmp = i, i = j, j = tmp;
for (l = L; l >= 0; l--)
if (dd[i] - dd[j] >= 1 << l)
add_edge(u, k + i * (L + 1) + l), i = pp[i][l];
if (i == j)
add_edge(u, k + i * (L + 1) + 0);
else {
for (l = L; l >= 0; l--)
if (dd[i] - dd[j] >= 1 << l) {
add_edge(u, k + i * (L + 1) + l), i = pp[i][l];
add_edge(u, k + j * (L + 1) + l), j = pp[i][l];
}
add_edge(u, k + i * (L + 1) + 1);
add_edge(u, k + j * (L + 1) + 1);
}
}
int main() {
static int kk[N_], prev[N];
int n_, h, i, j, a, c, ans;
scanf("%d%d", &n, &k);
n_ = k + n * (L + 1);
for (i = 0; i < n; i++)
ej[i] = (int *) malloc(2 * sizeof *ej[i]);
for (i = 0; i < n_; i++) {
ej_[i] = (int *) malloc(2 * sizeof *ej_[i]);
fi_[i] = (int *) malloc(2 * sizeof *fi_[i]);
}
for (h = 0; h < n - 1; h++) {
scanf("%d%d", &i, &j), i--, j--;
append(ej, eo, i, j), append(ej, eo, j, i);
}
for (i = 0; i < n; i++)
scanf("%d", &aa[i]), aa[i]--;
dfs0(-1, 0, 0);
memset(prev, -1, k * sizeof *prev);
for (h = 0; h < cnt; h++) {
j = qu[h], a = aa[j];
prev[a] = j;
}
for (h = 0; h < cnt; h++) {
j = qu[h], a = aa[j], i = prev[a];
add_path(i, j, a);
prev[a] = j;
}
cnt = n_;
for (i = 0; i < n_; i++)
dfs1(i);
for (h = 0, c = 0; h < n_; h++) {
i = qu[h];
if (cc[i] == -1)
dfs2(i, c++);
}
for (a = 0; a < k; a++)
kk[cc[a]]++;
ans = k + 1;
for (a = 0; a < k; a++)
if (!out[cc[a]])
ans = min(ans, kk[cc[a]] - 1);
printf("%d\n", ans);
return 0;
}
Compilation message
capital_city.c: In function 'append':
capital_city.c:17:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
17 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
capital_city.c: In function 'main':
capital_city.c:109:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
109 | scanf("%d%d", &n, &k);
| ^~~~~~~~~~~~~~~~~~~~~
capital_city.c:118:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
118 | scanf("%d%d", &i, &j), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~
capital_city.c:122:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
122 | scanf("%d", &aa[i]), aa[i]--;
| ^~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
420 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
416 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
420 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
416 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
6 ms |
4400 KB |
Output is correct |
12 |
Correct |
7 ms |
4436 KB |
Output is correct |
13 |
Correct |
8 ms |
4404 KB |
Output is correct |
14 |
Correct |
6 ms |
4436 KB |
Output is correct |
15 |
Correct |
7 ms |
4484 KB |
Output is correct |
16 |
Correct |
7 ms |
4564 KB |
Output is correct |
17 |
Correct |
6 ms |
4528 KB |
Output is correct |
18 |
Correct |
6 ms |
4436 KB |
Output is correct |
19 |
Correct |
8 ms |
4524 KB |
Output is correct |
20 |
Correct |
6 ms |
4564 KB |
Output is correct |
21 |
Correct |
6 ms |
4564 KB |
Output is correct |
22 |
Correct |
7 ms |
4724 KB |
Output is correct |
23 |
Correct |
8 ms |
4528 KB |
Output is correct |
24 |
Correct |
7 ms |
4436 KB |
Output is correct |
25 |
Correct |
7 ms |
4400 KB |
Output is correct |
26 |
Correct |
7 ms |
4436 KB |
Output is correct |
27 |
Correct |
7 ms |
4436 KB |
Output is correct |
28 |
Correct |
8 ms |
4436 KB |
Output is correct |
29 |
Correct |
7 ms |
4500 KB |
Output is correct |
30 |
Correct |
9 ms |
4592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
294 ms |
389188 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
420 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
416 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
6 ms |
4400 KB |
Output is correct |
12 |
Correct |
7 ms |
4436 KB |
Output is correct |
13 |
Correct |
8 ms |
4404 KB |
Output is correct |
14 |
Correct |
6 ms |
4436 KB |
Output is correct |
15 |
Correct |
7 ms |
4484 KB |
Output is correct |
16 |
Correct |
7 ms |
4564 KB |
Output is correct |
17 |
Correct |
6 ms |
4528 KB |
Output is correct |
18 |
Correct |
6 ms |
4436 KB |
Output is correct |
19 |
Correct |
8 ms |
4524 KB |
Output is correct |
20 |
Correct |
6 ms |
4564 KB |
Output is correct |
21 |
Correct |
6 ms |
4564 KB |
Output is correct |
22 |
Correct |
7 ms |
4724 KB |
Output is correct |
23 |
Correct |
8 ms |
4528 KB |
Output is correct |
24 |
Correct |
7 ms |
4436 KB |
Output is correct |
25 |
Correct |
7 ms |
4400 KB |
Output is correct |
26 |
Correct |
7 ms |
4436 KB |
Output is correct |
27 |
Correct |
7 ms |
4436 KB |
Output is correct |
28 |
Correct |
8 ms |
4436 KB |
Output is correct |
29 |
Correct |
7 ms |
4500 KB |
Output is correct |
30 |
Correct |
9 ms |
4592 KB |
Output is correct |
31 |
Runtime error |
294 ms |
389188 KB |
Execution killed with signal 11 |
32 |
Halted |
0 ms |
0 KB |
- |