/**
_ _ __ _ _ _ _ _ _
|a ||t ||o d | |o |
| __ _| | _ | __| _ |
| __ |/_ | __ /__\ / _\|
**/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N_MAX = 200000;
int N, K;
vector <int> adj[N_MAX + 2];
int C[N_MAX + 2];
bool erased[N_MAX + 2];
vector <int> nodes;
int parent[N_MAX + 2];
int sub[N_MAX + 2];
bool in_comp[N_MAX + 2];
vector <int> with_color[N_MAX + 2];
void dfs (int u, bool first) {
if (first == true) {
nodes.push_back(u);
in_comp[u] = true;
}
sub[u] = 1;
for (int v : adj[u]) {
if (v != parent[u] && erased[v] == false) {
parent[v] = u;
dfs(v, first);
sub[u] += sub[v];
}
}
}
int answer = INT_MAX;
bool seen[N_MAX + 2];
bool cleared[N_MAX + 2];
void solve (int root) {
parent[root] = 0; dfs(root, true);
int centroid = 0;
for (int u : nodes) {
if (sub[root] - sub[u] > (int) nodes.size() / 2) {
continue;
}
bool ok = true;
for (int v : adj[u]) {
if (v != parent[u] && erased[v] == false && sub[v] > (int) nodes.size() / 2) {
ok = false;
break;
}
}
if (ok == true) {
centroid = u;
break;
}
}
root = centroid;
parent[root] = 0; dfs(root, false);
queue <int> q;
q.push(C[root]);
cleared[C[root]] = true;
int merges = 0;
while (q.empty() == false) {
int c = q.front(); q.pop();
for (int u : with_color[c]) {
if (in_comp[u] == false) {
merges = INT_MAX;
break;
}
while (u != 0 && seen[u] == false) {
if (cleared[C[u]] == false) {
cleared[C[u]] = true;
q.push(C[u]);
merges++;
}
seen[u] = true;
u = parent[u];
}
}
}
answer = min(answer, merges);
for (int u : nodes) {
in_comp[u] = false;
seen[u] = false;
cleared[C[u]] = false;
}
nodes.clear();
erased[root] = true;
for (int u : adj[root]) {
if (erased[u] == false) {
solve(u);
}
}
}
int main () {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> N >> K;
for (int i = 1; i <= N - 1; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int u = 1; u <= N; u++) {
cin >> C[u];
with_color[C[u]].push_back(u);
}
solve(1);
cout << answer << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
12636 KB |
Output is correct |
2 |
Correct |
3 ms |
12636 KB |
Output is correct |
3 |
Correct |
3 ms |
12636 KB |
Output is correct |
4 |
Correct |
3 ms |
12636 KB |
Output is correct |
5 |
Correct |
2 ms |
12804 KB |
Output is correct |
6 |
Correct |
2 ms |
12636 KB |
Output is correct |
7 |
Correct |
3 ms |
12636 KB |
Output is correct |
8 |
Correct |
2 ms |
12636 KB |
Output is correct |
9 |
Correct |
2 ms |
12636 KB |
Output is correct |
10 |
Correct |
2 ms |
12636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
12636 KB |
Output is correct |
2 |
Correct |
3 ms |
12636 KB |
Output is correct |
3 |
Correct |
3 ms |
12636 KB |
Output is correct |
4 |
Correct |
3 ms |
12636 KB |
Output is correct |
5 |
Correct |
2 ms |
12804 KB |
Output is correct |
6 |
Correct |
2 ms |
12636 KB |
Output is correct |
7 |
Correct |
3 ms |
12636 KB |
Output is correct |
8 |
Correct |
2 ms |
12636 KB |
Output is correct |
9 |
Correct |
2 ms |
12636 KB |
Output is correct |
10 |
Correct |
2 ms |
12636 KB |
Output is correct |
11 |
Correct |
4 ms |
12888 KB |
Output is correct |
12 |
Correct |
4 ms |
12892 KB |
Output is correct |
13 |
Correct |
4 ms |
12892 KB |
Output is correct |
14 |
Correct |
5 ms |
12892 KB |
Output is correct |
15 |
Correct |
3 ms |
12892 KB |
Output is correct |
16 |
Correct |
4 ms |
12892 KB |
Output is correct |
17 |
Correct |
3 ms |
12892 KB |
Output is correct |
18 |
Correct |
3 ms |
12892 KB |
Output is correct |
19 |
Correct |
3 ms |
12892 KB |
Output is correct |
20 |
Correct |
3 ms |
12892 KB |
Output is correct |
21 |
Correct |
3 ms |
12892 KB |
Output is correct |
22 |
Correct |
3 ms |
12892 KB |
Output is correct |
23 |
Correct |
4 ms |
12888 KB |
Output is correct |
24 |
Correct |
4 ms |
12892 KB |
Output is correct |
25 |
Correct |
4 ms |
12892 KB |
Output is correct |
26 |
Correct |
4 ms |
12892 KB |
Output is correct |
27 |
Correct |
4 ms |
12892 KB |
Output is correct |
28 |
Correct |
4 ms |
12892 KB |
Output is correct |
29 |
Correct |
4 ms |
12888 KB |
Output is correct |
30 |
Correct |
4 ms |
12892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
331 ms |
41912 KB |
Output is correct |
2 |
Correct |
154 ms |
42444 KB |
Output is correct |
3 |
Correct |
319 ms |
41676 KB |
Output is correct |
4 |
Correct |
149 ms |
42312 KB |
Output is correct |
5 |
Correct |
308 ms |
38568 KB |
Output is correct |
6 |
Correct |
150 ms |
42464 KB |
Output is correct |
7 |
Correct |
281 ms |
39056 KB |
Output is correct |
8 |
Correct |
148 ms |
42184 KB |
Output is correct |
9 |
Correct |
366 ms |
37148 KB |
Output is correct |
10 |
Correct |
341 ms |
34760 KB |
Output is correct |
11 |
Correct |
355 ms |
37616 KB |
Output is correct |
12 |
Correct |
350 ms |
40104 KB |
Output is correct |
13 |
Correct |
353 ms |
34504 KB |
Output is correct |
14 |
Correct |
351 ms |
40716 KB |
Output is correct |
15 |
Correct |
334 ms |
40200 KB |
Output is correct |
16 |
Correct |
347 ms |
35464 KB |
Output is correct |
17 |
Correct |
330 ms |
35780 KB |
Output is correct |
18 |
Correct |
337 ms |
36036 KB |
Output is correct |
19 |
Correct |
373 ms |
39064 KB |
Output is correct |
20 |
Correct |
344 ms |
41388 KB |
Output is correct |
21 |
Correct |
2 ms |
12636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
12636 KB |
Output is correct |
2 |
Correct |
3 ms |
12636 KB |
Output is correct |
3 |
Correct |
3 ms |
12636 KB |
Output is correct |
4 |
Correct |
3 ms |
12636 KB |
Output is correct |
5 |
Correct |
2 ms |
12804 KB |
Output is correct |
6 |
Correct |
2 ms |
12636 KB |
Output is correct |
7 |
Correct |
3 ms |
12636 KB |
Output is correct |
8 |
Correct |
2 ms |
12636 KB |
Output is correct |
9 |
Correct |
2 ms |
12636 KB |
Output is correct |
10 |
Correct |
2 ms |
12636 KB |
Output is correct |
11 |
Correct |
4 ms |
12888 KB |
Output is correct |
12 |
Correct |
4 ms |
12892 KB |
Output is correct |
13 |
Correct |
4 ms |
12892 KB |
Output is correct |
14 |
Correct |
5 ms |
12892 KB |
Output is correct |
15 |
Correct |
3 ms |
12892 KB |
Output is correct |
16 |
Correct |
4 ms |
12892 KB |
Output is correct |
17 |
Correct |
3 ms |
12892 KB |
Output is correct |
18 |
Correct |
3 ms |
12892 KB |
Output is correct |
19 |
Correct |
3 ms |
12892 KB |
Output is correct |
20 |
Correct |
3 ms |
12892 KB |
Output is correct |
21 |
Correct |
3 ms |
12892 KB |
Output is correct |
22 |
Correct |
3 ms |
12892 KB |
Output is correct |
23 |
Correct |
4 ms |
12888 KB |
Output is correct |
24 |
Correct |
4 ms |
12892 KB |
Output is correct |
25 |
Correct |
4 ms |
12892 KB |
Output is correct |
26 |
Correct |
4 ms |
12892 KB |
Output is correct |
27 |
Correct |
4 ms |
12892 KB |
Output is correct |
28 |
Correct |
4 ms |
12892 KB |
Output is correct |
29 |
Correct |
4 ms |
12888 KB |
Output is correct |
30 |
Correct |
4 ms |
12892 KB |
Output is correct |
31 |
Correct |
331 ms |
41912 KB |
Output is correct |
32 |
Correct |
154 ms |
42444 KB |
Output is correct |
33 |
Correct |
319 ms |
41676 KB |
Output is correct |
34 |
Correct |
149 ms |
42312 KB |
Output is correct |
35 |
Correct |
308 ms |
38568 KB |
Output is correct |
36 |
Correct |
150 ms |
42464 KB |
Output is correct |
37 |
Correct |
281 ms |
39056 KB |
Output is correct |
38 |
Correct |
148 ms |
42184 KB |
Output is correct |
39 |
Correct |
366 ms |
37148 KB |
Output is correct |
40 |
Correct |
341 ms |
34760 KB |
Output is correct |
41 |
Correct |
355 ms |
37616 KB |
Output is correct |
42 |
Correct |
350 ms |
40104 KB |
Output is correct |
43 |
Correct |
353 ms |
34504 KB |
Output is correct |
44 |
Correct |
351 ms |
40716 KB |
Output is correct |
45 |
Correct |
334 ms |
40200 KB |
Output is correct |
46 |
Correct |
347 ms |
35464 KB |
Output is correct |
47 |
Correct |
330 ms |
35780 KB |
Output is correct |
48 |
Correct |
337 ms |
36036 KB |
Output is correct |
49 |
Correct |
373 ms |
39064 KB |
Output is correct |
50 |
Correct |
344 ms |
41388 KB |
Output is correct |
51 |
Correct |
2 ms |
12636 KB |
Output is correct |
52 |
Correct |
226 ms |
25296 KB |
Output is correct |
53 |
Correct |
232 ms |
25452 KB |
Output is correct |
54 |
Correct |
216 ms |
25292 KB |
Output is correct |
55 |
Correct |
252 ms |
25296 KB |
Output is correct |
56 |
Correct |
221 ms |
25420 KB |
Output is correct |
57 |
Correct |
232 ms |
25404 KB |
Output is correct |
58 |
Correct |
254 ms |
28916 KB |
Output is correct |
59 |
Correct |
213 ms |
29416 KB |
Output is correct |
60 |
Correct |
269 ms |
28652 KB |
Output is correct |
61 |
Incorrect |
266 ms |
28336 KB |
Output isn't correct |
62 |
Halted |
0 ms |
0 KB |
- |