#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(x) (x).begin(), (x).end()
const int N = 5e5 + 7, L = 20;
int n, k;
vector<int> adj[N];
int c[N], dep[N];
int lift[N][L];
int l[N], cum[N];
int cnt, r;
bool seen[N];
void prep(int v = 1, int p = 0) {
lift[v][0] = p;
for (int i = 1; i < L; i++)
lift[v][i] = lift[lift[v][i - 1]][i - 1];
for (int u : adj[v]) {
if (u == p)
continue;
dep[u] = dep[v] + 1;
prep(u, v);
}
}
int lca(int u, int v) {
if (u == 0 || v == 0)
return max(u, v);
if (dep[u] > dep[v])
swap(u, v);
for (int i = 0; i < L; i++)
if ((dep[v] - dep[u]) >> i & 1)
v = lift[v][i];
if (u == v)
return u;
for (int i = L - 1; i >= 0; i--)
if (lift[v][i] != lift[u][i])
v = lift[v][i], u = lift[u][i];
return lift[u][0];
}
void run(int v = 1, int p = 0) {
for (int u : adj[v]) {
if (u == p)
continue;
run(u, v);
cum[v] += cum[u];
}
}
void dfs(int v = 1, int p = 0) {
for (int u : adj[v]) {
if (u == p)
continue;
dfs(u, v);
seen[v] |= seen[u];
}
if (v != 1 && !cum[v] && !seen[v]) {
cnt++;
r = lca(r, v);
seen[v] = 1;
}
}
int main() {
cin.tie(0)->sync_with_stdio(false);
cin >> n >> k;
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
prep();
for (int i = 1; i <= n; i++) {
cin >> c[i];
l[c[i]] = lca(l[c[i]], i);
}
for (int i = 1; i <= n; i++)
cum[i]++, cum[l[c[i]]]--;
run();
dfs();
if (!r) {
cout << 0 << '\n';
return 0;
}
int need = 0;
while (r != 1) {
if (!cum[r])
need = 1;
r = lift[r][0];
}
cout << ((cnt | need) + 1) / 2 << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
18268 KB |
Output is correct |
2 |
Correct |
3 ms |
18268 KB |
Output is correct |
3 |
Correct |
3 ms |
18268 KB |
Output is correct |
4 |
Correct |
3 ms |
18268 KB |
Output is correct |
5 |
Correct |
3 ms |
18268 KB |
Output is correct |
6 |
Correct |
3 ms |
18264 KB |
Output is correct |
7 |
Correct |
3 ms |
18268 KB |
Output is correct |
8 |
Correct |
3 ms |
18268 KB |
Output is correct |
9 |
Correct |
3 ms |
18368 KB |
Output is correct |
10 |
Correct |
3 ms |
18268 KB |
Output is correct |
11 |
Correct |
3 ms |
18268 KB |
Output is correct |
12 |
Correct |
3 ms |
18268 KB |
Output is correct |
13 |
Correct |
3 ms |
18432 KB |
Output is correct |
14 |
Correct |
4 ms |
18268 KB |
Output is correct |
15 |
Correct |
4 ms |
18360 KB |
Output is correct |
16 |
Correct |
3 ms |
18268 KB |
Output is correct |
17 |
Correct |
3 ms |
18264 KB |
Output is correct |
18 |
Correct |
3 ms |
18372 KB |
Output is correct |
19 |
Correct |
3 ms |
18268 KB |
Output is correct |
20 |
Correct |
3 ms |
18268 KB |
Output is correct |
21 |
Correct |
3 ms |
18268 KB |
Output is correct |
22 |
Correct |
3 ms |
18424 KB |
Output is correct |
23 |
Correct |
3 ms |
18268 KB |
Output is correct |
24 |
Correct |
3 ms |
18268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
18268 KB |
Output is correct |
2 |
Correct |
3 ms |
18268 KB |
Output is correct |
3 |
Correct |
3 ms |
18268 KB |
Output is correct |
4 |
Correct |
3 ms |
18268 KB |
Output is correct |
5 |
Correct |
3 ms |
18268 KB |
Output is correct |
6 |
Correct |
3 ms |
18264 KB |
Output is correct |
7 |
Correct |
3 ms |
18268 KB |
Output is correct |
8 |
Correct |
3 ms |
18268 KB |
Output is correct |
9 |
Correct |
3 ms |
18368 KB |
Output is correct |
10 |
Correct |
3 ms |
18268 KB |
Output is correct |
11 |
Correct |
3 ms |
18268 KB |
Output is correct |
12 |
Correct |
3 ms |
18268 KB |
Output is correct |
13 |
Correct |
3 ms |
18432 KB |
Output is correct |
14 |
Correct |
4 ms |
18268 KB |
Output is correct |
15 |
Correct |
4 ms |
18360 KB |
Output is correct |
16 |
Correct |
3 ms |
18268 KB |
Output is correct |
17 |
Correct |
3 ms |
18264 KB |
Output is correct |
18 |
Correct |
3 ms |
18372 KB |
Output is correct |
19 |
Correct |
3 ms |
18268 KB |
Output is correct |
20 |
Correct |
3 ms |
18268 KB |
Output is correct |
21 |
Correct |
3 ms |
18268 KB |
Output is correct |
22 |
Correct |
3 ms |
18424 KB |
Output is correct |
23 |
Correct |
3 ms |
18268 KB |
Output is correct |
24 |
Correct |
3 ms |
18268 KB |
Output is correct |
25 |
Correct |
4 ms |
18428 KB |
Output is correct |
26 |
Correct |
4 ms |
18524 KB |
Output is correct |
27 |
Correct |
4 ms |
18576 KB |
Output is correct |
28 |
Correct |
4 ms |
18524 KB |
Output is correct |
29 |
Correct |
4 ms |
18524 KB |
Output is correct |
30 |
Correct |
4 ms |
18524 KB |
Output is correct |
31 |
Correct |
3 ms |
18268 KB |
Output is correct |
32 |
Correct |
5 ms |
18780 KB |
Output is correct |
33 |
Correct |
3 ms |
18268 KB |
Output is correct |
34 |
Correct |
4 ms |
18524 KB |
Output is correct |
35 |
Correct |
4 ms |
18524 KB |
Output is correct |
36 |
Correct |
4 ms |
18520 KB |
Output is correct |
37 |
Correct |
4 ms |
18564 KB |
Output is correct |
38 |
Correct |
3 ms |
18268 KB |
Output is correct |
39 |
Correct |
5 ms |
18524 KB |
Output is correct |
40 |
Correct |
5 ms |
18520 KB |
Output is correct |
41 |
Correct |
4 ms |
18520 KB |
Output is correct |
42 |
Correct |
4 ms |
18520 KB |
Output is correct |
43 |
Correct |
4 ms |
18744 KB |
Output is correct |
44 |
Correct |
3 ms |
18268 KB |
Output is correct |
45 |
Correct |
4 ms |
18524 KB |
Output is correct |
46 |
Correct |
4 ms |
18524 KB |
Output is correct |
47 |
Correct |
3 ms |
18268 KB |
Output is correct |
48 |
Correct |
5 ms |
18524 KB |
Output is correct |
49 |
Correct |
4 ms |
18524 KB |
Output is correct |
50 |
Correct |
4 ms |
18524 KB |
Output is correct |
51 |
Correct |
4 ms |
18524 KB |
Output is correct |
52 |
Correct |
4 ms |
18436 KB |
Output is correct |
53 |
Correct |
4 ms |
18524 KB |
Output is correct |
54 |
Correct |
4 ms |
18524 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
18268 KB |
Output is correct |
2 |
Correct |
3 ms |
18268 KB |
Output is correct |
3 |
Correct |
3 ms |
18268 KB |
Output is correct |
4 |
Correct |
3 ms |
18268 KB |
Output is correct |
5 |
Correct |
3 ms |
18268 KB |
Output is correct |
6 |
Correct |
3 ms |
18264 KB |
Output is correct |
7 |
Correct |
3 ms |
18268 KB |
Output is correct |
8 |
Correct |
3 ms |
18268 KB |
Output is correct |
9 |
Correct |
3 ms |
18368 KB |
Output is correct |
10 |
Correct |
3 ms |
18268 KB |
Output is correct |
11 |
Correct |
3 ms |
18268 KB |
Output is correct |
12 |
Correct |
3 ms |
18268 KB |
Output is correct |
13 |
Correct |
3 ms |
18432 KB |
Output is correct |
14 |
Correct |
4 ms |
18268 KB |
Output is correct |
15 |
Correct |
4 ms |
18360 KB |
Output is correct |
16 |
Correct |
3 ms |
18268 KB |
Output is correct |
17 |
Correct |
3 ms |
18264 KB |
Output is correct |
18 |
Correct |
3 ms |
18372 KB |
Output is correct |
19 |
Correct |
3 ms |
18268 KB |
Output is correct |
20 |
Correct |
3 ms |
18268 KB |
Output is correct |
21 |
Correct |
3 ms |
18268 KB |
Output is correct |
22 |
Correct |
3 ms |
18424 KB |
Output is correct |
23 |
Correct |
3 ms |
18268 KB |
Output is correct |
24 |
Correct |
3 ms |
18268 KB |
Output is correct |
25 |
Correct |
5 ms |
18268 KB |
Output is correct |
26 |
Correct |
35 ms |
29900 KB |
Output is correct |
27 |
Correct |
46 ms |
29520 KB |
Output is correct |
28 |
Correct |
4 ms |
18524 KB |
Output is correct |
29 |
Correct |
3 ms |
18352 KB |
Output is correct |
30 |
Correct |
3 ms |
18268 KB |
Output is correct |
31 |
Correct |
51 ms |
29616 KB |
Output is correct |
32 |
Correct |
4 ms |
18520 KB |
Output is correct |
33 |
Correct |
72 ms |
36868 KB |
Output is correct |
34 |
Correct |
43 ms |
29532 KB |
Output is correct |
35 |
Correct |
4 ms |
18524 KB |
Output is correct |
36 |
Correct |
59 ms |
30424 KB |
Output is correct |
37 |
Correct |
4 ms |
18520 KB |
Output is correct |
38 |
Correct |
4 ms |
18520 KB |
Output is correct |
39 |
Correct |
43 ms |
29924 KB |
Output is correct |
40 |
Correct |
5 ms |
18780 KB |
Output is correct |
41 |
Correct |
44 ms |
29576 KB |
Output is correct |
42 |
Correct |
61 ms |
31828 KB |
Output is correct |
43 |
Correct |
3 ms |
18264 KB |
Output is correct |
44 |
Correct |
66 ms |
37132 KB |
Output is correct |
45 |
Correct |
62 ms |
33360 KB |
Output is correct |
46 |
Correct |
4 ms |
18524 KB |
Output is correct |
47 |
Correct |
5 ms |
18524 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
29900 KB |
Output is correct |
2 |
Correct |
33 ms |
30668 KB |
Output is correct |
3 |
Correct |
4 ms |
18524 KB |
Output is correct |
4 |
Correct |
4 ms |
18436 KB |
Output is correct |
5 |
Correct |
3 ms |
18264 KB |
Output is correct |
6 |
Correct |
3 ms |
18268 KB |
Output is correct |
7 |
Correct |
4 ms |
18524 KB |
Output is correct |
8 |
Correct |
48 ms |
30036 KB |
Output is correct |
9 |
Correct |
4 ms |
18524 KB |
Output is correct |
10 |
Correct |
44 ms |
29632 KB |
Output is correct |
11 |
Correct |
3 ms |
18268 KB |
Output is correct |
12 |
Correct |
50 ms |
29812 KB |
Output is correct |
13 |
Correct |
57 ms |
30032 KB |
Output is correct |
14 |
Correct |
43 ms |
30356 KB |
Output is correct |
15 |
Correct |
31 ms |
29780 KB |
Output is correct |
16 |
Correct |
4 ms |
18632 KB |
Output is correct |
17 |
Correct |
3 ms |
18268 KB |
Output is correct |
18 |
Correct |
38 ms |
30540 KB |
Output is correct |
19 |
Correct |
57 ms |
34136 KB |
Output is correct |
20 |
Correct |
4 ms |
18524 KB |
Output is correct |
21 |
Correct |
3 ms |
18268 KB |
Output is correct |
22 |
Correct |
37 ms |
30404 KB |
Output is correct |
23 |
Correct |
4 ms |
18524 KB |
Output is correct |
24 |
Correct |
51 ms |
29776 KB |
Output is correct |
25 |
Correct |
54 ms |
32896 KB |
Output is correct |
26 |
Correct |
3 ms |
18524 KB |
Output is correct |
27 |
Correct |
4 ms |
18680 KB |
Output is correct |
28 |
Correct |
4 ms |
18524 KB |
Output is correct |
29 |
Correct |
4 ms |
18520 KB |
Output is correct |
30 |
Correct |
4 ms |
18524 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
18268 KB |
Output is correct |
2 |
Correct |
3 ms |
18268 KB |
Output is correct |
3 |
Correct |
3 ms |
18268 KB |
Output is correct |
4 |
Correct |
3 ms |
18268 KB |
Output is correct |
5 |
Correct |
3 ms |
18268 KB |
Output is correct |
6 |
Correct |
3 ms |
18264 KB |
Output is correct |
7 |
Correct |
3 ms |
18268 KB |
Output is correct |
8 |
Correct |
3 ms |
18268 KB |
Output is correct |
9 |
Correct |
3 ms |
18368 KB |
Output is correct |
10 |
Correct |
3 ms |
18268 KB |
Output is correct |
11 |
Correct |
3 ms |
18268 KB |
Output is correct |
12 |
Correct |
3 ms |
18268 KB |
Output is correct |
13 |
Correct |
3 ms |
18432 KB |
Output is correct |
14 |
Correct |
4 ms |
18268 KB |
Output is correct |
15 |
Correct |
4 ms |
18360 KB |
Output is correct |
16 |
Correct |
3 ms |
18268 KB |
Output is correct |
17 |
Correct |
3 ms |
18264 KB |
Output is correct |
18 |
Correct |
3 ms |
18372 KB |
Output is correct |
19 |
Correct |
3 ms |
18268 KB |
Output is correct |
20 |
Correct |
3 ms |
18268 KB |
Output is correct |
21 |
Correct |
3 ms |
18268 KB |
Output is correct |
22 |
Correct |
3 ms |
18424 KB |
Output is correct |
23 |
Correct |
3 ms |
18268 KB |
Output is correct |
24 |
Correct |
3 ms |
18268 KB |
Output is correct |
25 |
Correct |
4 ms |
18428 KB |
Output is correct |
26 |
Correct |
4 ms |
18524 KB |
Output is correct |
27 |
Correct |
4 ms |
18576 KB |
Output is correct |
28 |
Correct |
4 ms |
18524 KB |
Output is correct |
29 |
Correct |
4 ms |
18524 KB |
Output is correct |
30 |
Correct |
4 ms |
18524 KB |
Output is correct |
31 |
Correct |
3 ms |
18268 KB |
Output is correct |
32 |
Correct |
5 ms |
18780 KB |
Output is correct |
33 |
Correct |
3 ms |
18268 KB |
Output is correct |
34 |
Correct |
4 ms |
18524 KB |
Output is correct |
35 |
Correct |
4 ms |
18524 KB |
Output is correct |
36 |
Correct |
4 ms |
18520 KB |
Output is correct |
37 |
Correct |
4 ms |
18564 KB |
Output is correct |
38 |
Correct |
3 ms |
18268 KB |
Output is correct |
39 |
Correct |
5 ms |
18524 KB |
Output is correct |
40 |
Correct |
5 ms |
18520 KB |
Output is correct |
41 |
Correct |
4 ms |
18520 KB |
Output is correct |
42 |
Correct |
4 ms |
18520 KB |
Output is correct |
43 |
Correct |
4 ms |
18744 KB |
Output is correct |
44 |
Correct |
3 ms |
18268 KB |
Output is correct |
45 |
Correct |
4 ms |
18524 KB |
Output is correct |
46 |
Correct |
4 ms |
18524 KB |
Output is correct |
47 |
Correct |
3 ms |
18268 KB |
Output is correct |
48 |
Correct |
5 ms |
18524 KB |
Output is correct |
49 |
Correct |
4 ms |
18524 KB |
Output is correct |
50 |
Correct |
4 ms |
18524 KB |
Output is correct |
51 |
Correct |
4 ms |
18524 KB |
Output is correct |
52 |
Correct |
4 ms |
18436 KB |
Output is correct |
53 |
Correct |
4 ms |
18524 KB |
Output is correct |
54 |
Correct |
4 ms |
18524 KB |
Output is correct |
55 |
Correct |
5 ms |
18268 KB |
Output is correct |
56 |
Correct |
35 ms |
29900 KB |
Output is correct |
57 |
Correct |
46 ms |
29520 KB |
Output is correct |
58 |
Correct |
4 ms |
18524 KB |
Output is correct |
59 |
Correct |
3 ms |
18352 KB |
Output is correct |
60 |
Correct |
3 ms |
18268 KB |
Output is correct |
61 |
Correct |
51 ms |
29616 KB |
Output is correct |
62 |
Correct |
4 ms |
18520 KB |
Output is correct |
63 |
Correct |
72 ms |
36868 KB |
Output is correct |
64 |
Correct |
43 ms |
29532 KB |
Output is correct |
65 |
Correct |
4 ms |
18524 KB |
Output is correct |
66 |
Correct |
59 ms |
30424 KB |
Output is correct |
67 |
Correct |
4 ms |
18520 KB |
Output is correct |
68 |
Correct |
4 ms |
18520 KB |
Output is correct |
69 |
Correct |
43 ms |
29924 KB |
Output is correct |
70 |
Correct |
5 ms |
18780 KB |
Output is correct |
71 |
Correct |
44 ms |
29576 KB |
Output is correct |
72 |
Correct |
61 ms |
31828 KB |
Output is correct |
73 |
Correct |
3 ms |
18264 KB |
Output is correct |
74 |
Correct |
66 ms |
37132 KB |
Output is correct |
75 |
Correct |
62 ms |
33360 KB |
Output is correct |
76 |
Correct |
4 ms |
18524 KB |
Output is correct |
77 |
Correct |
5 ms |
18524 KB |
Output is correct |
78 |
Correct |
37 ms |
29900 KB |
Output is correct |
79 |
Correct |
33 ms |
30668 KB |
Output is correct |
80 |
Correct |
4 ms |
18524 KB |
Output is correct |
81 |
Correct |
4 ms |
18436 KB |
Output is correct |
82 |
Correct |
3 ms |
18264 KB |
Output is correct |
83 |
Correct |
3 ms |
18268 KB |
Output is correct |
84 |
Correct |
4 ms |
18524 KB |
Output is correct |
85 |
Correct |
48 ms |
30036 KB |
Output is correct |
86 |
Correct |
4 ms |
18524 KB |
Output is correct |
87 |
Correct |
44 ms |
29632 KB |
Output is correct |
88 |
Correct |
3 ms |
18268 KB |
Output is correct |
89 |
Correct |
50 ms |
29812 KB |
Output is correct |
90 |
Correct |
57 ms |
30032 KB |
Output is correct |
91 |
Correct |
43 ms |
30356 KB |
Output is correct |
92 |
Correct |
31 ms |
29780 KB |
Output is correct |
93 |
Correct |
4 ms |
18632 KB |
Output is correct |
94 |
Correct |
3 ms |
18268 KB |
Output is correct |
95 |
Correct |
38 ms |
30540 KB |
Output is correct |
96 |
Correct |
57 ms |
34136 KB |
Output is correct |
97 |
Correct |
4 ms |
18524 KB |
Output is correct |
98 |
Correct |
3 ms |
18268 KB |
Output is correct |
99 |
Correct |
37 ms |
30404 KB |
Output is correct |
100 |
Correct |
4 ms |
18524 KB |
Output is correct |
101 |
Correct |
51 ms |
29776 KB |
Output is correct |
102 |
Correct |
54 ms |
32896 KB |
Output is correct |
103 |
Correct |
3 ms |
18524 KB |
Output is correct |
104 |
Correct |
4 ms |
18680 KB |
Output is correct |
105 |
Correct |
4 ms |
18524 KB |
Output is correct |
106 |
Correct |
4 ms |
18520 KB |
Output is correct |
107 |
Correct |
4 ms |
18524 KB |
Output is correct |
108 |
Correct |
255 ms |
85952 KB |
Output is correct |
109 |
Correct |
643 ms |
99408 KB |
Output is correct |
110 |
Correct |
632 ms |
110804 KB |
Output is correct |
111 |
Correct |
552 ms |
108596 KB |
Output is correct |
112 |
Correct |
523 ms |
98896 KB |
Output is correct |
113 |
Correct |
297 ms |
87100 KB |
Output is correct |
114 |
Correct |
355 ms |
81744 KB |
Output is correct |
115 |
Correct |
337 ms |
81384 KB |
Output is correct |
116 |
Correct |
376 ms |
83796 KB |
Output is correct |
117 |
Correct |
407 ms |
85536 KB |
Output is correct |
118 |
Correct |
369 ms |
81492 KB |
Output is correct |
119 |
Correct |
386 ms |
85584 KB |
Output is correct |
120 |
Correct |
506 ms |
99668 KB |
Output is correct |
121 |
Correct |
378 ms |
85588 KB |
Output is correct |
122 |
Correct |
406 ms |
85056 KB |
Output is correct |
123 |
Correct |
288 ms |
87228 KB |
Output is correct |
124 |
Correct |
415 ms |
84892 KB |
Output is correct |