#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#include "debug.h"
#else
#define debug(...) 0
#endif
#define pb push_back
#define i2 array<int, 2>
#define ll long long
const int N = 2e5 + 4;
int n, k, c[N], sz[N], root[N], par[N];
bool done[N], in[N];
vector<int> adj[N], V[N];
queue<int> q;
void fill_sz(int v, int p = 0) {
sz[v] = 1;
for (int u : adj[v]) {
if (u != p && !done[u]) {
fill_sz(u, v);
sz[v] += sz[u];
}
}
}
int find_cen(int v, int siz, int p = 0) {
for (int u : adj[v]) {
if (!done[u] && u != p && sz[u] > siz / 2) {
return find_cen(u, siz, v);
}
}
return v;
}
void fill(int v, int p = 0) {
in[c[v]] = 0;
par[v] = p;
root[v] = (p == 0 ? v : root[p]);
for (int u : adj[v]) {
if (u != p && !done[u]) {
fill(u, v);
}
}
}
int dfs(int v = 1) {
fill_sz(v);
v = find_cen(v, sz[v]);
fill(v);
done[v] = true;
q.push(c[v]);
int ans = 0;
while (q.size()) {
int col = q.front();
q.pop();
if (in[col] == 1) {
continue;
}
in[col] = 1;
for (int u : V[col]) {
if (root[u] != v) {
ans = k;
break;
}
if (par[u] != 0) {
q.push(c[par[u]]);
}
}
ans++;
}
for (int u : adj[v]) {
if (!done[u]) {
ans = min(ans, dfs(u));
}
}
return ans;
}
void solve() {
cin >> n >> k;
for (int i = 1; i < n; i++) {
int v, u;
cin >> v >> u;
adj[v].pb(u);
adj[u].pb(v);
}
for (int i = 1; i <= n; i++) {
cin >> c[i];
V[c[i]].pb(i);
}
cout << dfs() - 1;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// cin >> t;
while (t--) {
solve();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
12888 KB |
Output is correct |
2 |
Correct |
3 ms |
12892 KB |
Output is correct |
3 |
Correct |
3 ms |
12952 KB |
Output is correct |
4 |
Correct |
3 ms |
12888 KB |
Output is correct |
5 |
Correct |
2 ms |
12940 KB |
Output is correct |
6 |
Correct |
2 ms |
12892 KB |
Output is correct |
7 |
Correct |
2 ms |
12892 KB |
Output is correct |
8 |
Correct |
3 ms |
12892 KB |
Output is correct |
9 |
Correct |
3 ms |
12892 KB |
Output is correct |
10 |
Correct |
3 ms |
12892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
12888 KB |
Output is correct |
2 |
Correct |
3 ms |
12892 KB |
Output is correct |
3 |
Correct |
3 ms |
12952 KB |
Output is correct |
4 |
Correct |
3 ms |
12888 KB |
Output is correct |
5 |
Correct |
2 ms |
12940 KB |
Output is correct |
6 |
Correct |
2 ms |
12892 KB |
Output is correct |
7 |
Correct |
2 ms |
12892 KB |
Output is correct |
8 |
Correct |
3 ms |
12892 KB |
Output is correct |
9 |
Correct |
3 ms |
12892 KB |
Output is correct |
10 |
Correct |
3 ms |
12892 KB |
Output is correct |
11 |
Correct |
4 ms |
12892 KB |
Output is correct |
12 |
Correct |
3 ms |
12892 KB |
Output is correct |
13 |
Correct |
4 ms |
12888 KB |
Output is correct |
14 |
Correct |
4 ms |
12888 KB |
Output is correct |
15 |
Correct |
4 ms |
12960 KB |
Output is correct |
16 |
Correct |
3 ms |
12892 KB |
Output is correct |
17 |
Correct |
3 ms |
13044 KB |
Output is correct |
18 |
Correct |
4 ms |
13140 KB |
Output is correct |
19 |
Correct |
3 ms |
12888 KB |
Output is correct |
20 |
Correct |
5 ms |
12888 KB |
Output is correct |
21 |
Correct |
3 ms |
12888 KB |
Output is correct |
22 |
Correct |
3 ms |
13148 KB |
Output is correct |
23 |
Correct |
3 ms |
13148 KB |
Output is correct |
24 |
Correct |
4 ms |
12892 KB |
Output is correct |
25 |
Correct |
4 ms |
13152 KB |
Output is correct |
26 |
Correct |
4 ms |
13148 KB |
Output is correct |
27 |
Correct |
4 ms |
13144 KB |
Output is correct |
28 |
Correct |
4 ms |
13148 KB |
Output is correct |
29 |
Correct |
4 ms |
13144 KB |
Output is correct |
30 |
Correct |
3 ms |
13028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
313 ms |
39940 KB |
Output is correct |
2 |
Correct |
151 ms |
42064 KB |
Output is correct |
3 |
Correct |
320 ms |
41128 KB |
Output is correct |
4 |
Correct |
141 ms |
42064 KB |
Output is correct |
5 |
Correct |
283 ms |
38480 KB |
Output is correct |
6 |
Correct |
182 ms |
41812 KB |
Output is correct |
7 |
Correct |
303 ms |
38572 KB |
Output is correct |
8 |
Correct |
141 ms |
41672 KB |
Output is correct |
9 |
Correct |
339 ms |
36904 KB |
Output is correct |
10 |
Correct |
368 ms |
34388 KB |
Output is correct |
11 |
Correct |
473 ms |
37436 KB |
Output is correct |
12 |
Correct |
427 ms |
40020 KB |
Output is correct |
13 |
Correct |
380 ms |
34088 KB |
Output is correct |
14 |
Correct |
377 ms |
40384 KB |
Output is correct |
15 |
Correct |
374 ms |
39976 KB |
Output is correct |
16 |
Correct |
364 ms |
35028 KB |
Output is correct |
17 |
Correct |
416 ms |
35548 KB |
Output is correct |
18 |
Correct |
421 ms |
35756 KB |
Output is correct |
19 |
Correct |
393 ms |
38860 KB |
Output is correct |
20 |
Correct |
411 ms |
41216 KB |
Output is correct |
21 |
Correct |
3 ms |
12892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
12888 KB |
Output is correct |
2 |
Correct |
3 ms |
12892 KB |
Output is correct |
3 |
Correct |
3 ms |
12952 KB |
Output is correct |
4 |
Correct |
3 ms |
12888 KB |
Output is correct |
5 |
Correct |
2 ms |
12940 KB |
Output is correct |
6 |
Correct |
2 ms |
12892 KB |
Output is correct |
7 |
Correct |
2 ms |
12892 KB |
Output is correct |
8 |
Correct |
3 ms |
12892 KB |
Output is correct |
9 |
Correct |
3 ms |
12892 KB |
Output is correct |
10 |
Correct |
3 ms |
12892 KB |
Output is correct |
11 |
Correct |
4 ms |
12892 KB |
Output is correct |
12 |
Correct |
3 ms |
12892 KB |
Output is correct |
13 |
Correct |
4 ms |
12888 KB |
Output is correct |
14 |
Correct |
4 ms |
12888 KB |
Output is correct |
15 |
Correct |
4 ms |
12960 KB |
Output is correct |
16 |
Correct |
3 ms |
12892 KB |
Output is correct |
17 |
Correct |
3 ms |
13044 KB |
Output is correct |
18 |
Correct |
4 ms |
13140 KB |
Output is correct |
19 |
Correct |
3 ms |
12888 KB |
Output is correct |
20 |
Correct |
5 ms |
12888 KB |
Output is correct |
21 |
Correct |
3 ms |
12888 KB |
Output is correct |
22 |
Correct |
3 ms |
13148 KB |
Output is correct |
23 |
Correct |
3 ms |
13148 KB |
Output is correct |
24 |
Correct |
4 ms |
12892 KB |
Output is correct |
25 |
Correct |
4 ms |
13152 KB |
Output is correct |
26 |
Correct |
4 ms |
13148 KB |
Output is correct |
27 |
Correct |
4 ms |
13144 KB |
Output is correct |
28 |
Correct |
4 ms |
13148 KB |
Output is correct |
29 |
Correct |
4 ms |
13144 KB |
Output is correct |
30 |
Correct |
3 ms |
13028 KB |
Output is correct |
31 |
Correct |
313 ms |
39940 KB |
Output is correct |
32 |
Correct |
151 ms |
42064 KB |
Output is correct |
33 |
Correct |
320 ms |
41128 KB |
Output is correct |
34 |
Correct |
141 ms |
42064 KB |
Output is correct |
35 |
Correct |
283 ms |
38480 KB |
Output is correct |
36 |
Correct |
182 ms |
41812 KB |
Output is correct |
37 |
Correct |
303 ms |
38572 KB |
Output is correct |
38 |
Correct |
141 ms |
41672 KB |
Output is correct |
39 |
Correct |
339 ms |
36904 KB |
Output is correct |
40 |
Correct |
368 ms |
34388 KB |
Output is correct |
41 |
Correct |
473 ms |
37436 KB |
Output is correct |
42 |
Correct |
427 ms |
40020 KB |
Output is correct |
43 |
Correct |
380 ms |
34088 KB |
Output is correct |
44 |
Correct |
377 ms |
40384 KB |
Output is correct |
45 |
Correct |
374 ms |
39976 KB |
Output is correct |
46 |
Correct |
364 ms |
35028 KB |
Output is correct |
47 |
Correct |
416 ms |
35548 KB |
Output is correct |
48 |
Correct |
421 ms |
35756 KB |
Output is correct |
49 |
Correct |
393 ms |
38860 KB |
Output is correct |
50 |
Correct |
411 ms |
41216 KB |
Output is correct |
51 |
Correct |
3 ms |
12892 KB |
Output is correct |
52 |
Correct |
235 ms |
25240 KB |
Output is correct |
53 |
Correct |
280 ms |
25404 KB |
Output is correct |
54 |
Correct |
308 ms |
25260 KB |
Output is correct |
55 |
Correct |
293 ms |
25416 KB |
Output is correct |
56 |
Correct |
273 ms |
25280 KB |
Output is correct |
57 |
Correct |
307 ms |
25360 KB |
Output is correct |
58 |
Correct |
238 ms |
28456 KB |
Output is correct |
59 |
Correct |
203 ms |
28908 KB |
Output is correct |
60 |
Correct |
277 ms |
28148 KB |
Output is correct |
61 |
Correct |
237 ms |
27692 KB |
Output is correct |
62 |
Correct |
132 ms |
42064 KB |
Output is correct |
63 |
Correct |
152 ms |
41968 KB |
Output is correct |
64 |
Correct |
142 ms |
42072 KB |
Output is correct |
65 |
Correct |
138 ms |
42048 KB |
Output is correct |
66 |
Correct |
179 ms |
33948 KB |
Output is correct |
67 |
Correct |
196 ms |
33812 KB |
Output is correct |
68 |
Correct |
192 ms |
33932 KB |
Output is correct |
69 |
Correct |
162 ms |
33736 KB |
Output is correct |
70 |
Correct |
183 ms |
33848 KB |
Output is correct |
71 |
Correct |
184 ms |
33852 KB |
Output is correct |
72 |
Correct |
163 ms |
33852 KB |
Output is correct |
73 |
Correct |
175 ms |
32972 KB |
Output is correct |
74 |
Correct |
194 ms |
33952 KB |
Output is correct |
75 |
Correct |
228 ms |
33904 KB |
Output is correct |
76 |
Correct |
299 ms |
32088 KB |
Output is correct |
77 |
Correct |
238 ms |
30224 KB |
Output is correct |
78 |
Correct |
315 ms |
35512 KB |
Output is correct |
79 |
Correct |
395 ms |
33456 KB |
Output is correct |
80 |
Correct |
411 ms |
40788 KB |
Output is correct |
81 |
Correct |
372 ms |
36944 KB |
Output is correct |
82 |
Correct |
368 ms |
37460 KB |
Output is correct |
83 |
Correct |
382 ms |
33872 KB |
Output is correct |
84 |
Correct |
354 ms |
39896 KB |
Output is correct |
85 |
Correct |
324 ms |
38212 KB |
Output is correct |
86 |
Correct |
424 ms |
33484 KB |
Output is correct |
87 |
Correct |
373 ms |
35152 KB |
Output is correct |
88 |
Correct |
347 ms |
36340 KB |
Output is correct |
89 |
Correct |
359 ms |
32404 KB |
Output is correct |
90 |
Correct |
283 ms |
32184 KB |
Output is correct |
91 |
Correct |
319 ms |
34416 KB |
Output is correct |
92 |
Correct |
317 ms |
33108 KB |
Output is correct |
93 |
Correct |
319 ms |
33092 KB |
Output is correct |
94 |
Correct |
291 ms |
32340 KB |
Output is correct |
95 |
Correct |
364 ms |
33720 KB |
Output is correct |
96 |
Correct |
272 ms |
32404 KB |
Output is correct |
97 |
Correct |
283 ms |
34476 KB |
Output is correct |