#include <bits/stdc++.h>
using namespace std;
struct DSU {
vector<int> dad, sz;
DSU(int n = 0) : dad(n, -1), sz(n, 1) {}
int Find(int x) {
if (dad[x] == -1) return x;
return dad[x] = Find(dad[x]);
}
bool Union(int x, int y) {
x = Find(x), y = Find(y);
if (x == y) return false;
if (sz[x] < sz[y]) swap(x, y);
dad[y] = x;
sz[x] += sz[y];
return true;
}
void Reset(int node) {
dad[node] = -1;
sz[node] = 1;
}
};
struct Solver {
int n, k, ans;
DSU dsu;
vector<bool> vis;
vector<vector<int>> adj, city;
vector<int> col, sz, nodes, dad, cnt;
void Main() {
cin >> n >> k;
adj.resize(n);
for (int i = 0; i < n - 1; ++i) {
int a, b; cin >> a >> b; --a, --b;
adj[a].emplace_back(b);
adj[b].emplace_back(a);
}
col.resize(n);
cnt.resize(k);
for (int i = 0; i < n; ++i) {
cin >> col[i]; --col[i];
++cnt[col[i]];
}
city.resize(k);
dad.resize(n);
vis.resize(n);
dsu = DSU(k);
sz.resize(n);
ans = n;
Centroid(0);
cout << ans << endl;
}
void GetSizes(int node, int par) {
sz[node] = 1;
nodes.emplace_back(node);
for (int &x : adj[node]) {
if (x == par || vis[x]) continue;
GetSizes(x, node);
sz[node] += sz[x];
}
}
int GetCentroid(int node) {
nodes.clear();
GetSizes(node, -1);
while (true) {
bool br = true;
for (int &x : adj[node]) {
if (!vis[x] && sz[x] < sz[node] && 2 * sz[x] > (int)nodes.size()) {
br = false;
node = x;
break;
}
}
if (br) break;
}
return node;
}
void GetDads(int node, int par) {
dad[node] = par;
for (int &x : adj[node]) {
if (x == par || vis[x]) continue;
GetDads(x, node);
}
}
void Centroid(int node) {
node = GetCentroid(node);
vis[node] = true;
GetDads(node, -1);
for (int &x : nodes) {
city[col[x]].emplace_back(x);
}
vector<int> q;
for (int &x : city[col[node]]) {
if (x != node)
q.emplace_back(x);
}
int cnt_merges = 0;
bool all_nodes_of_col = (int)city[col[node]].size() == cnt[col[node]];
for (int it = 0; it < (int)q.size(); ++it) {
int x = q[it];
all_nodes_of_col &= (int)city[col[x]].size() == cnt[col[x]];
if (dsu.Union(col[x], col[dad[x]])) {
++cnt_merges;
for (int &y : city[col[dad[x]]]) {
q.emplace_back(y);
}
}
}
if (all_nodes_of_col) {
ans = min(ans, cnt_merges);
}
for (int &x : nodes) {
dsu.Reset(col[x]);
city[col[x]].clear();
}
for (int &x : adj[node]) {
if (!vis[x]) {
Centroid(x);
}
}
}
};
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
Solver solver;
solver.Main();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
6 ms |
512 KB |
Output is correct |
12 |
Correct |
7 ms |
512 KB |
Output is correct |
13 |
Correct |
7 ms |
512 KB |
Output is correct |
14 |
Correct |
6 ms |
512 KB |
Output is correct |
15 |
Correct |
7 ms |
512 KB |
Output is correct |
16 |
Correct |
7 ms |
512 KB |
Output is correct |
17 |
Correct |
6 ms |
512 KB |
Output is correct |
18 |
Correct |
7 ms |
512 KB |
Output is correct |
19 |
Correct |
6 ms |
512 KB |
Output is correct |
20 |
Correct |
7 ms |
512 KB |
Output is correct |
21 |
Correct |
6 ms |
512 KB |
Output is correct |
22 |
Correct |
7 ms |
640 KB |
Output is correct |
23 |
Correct |
7 ms |
640 KB |
Output is correct |
24 |
Correct |
7 ms |
512 KB |
Output is correct |
25 |
Correct |
8 ms |
640 KB |
Output is correct |
26 |
Correct |
7 ms |
640 KB |
Output is correct |
27 |
Correct |
9 ms |
640 KB |
Output is correct |
28 |
Correct |
7 ms |
640 KB |
Output is correct |
29 |
Correct |
7 ms |
512 KB |
Output is correct |
30 |
Correct |
7 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
854 ms |
29548 KB |
Output is correct |
2 |
Correct |
286 ms |
29804 KB |
Output is correct |
3 |
Correct |
866 ms |
29516 KB |
Output is correct |
4 |
Correct |
298 ms |
29932 KB |
Output is correct |
5 |
Correct |
818 ms |
27628 KB |
Output is correct |
6 |
Correct |
292 ms |
29676 KB |
Output is correct |
7 |
Correct |
820 ms |
27756 KB |
Output is correct |
8 |
Correct |
296 ms |
29292 KB |
Output is correct |
9 |
Correct |
1022 ms |
26064 KB |
Output is correct |
10 |
Correct |
971 ms |
24556 KB |
Output is correct |
11 |
Correct |
998 ms |
26220 KB |
Output is correct |
12 |
Correct |
1047 ms |
27652 KB |
Output is correct |
13 |
Correct |
992 ms |
24428 KB |
Output is correct |
14 |
Correct |
973 ms |
27884 KB |
Output is correct |
15 |
Correct |
1083 ms |
27756 KB |
Output is correct |
16 |
Correct |
989 ms |
24876 KB |
Output is correct |
17 |
Correct |
988 ms |
25196 KB |
Output is correct |
18 |
Correct |
983 ms |
25268 KB |
Output is correct |
19 |
Correct |
1042 ms |
27224 KB |
Output is correct |
20 |
Correct |
1020 ms |
28396 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
6 ms |
512 KB |
Output is correct |
12 |
Correct |
7 ms |
512 KB |
Output is correct |
13 |
Correct |
7 ms |
512 KB |
Output is correct |
14 |
Correct |
6 ms |
512 KB |
Output is correct |
15 |
Correct |
7 ms |
512 KB |
Output is correct |
16 |
Correct |
7 ms |
512 KB |
Output is correct |
17 |
Correct |
6 ms |
512 KB |
Output is correct |
18 |
Correct |
7 ms |
512 KB |
Output is correct |
19 |
Correct |
6 ms |
512 KB |
Output is correct |
20 |
Correct |
7 ms |
512 KB |
Output is correct |
21 |
Correct |
6 ms |
512 KB |
Output is correct |
22 |
Correct |
7 ms |
640 KB |
Output is correct |
23 |
Correct |
7 ms |
640 KB |
Output is correct |
24 |
Correct |
7 ms |
512 KB |
Output is correct |
25 |
Correct |
8 ms |
640 KB |
Output is correct |
26 |
Correct |
7 ms |
640 KB |
Output is correct |
27 |
Correct |
9 ms |
640 KB |
Output is correct |
28 |
Correct |
7 ms |
640 KB |
Output is correct |
29 |
Correct |
7 ms |
512 KB |
Output is correct |
30 |
Correct |
7 ms |
512 KB |
Output is correct |
31 |
Correct |
854 ms |
29548 KB |
Output is correct |
32 |
Correct |
286 ms |
29804 KB |
Output is correct |
33 |
Correct |
866 ms |
29516 KB |
Output is correct |
34 |
Correct |
298 ms |
29932 KB |
Output is correct |
35 |
Correct |
818 ms |
27628 KB |
Output is correct |
36 |
Correct |
292 ms |
29676 KB |
Output is correct |
37 |
Correct |
820 ms |
27756 KB |
Output is correct |
38 |
Correct |
296 ms |
29292 KB |
Output is correct |
39 |
Correct |
1022 ms |
26064 KB |
Output is correct |
40 |
Correct |
971 ms |
24556 KB |
Output is correct |
41 |
Correct |
998 ms |
26220 KB |
Output is correct |
42 |
Correct |
1047 ms |
27652 KB |
Output is correct |
43 |
Correct |
992 ms |
24428 KB |
Output is correct |
44 |
Correct |
973 ms |
27884 KB |
Output is correct |
45 |
Correct |
1083 ms |
27756 KB |
Output is correct |
46 |
Correct |
989 ms |
24876 KB |
Output is correct |
47 |
Correct |
988 ms |
25196 KB |
Output is correct |
48 |
Correct |
983 ms |
25268 KB |
Output is correct |
49 |
Correct |
1042 ms |
27224 KB |
Output is correct |
50 |
Correct |
1020 ms |
28396 KB |
Output is correct |
51 |
Correct |
5 ms |
384 KB |
Output is correct |
52 |
Correct |
560 ms |
18536 KB |
Output is correct |
53 |
Correct |
623 ms |
18564 KB |
Output is correct |
54 |
Correct |
627 ms |
18512 KB |
Output is correct |
55 |
Correct |
664 ms |
18672 KB |
Output is correct |
56 |
Correct |
632 ms |
18716 KB |
Output is correct |
57 |
Correct |
624 ms |
18540 KB |
Output is correct |
58 |
Correct |
688 ms |
21740 KB |
Output is correct |
59 |
Correct |
647 ms |
21484 KB |
Output is correct |
60 |
Correct |
784 ms |
21872 KB |
Output is correct |
61 |
Correct |
831 ms |
21872 KB |
Output is correct |
62 |
Correct |
286 ms |
29676 KB |
Output is correct |
63 |
Correct |
285 ms |
29804 KB |
Output is correct |
64 |
Correct |
302 ms |
29420 KB |
Output is correct |
65 |
Correct |
291 ms |
29676 KB |
Output is correct |
66 |
Correct |
489 ms |
24212 KB |
Output is correct |
67 |
Correct |
487 ms |
24296 KB |
Output is correct |
68 |
Correct |
487 ms |
24044 KB |
Output is correct |
69 |
Correct |
507 ms |
24168 KB |
Output is correct |
70 |
Correct |
489 ms |
24040 KB |
Output is correct |
71 |
Correct |
491 ms |
24232 KB |
Output is correct |
72 |
Correct |
484 ms |
24172 KB |
Output is correct |
73 |
Correct |
488 ms |
23532 KB |
Output is correct |
74 |
Correct |
494 ms |
24176 KB |
Output is correct |
75 |
Correct |
491 ms |
24044 KB |
Output is correct |
76 |
Correct |
743 ms |
24860 KB |
Output is correct |
77 |
Correct |
707 ms |
24044 KB |
Output is correct |
78 |
Correct |
1000 ms |
25196 KB |
Output is correct |
79 |
Correct |
1006 ms |
24172 KB |
Output is correct |
80 |
Correct |
1029 ms |
27948 KB |
Output is correct |
81 |
Correct |
1027 ms |
26092 KB |
Output is correct |
82 |
Correct |
1041 ms |
26224 KB |
Output is correct |
83 |
Correct |
1053 ms |
24428 KB |
Output is correct |
84 |
Correct |
1066 ms |
27628 KB |
Output is correct |
85 |
Correct |
1044 ms |
26804 KB |
Output is correct |
86 |
Correct |
1008 ms |
24172 KB |
Output is correct |
87 |
Correct |
1000 ms |
25324 KB |
Output is correct |
88 |
Correct |
851 ms |
25564 KB |
Output is correct |
89 |
Correct |
855 ms |
23284 KB |
Output is correct |
90 |
Correct |
860 ms |
23156 KB |
Output is correct |
91 |
Correct |
885 ms |
24560 KB |
Output is correct |
92 |
Correct |
859 ms |
23792 KB |
Output is correct |
93 |
Correct |
857 ms |
23788 KB |
Output is correct |
94 |
Correct |
862 ms |
23280 KB |
Output is correct |
95 |
Correct |
862 ms |
24228 KB |
Output is correct |
96 |
Correct |
863 ms |
23520 KB |
Output is correct |
97 |
Correct |
874 ms |
24560 KB |
Output is correct |