#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] >= 1 << l && pp[i][l] != pp[j][l]) {
add_edge(u, k + i * (L + 1) + l), i = pp[i][l];
add_edge(u, k + j * (L + 1) + l), j = pp[j][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]--;
| ^~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
7 ms |
4436 KB |
Output is correct |
12 |
Correct |
6 ms |
4404 KB |
Output is correct |
13 |
Correct |
6 ms |
4468 KB |
Output is correct |
14 |
Correct |
6 ms |
4408 KB |
Output is correct |
15 |
Correct |
8 ms |
4456 KB |
Output is correct |
16 |
Correct |
7 ms |
4564 KB |
Output is correct |
17 |
Correct |
6 ms |
4460 KB |
Output is correct |
18 |
Correct |
6 ms |
4436 KB |
Output is correct |
19 |
Correct |
6 ms |
4564 KB |
Output is correct |
20 |
Correct |
7 ms |
4564 KB |
Output is correct |
21 |
Correct |
7 ms |
4564 KB |
Output is correct |
22 |
Correct |
7 ms |
4668 KB |
Output is correct |
23 |
Correct |
8 ms |
4820 KB |
Output is correct |
24 |
Correct |
6 ms |
4436 KB |
Output is correct |
25 |
Correct |
7 ms |
4436 KB |
Output is correct |
26 |
Correct |
7 ms |
4540 KB |
Output is correct |
27 |
Correct |
7 ms |
4400 KB |
Output is correct |
28 |
Correct |
7 ms |
4436 KB |
Output is correct |
29 |
Correct |
7 ms |
4436 KB |
Output is correct |
30 |
Correct |
7 ms |
4436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2257 ms |
443844 KB |
Output is correct |
2 |
Correct |
1437 ms |
443476 KB |
Output is correct |
3 |
Correct |
2335 ms |
447076 KB |
Output is correct |
4 |
Correct |
1472 ms |
443480 KB |
Output is correct |
5 |
Correct |
2243 ms |
448052 KB |
Output is correct |
6 |
Correct |
1444 ms |
442996 KB |
Output is correct |
7 |
Correct |
2160 ms |
439520 KB |
Output is correct |
8 |
Correct |
1285 ms |
438144 KB |
Output is correct |
9 |
Correct |
1994 ms |
412780 KB |
Output is correct |
10 |
Correct |
1974 ms |
409040 KB |
Output is correct |
11 |
Correct |
2009 ms |
413444 KB |
Output is correct |
12 |
Correct |
2037 ms |
416572 KB |
Output is correct |
13 |
Correct |
1967 ms |
409012 KB |
Output is correct |
14 |
Correct |
2007 ms |
417716 KB |
Output is correct |
15 |
Correct |
2085 ms |
417292 KB |
Output is correct |
16 |
Correct |
1973 ms |
409732 KB |
Output is correct |
17 |
Correct |
1994 ms |
411168 KB |
Output is correct |
18 |
Correct |
1970 ms |
410720 KB |
Output is correct |
19 |
Correct |
2029 ms |
415888 KB |
Output is correct |
20 |
Correct |
2114 ms |
418548 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
7 ms |
4436 KB |
Output is correct |
12 |
Correct |
6 ms |
4404 KB |
Output is correct |
13 |
Correct |
6 ms |
4468 KB |
Output is correct |
14 |
Correct |
6 ms |
4408 KB |
Output is correct |
15 |
Correct |
8 ms |
4456 KB |
Output is correct |
16 |
Correct |
7 ms |
4564 KB |
Output is correct |
17 |
Correct |
6 ms |
4460 KB |
Output is correct |
18 |
Correct |
6 ms |
4436 KB |
Output is correct |
19 |
Correct |
6 ms |
4564 KB |
Output is correct |
20 |
Correct |
7 ms |
4564 KB |
Output is correct |
21 |
Correct |
7 ms |
4564 KB |
Output is correct |
22 |
Correct |
7 ms |
4668 KB |
Output is correct |
23 |
Correct |
8 ms |
4820 KB |
Output is correct |
24 |
Correct |
6 ms |
4436 KB |
Output is correct |
25 |
Correct |
7 ms |
4436 KB |
Output is correct |
26 |
Correct |
7 ms |
4540 KB |
Output is correct |
27 |
Correct |
7 ms |
4400 KB |
Output is correct |
28 |
Correct |
7 ms |
4436 KB |
Output is correct |
29 |
Correct |
7 ms |
4436 KB |
Output is correct |
30 |
Correct |
7 ms |
4436 KB |
Output is correct |
31 |
Correct |
2257 ms |
443844 KB |
Output is correct |
32 |
Correct |
1437 ms |
443476 KB |
Output is correct |
33 |
Correct |
2335 ms |
447076 KB |
Output is correct |
34 |
Correct |
1472 ms |
443480 KB |
Output is correct |
35 |
Correct |
2243 ms |
448052 KB |
Output is correct |
36 |
Correct |
1444 ms |
442996 KB |
Output is correct |
37 |
Correct |
2160 ms |
439520 KB |
Output is correct |
38 |
Correct |
1285 ms |
438144 KB |
Output is correct |
39 |
Correct |
1994 ms |
412780 KB |
Output is correct |
40 |
Correct |
1974 ms |
409040 KB |
Output is correct |
41 |
Correct |
2009 ms |
413444 KB |
Output is correct |
42 |
Correct |
2037 ms |
416572 KB |
Output is correct |
43 |
Correct |
1967 ms |
409012 KB |
Output is correct |
44 |
Correct |
2007 ms |
417716 KB |
Output is correct |
45 |
Correct |
2085 ms |
417292 KB |
Output is correct |
46 |
Correct |
1973 ms |
409732 KB |
Output is correct |
47 |
Correct |
1994 ms |
411168 KB |
Output is correct |
48 |
Correct |
1970 ms |
410720 KB |
Output is correct |
49 |
Correct |
2029 ms |
415888 KB |
Output is correct |
50 |
Correct |
2114 ms |
418548 KB |
Output is correct |
51 |
Correct |
1 ms |
340 KB |
Output is correct |
52 |
Correct |
1158 ms |
415288 KB |
Output is correct |
53 |
Correct |
1180 ms |
415292 KB |
Output is correct |
54 |
Correct |
1187 ms |
415404 KB |
Output is correct |
55 |
Correct |
1092 ms |
415576 KB |
Output is correct |
56 |
Correct |
1089 ms |
415380 KB |
Output is correct |
57 |
Correct |
1076 ms |
415316 KB |
Output is correct |
58 |
Correct |
1881 ms |
419844 KB |
Output is correct |
59 |
Correct |
1920 ms |
420896 KB |
Output is correct |
60 |
Correct |
1876 ms |
448840 KB |
Output is correct |
61 |
Correct |
1913 ms |
450132 KB |
Output is correct |
62 |
Correct |
1421 ms |
443536 KB |
Output is correct |
63 |
Correct |
1432 ms |
443508 KB |
Output is correct |
64 |
Correct |
1357 ms |
439468 KB |
Output is correct |
65 |
Correct |
1427 ms |
443004 KB |
Output is correct |
66 |
Correct |
1412 ms |
414684 KB |
Output is correct |
67 |
Correct |
1552 ms |
422592 KB |
Output is correct |
68 |
Correct |
1443 ms |
414736 KB |
Output is correct |
69 |
Correct |
1534 ms |
417652 KB |
Output is correct |
70 |
Correct |
1560 ms |
417580 KB |
Output is correct |
71 |
Correct |
1431 ms |
414768 KB |
Output is correct |
72 |
Correct |
1460 ms |
414648 KB |
Output is correct |
73 |
Correct |
1591 ms |
423176 KB |
Output is correct |
74 |
Correct |
1441 ms |
414876 KB |
Output is correct |
75 |
Correct |
1549 ms |
417668 KB |
Output is correct |
76 |
Correct |
1612 ms |
414704 KB |
Output is correct |
77 |
Correct |
1557 ms |
410440 KB |
Output is correct |
78 |
Correct |
1996 ms |
411160 KB |
Output is correct |
79 |
Correct |
2005 ms |
408248 KB |
Output is correct |
80 |
Correct |
2064 ms |
417896 KB |
Output is correct |
81 |
Correct |
2029 ms |
413356 KB |
Output is correct |
82 |
Correct |
2047 ms |
413096 KB |
Output is correct |
83 |
Correct |
2020 ms |
408092 KB |
Output is correct |
84 |
Correct |
2072 ms |
416640 KB |
Output is correct |
85 |
Correct |
2043 ms |
414012 KB |
Output is correct |
86 |
Correct |
2062 ms |
408268 KB |
Output is correct |
87 |
Correct |
2017 ms |
410648 KB |
Output is correct |
88 |
Correct |
2008 ms |
423060 KB |
Output is correct |
89 |
Correct |
1913 ms |
416396 KB |
Output is correct |
90 |
Correct |
1911 ms |
416080 KB |
Output is correct |
91 |
Correct |
1937 ms |
420528 KB |
Output is correct |
92 |
Correct |
1949 ms |
418960 KB |
Output is correct |
93 |
Correct |
1918 ms |
417000 KB |
Output is correct |
94 |
Correct |
1911 ms |
415748 KB |
Output is correct |
95 |
Correct |
1950 ms |
418620 KB |
Output is correct |
96 |
Correct |
1939 ms |
416876 KB |
Output is correct |
97 |
Correct |
1937 ms |
420632 KB |
Output is correct |