#include <bits/stdc++.h>
#ifdef __LOCAL__
#include <debug_local.h>
#endif
using namespace std;
const int mxN = 2e5 + 5;
vector<int> ad[mxN], l[mxN];
int city[mxN];
int ans = mxN;
int sz[mxN], par[mxN];
bool deleted[mxN], visc[mxN];
vector<int> vec;
void get_size(int u, int p = -1) {
vec.push_back(u);
visc[city[u]] = false;
sz[u] = 1;
for (int v : ad[u]) {
if (v == p || deleted[v]) continue;
get_size(v, u);
par[v] = u;
sz[u] += sz[v];
}
}
int get_centroid(int u, int p, int n) {
for (int v : ad[u]) {
if (v == p || deleted[v]) continue;
if (sz[v] > n / 2) return get_centroid(v, u, n);
}
return u;
}
void solve(int s) {
get_size(s);
int c = get_centroid(s, -1, sz[s]);
vec.clear();
get_size(c);
sort(vec.begin(), vec.end());
int cur = 0;
queue<int> q;
visc[city[c]] = true;
q.push(city[c]);
while (!q.empty()) {
cur++;
int u = q.front();
q.pop();
for (int v : l[u]) {
if (!binary_search(vec.begin(), vec.end(), v)) cur = mxN;
else {
if (v != c && !visc[city[par[v]]]) {
q.push(city[par[v]]);
visc[city[par[v]]] = true;
}
}
}
}
ans = min(ans, cur);
deleted[c] = true;
for (int v : ad[c]) {
if (!deleted[v]) solve(v);
}
}
void testCase() {
int n, k;
cin >> n >> k;
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
ad[u].push_back(v);
ad[v].push_back(u);
}
for (int i = 1; i <= n; i++) {
cin >> city[i];
l[city[i]].push_back(i);
}
solve(1);
cout << ans - 1 << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
testCase();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9684 KB |
Output is correct |
3 |
Correct |
6 ms |
9660 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9748 KB |
Output is correct |
6 |
Correct |
6 ms |
9684 KB |
Output is correct |
7 |
Correct |
5 ms |
9728 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
5 ms |
9724 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9684 KB |
Output is correct |
3 |
Correct |
6 ms |
9660 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9748 KB |
Output is correct |
6 |
Correct |
6 ms |
9684 KB |
Output is correct |
7 |
Correct |
5 ms |
9728 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
5 ms |
9724 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
8 ms |
9904 KB |
Output is correct |
12 |
Correct |
8 ms |
9812 KB |
Output is correct |
13 |
Correct |
8 ms |
9812 KB |
Output is correct |
14 |
Correct |
8 ms |
9812 KB |
Output is correct |
15 |
Correct |
8 ms |
9940 KB |
Output is correct |
16 |
Correct |
8 ms |
9812 KB |
Output is correct |
17 |
Correct |
12 ms |
9940 KB |
Output is correct |
18 |
Correct |
12 ms |
9940 KB |
Output is correct |
19 |
Correct |
11 ms |
9980 KB |
Output is correct |
20 |
Correct |
13 ms |
9984 KB |
Output is correct |
21 |
Correct |
12 ms |
9984 KB |
Output is correct |
22 |
Correct |
7 ms |
10012 KB |
Output is correct |
23 |
Correct |
7 ms |
9940 KB |
Output is correct |
24 |
Correct |
7 ms |
9940 KB |
Output is correct |
25 |
Correct |
9 ms |
9908 KB |
Output is correct |
26 |
Correct |
11 ms |
9940 KB |
Output is correct |
27 |
Correct |
9 ms |
9940 KB |
Output is correct |
28 |
Correct |
10 ms |
9940 KB |
Output is correct |
29 |
Correct |
10 ms |
9932 KB |
Output is correct |
30 |
Correct |
10 ms |
9940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
706 ms |
38884 KB |
Output is correct |
2 |
Correct |
305 ms |
39264 KB |
Output is correct |
3 |
Correct |
626 ms |
42224 KB |
Output is correct |
4 |
Correct |
300 ms |
42960 KB |
Output is correct |
5 |
Correct |
1654 ms |
39240 KB |
Output is correct |
6 |
Correct |
361 ms |
42924 KB |
Output is correct |
7 |
Execution timed out |
3079 ms |
39560 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9684 KB |
Output is correct |
3 |
Correct |
6 ms |
9660 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9748 KB |
Output is correct |
6 |
Correct |
6 ms |
9684 KB |
Output is correct |
7 |
Correct |
5 ms |
9728 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
5 ms |
9724 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
8 ms |
9904 KB |
Output is correct |
12 |
Correct |
8 ms |
9812 KB |
Output is correct |
13 |
Correct |
8 ms |
9812 KB |
Output is correct |
14 |
Correct |
8 ms |
9812 KB |
Output is correct |
15 |
Correct |
8 ms |
9940 KB |
Output is correct |
16 |
Correct |
8 ms |
9812 KB |
Output is correct |
17 |
Correct |
12 ms |
9940 KB |
Output is correct |
18 |
Correct |
12 ms |
9940 KB |
Output is correct |
19 |
Correct |
11 ms |
9980 KB |
Output is correct |
20 |
Correct |
13 ms |
9984 KB |
Output is correct |
21 |
Correct |
12 ms |
9984 KB |
Output is correct |
22 |
Correct |
7 ms |
10012 KB |
Output is correct |
23 |
Correct |
7 ms |
9940 KB |
Output is correct |
24 |
Correct |
7 ms |
9940 KB |
Output is correct |
25 |
Correct |
9 ms |
9908 KB |
Output is correct |
26 |
Correct |
11 ms |
9940 KB |
Output is correct |
27 |
Correct |
9 ms |
9940 KB |
Output is correct |
28 |
Correct |
10 ms |
9940 KB |
Output is correct |
29 |
Correct |
10 ms |
9932 KB |
Output is correct |
30 |
Correct |
10 ms |
9940 KB |
Output is correct |
31 |
Correct |
706 ms |
38884 KB |
Output is correct |
32 |
Correct |
305 ms |
39264 KB |
Output is correct |
33 |
Correct |
626 ms |
42224 KB |
Output is correct |
34 |
Correct |
300 ms |
42960 KB |
Output is correct |
35 |
Correct |
1654 ms |
39240 KB |
Output is correct |
36 |
Correct |
361 ms |
42924 KB |
Output is correct |
37 |
Execution timed out |
3079 ms |
39560 KB |
Time limit exceeded |
38 |
Halted |
0 ms |
0 KB |
- |