#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <random>
#include <iomanip>
#include <functional>
#include <cassert>
using namespace std;
typedef long long ll;
const int N = 3007;
int par[N];
int get(int a) {
return (a == par[a] ? a : par[a] = get(par[a]));
}
void join(int a, int b) {
a = get(a);
b = get(b);
par[a] = b;
}
vector <int> g[N];
int a[N];
int cnt[N][N];
int all[N];
int k;
void dfs(int u, int p) {
++cnt[u][a[u]];
for (auto v : g[u]) {
if (v != p) {
dfs(v, u);
for (int i = 0; i < k; ++i) {
cnt[u][i] += cnt[v][i];
}
}
}
bool ok = true;
for (int i = 0; i < k; ++i) {
ok &= (cnt[u][i] == 0 || cnt[u][i] == all[i]);
}
if (!ok && p != -1) {
join(u, p);
} else {
//cout << u << ' ' << p << endl;
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
int n;
cin >> n >> k;
for (int i = 0; i + 1 < n; ++i) {
int u, v;
cin >> u >> v;
--u, --v;
g[u].push_back(v);
g[v].push_back(u);
}
for (int i = 0; i < n; ++i) {
cin >> a[i];
--a[i];
++all[a[i]];
}
for (int i = 0; i < n; ++i) par[i] = i;
dfs(0, -1);
vector <int> deg(n);
for (int i = 0; i < n; ++i) {
for (int j : g[i]) {
if (get(i) != get(j)) {
++deg[get(i)];
}
}
}
int cnt = 0;
for (int i = 0; i < n; ++i) if (deg[i] == 1) ++cnt;
cout << (cnt + 1) / 2 << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
768 KB |
Output is correct |
3 |
Correct |
1 ms |
768 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
768 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
768 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
768 KB |
Output is correct |
12 |
Correct |
1 ms |
768 KB |
Output is correct |
13 |
Correct |
1 ms |
512 KB |
Output is correct |
14 |
Correct |
1 ms |
768 KB |
Output is correct |
15 |
Correct |
1 ms |
768 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
768 KB |
Output is correct |
19 |
Correct |
0 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
768 KB |
Output is correct |
22 |
Correct |
1 ms |
768 KB |
Output is correct |
23 |
Correct |
1 ms |
512 KB |
Output is correct |
24 |
Correct |
1 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
768 KB |
Output is correct |
3 |
Correct |
1 ms |
768 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
768 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
768 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
768 KB |
Output is correct |
12 |
Correct |
1 ms |
768 KB |
Output is correct |
13 |
Correct |
1 ms |
512 KB |
Output is correct |
14 |
Correct |
1 ms |
768 KB |
Output is correct |
15 |
Correct |
1 ms |
768 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
768 KB |
Output is correct |
19 |
Correct |
0 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
768 KB |
Output is correct |
22 |
Correct |
1 ms |
768 KB |
Output is correct |
23 |
Correct |
1 ms |
512 KB |
Output is correct |
24 |
Correct |
1 ms |
512 KB |
Output is correct |
25 |
Correct |
1 ms |
640 KB |
Output is correct |
26 |
Correct |
26 ms |
28024 KB |
Output is correct |
27 |
Correct |
9 ms |
13184 KB |
Output is correct |
28 |
Correct |
43 ms |
26232 KB |
Output is correct |
29 |
Correct |
34 ms |
26164 KB |
Output is correct |
30 |
Correct |
10 ms |
13952 KB |
Output is correct |
31 |
Correct |
0 ms |
640 KB |
Output is correct |
32 |
Correct |
57 ms |
36140 KB |
Output is correct |
33 |
Correct |
1 ms |
640 KB |
Output is correct |
34 |
Correct |
9 ms |
12672 KB |
Output is correct |
35 |
Correct |
33 ms |
26232 KB |
Output is correct |
36 |
Correct |
9 ms |
12928 KB |
Output is correct |
37 |
Correct |
24 ms |
21368 KB |
Output is correct |
38 |
Correct |
1 ms |
896 KB |
Output is correct |
39 |
Correct |
25 ms |
21504 KB |
Output is correct |
40 |
Correct |
8 ms |
12672 KB |
Output is correct |
41 |
Correct |
8 ms |
12672 KB |
Output is correct |
42 |
Correct |
17 ms |
12672 KB |
Output is correct |
43 |
Correct |
8 ms |
12928 KB |
Output is correct |
44 |
Correct |
1 ms |
896 KB |
Output is correct |
45 |
Correct |
38 ms |
35832 KB |
Output is correct |
46 |
Correct |
20 ms |
21632 KB |
Output is correct |
47 |
Correct |
1 ms |
768 KB |
Output is correct |
48 |
Correct |
15 ms |
16640 KB |
Output is correct |
49 |
Correct |
27 ms |
12408 KB |
Output is correct |
50 |
Correct |
46 ms |
26232 KB |
Output is correct |
51 |
Correct |
14 ms |
18560 KB |
Output is correct |
52 |
Correct |
9 ms |
12672 KB |
Output is correct |
53 |
Correct |
17 ms |
12672 KB |
Output is correct |
54 |
Correct |
8 ms |
12928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
768 KB |
Output is correct |
3 |
Correct |
1 ms |
768 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
768 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
768 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
768 KB |
Output is correct |
12 |
Correct |
1 ms |
768 KB |
Output is correct |
13 |
Correct |
1 ms |
512 KB |
Output is correct |
14 |
Correct |
1 ms |
768 KB |
Output is correct |
15 |
Correct |
1 ms |
768 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
768 KB |
Output is correct |
19 |
Correct |
0 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
768 KB |
Output is correct |
22 |
Correct |
1 ms |
768 KB |
Output is correct |
23 |
Correct |
1 ms |
512 KB |
Output is correct |
24 |
Correct |
1 ms |
512 KB |
Output is correct |
25 |
Correct |
1 ms |
640 KB |
Output is correct |
26 |
Runtime error |
2 ms |
640 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
27 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
2 ms |
640 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
768 KB |
Output is correct |
3 |
Correct |
1 ms |
768 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
768 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
768 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
768 KB |
Output is correct |
12 |
Correct |
1 ms |
768 KB |
Output is correct |
13 |
Correct |
1 ms |
512 KB |
Output is correct |
14 |
Correct |
1 ms |
768 KB |
Output is correct |
15 |
Correct |
1 ms |
768 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
768 KB |
Output is correct |
19 |
Correct |
0 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
768 KB |
Output is correct |
22 |
Correct |
1 ms |
768 KB |
Output is correct |
23 |
Correct |
1 ms |
512 KB |
Output is correct |
24 |
Correct |
1 ms |
512 KB |
Output is correct |
25 |
Correct |
1 ms |
640 KB |
Output is correct |
26 |
Correct |
26 ms |
28024 KB |
Output is correct |
27 |
Correct |
9 ms |
13184 KB |
Output is correct |
28 |
Correct |
43 ms |
26232 KB |
Output is correct |
29 |
Correct |
34 ms |
26164 KB |
Output is correct |
30 |
Correct |
10 ms |
13952 KB |
Output is correct |
31 |
Correct |
0 ms |
640 KB |
Output is correct |
32 |
Correct |
57 ms |
36140 KB |
Output is correct |
33 |
Correct |
1 ms |
640 KB |
Output is correct |
34 |
Correct |
9 ms |
12672 KB |
Output is correct |
35 |
Correct |
33 ms |
26232 KB |
Output is correct |
36 |
Correct |
9 ms |
12928 KB |
Output is correct |
37 |
Correct |
24 ms |
21368 KB |
Output is correct |
38 |
Correct |
1 ms |
896 KB |
Output is correct |
39 |
Correct |
25 ms |
21504 KB |
Output is correct |
40 |
Correct |
8 ms |
12672 KB |
Output is correct |
41 |
Correct |
8 ms |
12672 KB |
Output is correct |
42 |
Correct |
17 ms |
12672 KB |
Output is correct |
43 |
Correct |
8 ms |
12928 KB |
Output is correct |
44 |
Correct |
1 ms |
896 KB |
Output is correct |
45 |
Correct |
38 ms |
35832 KB |
Output is correct |
46 |
Correct |
20 ms |
21632 KB |
Output is correct |
47 |
Correct |
1 ms |
768 KB |
Output is correct |
48 |
Correct |
15 ms |
16640 KB |
Output is correct |
49 |
Correct |
27 ms |
12408 KB |
Output is correct |
50 |
Correct |
46 ms |
26232 KB |
Output is correct |
51 |
Correct |
14 ms |
18560 KB |
Output is correct |
52 |
Correct |
9 ms |
12672 KB |
Output is correct |
53 |
Correct |
17 ms |
12672 KB |
Output is correct |
54 |
Correct |
8 ms |
12928 KB |
Output is correct |
55 |
Correct |
1 ms |
640 KB |
Output is correct |
56 |
Runtime error |
2 ms |
640 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
57 |
Halted |
0 ms |
0 KB |
- |