#include <bits/stdc++.h>
#define dbg() cerr <<
#define name(x) (#x) << ": " << (x) << ' ' <<
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);
// dbg() "Begin: " << name(1 + node) endl;
// dbg() "Nodes: ";
for (int &x : nodes) {
// dbg() x << ' ';
city[col[x]].emplace_back(x);
}
// dbg() endl;
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);
// dbg() name(cnt_merges) name(1 + node) name(col[node]) endl;
}
for (int &x : nodes) {
dsu.Reset(col[x]);
city[col[x]].clear();
}
for (int &x : adj[node]) {
if (!vis[x]) {
Centroid(x);
}
}
// dbg() "End: " << name(node) endl;
}
};
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 |
336 KB |
Output is correct |
5 |
Correct |
4 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 |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
4 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 |
336 KB |
Output is correct |
5 |
Correct |
4 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 |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
4 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 |
6 ms |
512 KB |
Output is correct |
13 |
Correct |
6 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 |
6 ms |
512 KB |
Output is correct |
17 |
Correct |
6 ms |
512 KB |
Output is correct |
18 |
Correct |
6 ms |
512 KB |
Output is correct |
19 |
Correct |
6 ms |
512 KB |
Output is correct |
20 |
Correct |
6 ms |
512 KB |
Output is correct |
21 |
Correct |
6 ms |
512 KB |
Output is correct |
22 |
Correct |
6 ms |
640 KB |
Output is correct |
23 |
Correct |
7 ms |
640 KB |
Output is correct |
24 |
Correct |
7 ms |
768 KB |
Output is correct |
25 |
Correct |
7 ms |
640 KB |
Output is correct |
26 |
Correct |
7 ms |
640 KB |
Output is correct |
27 |
Correct |
7 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 |
722 ms |
29548 KB |
Output is correct |
2 |
Correct |
293 ms |
29676 KB |
Output is correct |
3 |
Correct |
992 ms |
29388 KB |
Output is correct |
4 |
Correct |
311 ms |
29804 KB |
Output is correct |
5 |
Correct |
864 ms |
27600 KB |
Output is correct |
6 |
Correct |
294 ms |
29676 KB |
Output is correct |
7 |
Correct |
846 ms |
27756 KB |
Output is correct |
8 |
Correct |
308 ms |
29292 KB |
Output is correct |
9 |
Correct |
1012 ms |
26012 KB |
Output is correct |
10 |
Correct |
1000 ms |
24684 KB |
Output is correct |
11 |
Correct |
1006 ms |
26092 KB |
Output is correct |
12 |
Correct |
1016 ms |
27628 KB |
Output is correct |
13 |
Correct |
999 ms |
24404 KB |
Output is correct |
14 |
Correct |
1020 ms |
27756 KB |
Output is correct |
15 |
Correct |
1004 ms |
27884 KB |
Output is correct |
16 |
Correct |
996 ms |
25068 KB |
Output is correct |
17 |
Correct |
990 ms |
25332 KB |
Output is correct |
18 |
Correct |
992 ms |
25428 KB |
Output is correct |
19 |
Correct |
1002 ms |
27116 KB |
Output is correct |
20 |
Correct |
1000 ms |
28268 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 |
336 KB |
Output is correct |
5 |
Correct |
4 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 |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
4 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 |
6 ms |
512 KB |
Output is correct |
13 |
Correct |
6 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 |
6 ms |
512 KB |
Output is correct |
17 |
Correct |
6 ms |
512 KB |
Output is correct |
18 |
Correct |
6 ms |
512 KB |
Output is correct |
19 |
Correct |
6 ms |
512 KB |
Output is correct |
20 |
Correct |
6 ms |
512 KB |
Output is correct |
21 |
Correct |
6 ms |
512 KB |
Output is correct |
22 |
Correct |
6 ms |
640 KB |
Output is correct |
23 |
Correct |
7 ms |
640 KB |
Output is correct |
24 |
Correct |
7 ms |
768 KB |
Output is correct |
25 |
Correct |
7 ms |
640 KB |
Output is correct |
26 |
Correct |
7 ms |
640 KB |
Output is correct |
27 |
Correct |
7 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 |
722 ms |
29548 KB |
Output is correct |
32 |
Correct |
293 ms |
29676 KB |
Output is correct |
33 |
Correct |
992 ms |
29388 KB |
Output is correct |
34 |
Correct |
311 ms |
29804 KB |
Output is correct |
35 |
Correct |
864 ms |
27600 KB |
Output is correct |
36 |
Correct |
294 ms |
29676 KB |
Output is correct |
37 |
Correct |
846 ms |
27756 KB |
Output is correct |
38 |
Correct |
308 ms |
29292 KB |
Output is correct |
39 |
Correct |
1012 ms |
26012 KB |
Output is correct |
40 |
Correct |
1000 ms |
24684 KB |
Output is correct |
41 |
Correct |
1006 ms |
26092 KB |
Output is correct |
42 |
Correct |
1016 ms |
27628 KB |
Output is correct |
43 |
Correct |
999 ms |
24404 KB |
Output is correct |
44 |
Correct |
1020 ms |
27756 KB |
Output is correct |
45 |
Correct |
1004 ms |
27884 KB |
Output is correct |
46 |
Correct |
996 ms |
25068 KB |
Output is correct |
47 |
Correct |
990 ms |
25332 KB |
Output is correct |
48 |
Correct |
992 ms |
25428 KB |
Output is correct |
49 |
Correct |
1002 ms |
27116 KB |
Output is correct |
50 |
Correct |
1000 ms |
28268 KB |
Output is correct |
51 |
Correct |
5 ms |
384 KB |
Output is correct |
52 |
Correct |
599 ms |
18668 KB |
Output is correct |
53 |
Correct |
631 ms |
18536 KB |
Output is correct |
54 |
Correct |
578 ms |
18412 KB |
Output is correct |
55 |
Correct |
580 ms |
18800 KB |
Output is correct |
56 |
Correct |
578 ms |
18588 KB |
Output is correct |
57 |
Correct |
541 ms |
18540 KB |
Output is correct |
58 |
Correct |
626 ms |
21612 KB |
Output is correct |
59 |
Correct |
558 ms |
21484 KB |
Output is correct |
60 |
Correct |
666 ms |
21744 KB |
Output is correct |
61 |
Correct |
707 ms |
21872 KB |
Output is correct |
62 |
Correct |
291 ms |
29932 KB |
Output is correct |
63 |
Correct |
290 ms |
29784 KB |
Output is correct |
64 |
Correct |
301 ms |
29420 KB |
Output is correct |
65 |
Correct |
286 ms |
29676 KB |
Output is correct |
66 |
Correct |
415 ms |
24044 KB |
Output is correct |
67 |
Correct |
417 ms |
24520 KB |
Output is correct |
68 |
Correct |
411 ms |
24172 KB |
Output is correct |
69 |
Correct |
418 ms |
24044 KB |
Output is correct |
70 |
Correct |
423 ms |
24172 KB |
Output is correct |
71 |
Correct |
404 ms |
24048 KB |
Output is correct |
72 |
Correct |
426 ms |
24168 KB |
Output is correct |
73 |
Correct |
422 ms |
23656 KB |
Output is correct |
74 |
Correct |
433 ms |
24300 KB |
Output is correct |
75 |
Correct |
419 ms |
24044 KB |
Output is correct |
76 |
Correct |
682 ms |
24812 KB |
Output is correct |
77 |
Correct |
638 ms |
24044 KB |
Output is correct |
78 |
Correct |
856 ms |
25184 KB |
Output is correct |
79 |
Correct |
852 ms |
24172 KB |
Output is correct |
80 |
Correct |
866 ms |
28140 KB |
Output is correct |
81 |
Correct |
913 ms |
26348 KB |
Output is correct |
82 |
Correct |
846 ms |
26220 KB |
Output is correct |
83 |
Correct |
834 ms |
24300 KB |
Output is correct |
84 |
Correct |
849 ms |
27628 KB |
Output is correct |
85 |
Correct |
849 ms |
26604 KB |
Output is correct |
86 |
Correct |
858 ms |
24044 KB |
Output is correct |
87 |
Correct |
838 ms |
25068 KB |
Output is correct |
88 |
Correct |
717 ms |
25456 KB |
Output is correct |
89 |
Correct |
740 ms |
23408 KB |
Output is correct |
90 |
Correct |
781 ms |
23304 KB |
Output is correct |
91 |
Correct |
811 ms |
24592 KB |
Output is correct |
92 |
Correct |
791 ms |
23776 KB |
Output is correct |
93 |
Correct |
806 ms |
23668 KB |
Output is correct |
94 |
Correct |
780 ms |
23284 KB |
Output is correct |
95 |
Correct |
796 ms |
24152 KB |
Output is correct |
96 |
Correct |
859 ms |
23248 KB |
Output is correct |
97 |
Correct |
823 ms |
24520 KB |
Output is correct |