#include <bits/stdc++.h>
using namespace std;
#define ii pair<int, int>
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
const int N = 2e5 + 5;
int n, k;
vector<int> ad[N], in[N];
int col[N];
ii ed[N];
int sz[N], dd[N], cnt, cnt_col[N], pd[N], vis[N];
int vis_col[N];
int res = 1e9;
void find_size(int u, int par) {
sz[u] = 1;
cnt++;
for (int &v : ad[u]) {
if(v != par && !dd[v]) {
find_size(v, u);
sz[u] += sz[v];
//cerr << sz[v] << '\n';
}
}
}
int find_big(int u, int par) {
for (int &v : ad[u]) {
if(v != par && !dd[v] && sz[v] >= cnt / 2) {
return find_big(v, u);
}
}
return u;
}
void cen(int u) {
cnt = 0;
find_size(u, 0);
u = find_big(u, 0);
//cerr << u << ' ';
int root = u;
pd[u] = u;
function<void(int, int)> dfs = [&](int u, int par) {
vis[u] = root;
for (auto v : ad[u]) {
if(v != par && !dd[v]) {
pd[v] = u;
vis[v] = root;
dfs(v, u);
}
}
};
dfs(u, 0);
vector <int> ans;
queue <int> q;
q.push(u);
int dem = -1;
bool ok = 1;
while(q.size() && ok) {
int u = q.front();
q.pop();
dem++;
while(ok) {
if(vis[u] == 0) break;
if(vis_col[col[u]] == 0) {
dem -= cnt_col[col[u]];
ans.pb(col[u]);
vis_col[col[u]] = 1;
for (int &v : in[col[u]]) {
q.push(v);
if(vis[v] != root) {
ok = 0;
break;
}
}
}
vis[u] = 0;
u = pd[u];
if(u == root) break;
}
}
for (auto v : ans) vis_col[v] = 0;
if(dem == 0) {
res = min(res, (int)(ans.size()) - 1);
}
dd[u] = 1;
for (auto v : ad[u]) {
if(!dd[v]) {
cen(v);
}
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n >> k;
for (int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
ed[i] = {u, v};
ad[u].pb(v);
ad[v].pb(u);
}
for (int i = 1; i <= n; ++i) {
cin >> col[i];
cnt_col[col[i]]++;
in[col[i]].pb(i);
}
cen(1);
cout << res << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9868 KB |
Output is correct |
2 |
Correct |
7 ms |
9836 KB |
Output is correct |
3 |
Correct |
7 ms |
9836 KB |
Output is correct |
4 |
Correct |
7 ms |
9836 KB |
Output is correct |
5 |
Correct |
7 ms |
9836 KB |
Output is correct |
6 |
Correct |
7 ms |
9836 KB |
Output is correct |
7 |
Correct |
7 ms |
9836 KB |
Output is correct |
8 |
Correct |
7 ms |
9836 KB |
Output is correct |
9 |
Correct |
7 ms |
9836 KB |
Output is correct |
10 |
Correct |
7 ms |
9708 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9868 KB |
Output is correct |
2 |
Correct |
7 ms |
9836 KB |
Output is correct |
3 |
Correct |
7 ms |
9836 KB |
Output is correct |
4 |
Correct |
7 ms |
9836 KB |
Output is correct |
5 |
Correct |
7 ms |
9836 KB |
Output is correct |
6 |
Correct |
7 ms |
9836 KB |
Output is correct |
7 |
Correct |
7 ms |
9836 KB |
Output is correct |
8 |
Correct |
7 ms |
9836 KB |
Output is correct |
9 |
Correct |
7 ms |
9836 KB |
Output is correct |
10 |
Correct |
7 ms |
9708 KB |
Output is correct |
11 |
Correct |
9 ms |
9964 KB |
Output is correct |
12 |
Correct |
10 ms |
9964 KB |
Output is correct |
13 |
Correct |
9 ms |
9964 KB |
Output is correct |
14 |
Correct |
11 ms |
9964 KB |
Output is correct |
15 |
Correct |
9 ms |
9964 KB |
Output is correct |
16 |
Correct |
9 ms |
9964 KB |
Output is correct |
17 |
Correct |
8 ms |
10092 KB |
Output is correct |
18 |
Correct |
9 ms |
10112 KB |
Output is correct |
19 |
Correct |
9 ms |
9964 KB |
Output is correct |
20 |
Correct |
9 ms |
9964 KB |
Output is correct |
21 |
Correct |
9 ms |
9964 KB |
Output is correct |
22 |
Correct |
9 ms |
10092 KB |
Output is correct |
23 |
Correct |
9 ms |
10092 KB |
Output is correct |
24 |
Correct |
10 ms |
9964 KB |
Output is correct |
25 |
Correct |
9 ms |
9984 KB |
Output is correct |
26 |
Correct |
10 ms |
10240 KB |
Output is correct |
27 |
Correct |
9 ms |
10092 KB |
Output is correct |
28 |
Correct |
10 ms |
9964 KB |
Output is correct |
29 |
Correct |
9 ms |
9964 KB |
Output is correct |
30 |
Correct |
9 ms |
9964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
726 ms |
41408 KB |
Output is correct |
2 |
Correct |
236 ms |
41580 KB |
Output is correct |
3 |
Correct |
733 ms |
41068 KB |
Output is correct |
4 |
Correct |
243 ms |
41580 KB |
Output is correct |
5 |
Correct |
748 ms |
38764 KB |
Output is correct |
6 |
Correct |
231 ms |
41580 KB |
Output is correct |
7 |
Correct |
744 ms |
39020 KB |
Output is correct |
8 |
Correct |
244 ms |
41576 KB |
Output is correct |
9 |
Correct |
762 ms |
37612 KB |
Output is correct |
10 |
Correct |
757 ms |
37660 KB |
Output is correct |
11 |
Correct |
703 ms |
37808 KB |
Output is correct |
12 |
Correct |
737 ms |
39916 KB |
Output is correct |
13 |
Correct |
770 ms |
37580 KB |
Output is correct |
14 |
Correct |
725 ms |
40108 KB |
Output is correct |
15 |
Correct |
775 ms |
39852 KB |
Output is correct |
16 |
Correct |
752 ms |
37672 KB |
Output is correct |
17 |
Correct |
760 ms |
37740 KB |
Output is correct |
18 |
Correct |
786 ms |
37644 KB |
Output is correct |
19 |
Correct |
789 ms |
38948 KB |
Output is correct |
20 |
Correct |
702 ms |
40768 KB |
Output is correct |
21 |
Correct |
7 ms |
9708 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9868 KB |
Output is correct |
2 |
Correct |
7 ms |
9836 KB |
Output is correct |
3 |
Correct |
7 ms |
9836 KB |
Output is correct |
4 |
Correct |
7 ms |
9836 KB |
Output is correct |
5 |
Correct |
7 ms |
9836 KB |
Output is correct |
6 |
Correct |
7 ms |
9836 KB |
Output is correct |
7 |
Correct |
7 ms |
9836 KB |
Output is correct |
8 |
Correct |
7 ms |
9836 KB |
Output is correct |
9 |
Correct |
7 ms |
9836 KB |
Output is correct |
10 |
Correct |
7 ms |
9708 KB |
Output is correct |
11 |
Correct |
9 ms |
9964 KB |
Output is correct |
12 |
Correct |
10 ms |
9964 KB |
Output is correct |
13 |
Correct |
9 ms |
9964 KB |
Output is correct |
14 |
Correct |
11 ms |
9964 KB |
Output is correct |
15 |
Correct |
9 ms |
9964 KB |
Output is correct |
16 |
Correct |
9 ms |
9964 KB |
Output is correct |
17 |
Correct |
8 ms |
10092 KB |
Output is correct |
18 |
Correct |
9 ms |
10112 KB |
Output is correct |
19 |
Correct |
9 ms |
9964 KB |
Output is correct |
20 |
Correct |
9 ms |
9964 KB |
Output is correct |
21 |
Correct |
9 ms |
9964 KB |
Output is correct |
22 |
Correct |
9 ms |
10092 KB |
Output is correct |
23 |
Correct |
9 ms |
10092 KB |
Output is correct |
24 |
Correct |
10 ms |
9964 KB |
Output is correct |
25 |
Correct |
9 ms |
9984 KB |
Output is correct |
26 |
Correct |
10 ms |
10240 KB |
Output is correct |
27 |
Correct |
9 ms |
10092 KB |
Output is correct |
28 |
Correct |
10 ms |
9964 KB |
Output is correct |
29 |
Correct |
9 ms |
9964 KB |
Output is correct |
30 |
Correct |
9 ms |
9964 KB |
Output is correct |
31 |
Correct |
726 ms |
41408 KB |
Output is correct |
32 |
Correct |
236 ms |
41580 KB |
Output is correct |
33 |
Correct |
733 ms |
41068 KB |
Output is correct |
34 |
Correct |
243 ms |
41580 KB |
Output is correct |
35 |
Correct |
748 ms |
38764 KB |
Output is correct |
36 |
Correct |
231 ms |
41580 KB |
Output is correct |
37 |
Correct |
744 ms |
39020 KB |
Output is correct |
38 |
Correct |
244 ms |
41576 KB |
Output is correct |
39 |
Correct |
762 ms |
37612 KB |
Output is correct |
40 |
Correct |
757 ms |
37660 KB |
Output is correct |
41 |
Correct |
703 ms |
37808 KB |
Output is correct |
42 |
Correct |
737 ms |
39916 KB |
Output is correct |
43 |
Correct |
770 ms |
37580 KB |
Output is correct |
44 |
Correct |
725 ms |
40108 KB |
Output is correct |
45 |
Correct |
775 ms |
39852 KB |
Output is correct |
46 |
Correct |
752 ms |
37672 KB |
Output is correct |
47 |
Correct |
760 ms |
37740 KB |
Output is correct |
48 |
Correct |
786 ms |
37644 KB |
Output is correct |
49 |
Correct |
789 ms |
38948 KB |
Output is correct |
50 |
Correct |
702 ms |
40768 KB |
Output is correct |
51 |
Correct |
7 ms |
9708 KB |
Output is correct |
52 |
Correct |
574 ms |
27916 KB |
Output is correct |
53 |
Correct |
609 ms |
27796 KB |
Output is correct |
54 |
Correct |
584 ms |
27628 KB |
Output is correct |
55 |
Correct |
541 ms |
27560 KB |
Output is correct |
56 |
Correct |
576 ms |
27564 KB |
Output is correct |
57 |
Correct |
583 ms |
27852 KB |
Output is correct |
58 |
Correct |
507 ms |
31864 KB |
Output is correct |
59 |
Correct |
546 ms |
32108 KB |
Output is correct |
60 |
Correct |
676 ms |
30896 KB |
Output is correct |
61 |
Correct |
681 ms |
30936 KB |
Output is correct |
62 |
Correct |
228 ms |
41708 KB |
Output is correct |
63 |
Correct |
230 ms |
41580 KB |
Output is correct |
64 |
Correct |
227 ms |
41580 KB |
Output is correct |
65 |
Correct |
232 ms |
41700 KB |
Output is correct |
66 |
Correct |
448 ms |
35556 KB |
Output is correct |
67 |
Correct |
477 ms |
35556 KB |
Output is correct |
68 |
Correct |
467 ms |
35428 KB |
Output is correct |
69 |
Correct |
453 ms |
35428 KB |
Output is correct |
70 |
Correct |
470 ms |
35428 KB |
Output is correct |
71 |
Correct |
463 ms |
35300 KB |
Output is correct |
72 |
Correct |
469 ms |
35500 KB |
Output is correct |
73 |
Correct |
447 ms |
35556 KB |
Output is correct |
74 |
Correct |
486 ms |
35428 KB |
Output is correct |
75 |
Correct |
478 ms |
35556 KB |
Output is correct |
76 |
Correct |
679 ms |
34320 KB |
Output is correct |
77 |
Correct |
666 ms |
33924 KB |
Output is correct |
78 |
Correct |
772 ms |
37672 KB |
Output is correct |
79 |
Correct |
754 ms |
37672 KB |
Output is correct |
80 |
Correct |
785 ms |
40380 KB |
Output is correct |
81 |
Correct |
781 ms |
37740 KB |
Output is correct |
82 |
Correct |
748 ms |
37632 KB |
Output is correct |
83 |
Correct |
758 ms |
37644 KB |
Output is correct |
84 |
Correct |
780 ms |
39604 KB |
Output is correct |
85 |
Correct |
744 ms |
38428 KB |
Output is correct |
86 |
Correct |
738 ms |
37868 KB |
Output is correct |
87 |
Correct |
735 ms |
37664 KB |
Output is correct |
88 |
Correct |
642 ms |
36844 KB |
Output is correct |
89 |
Correct |
640 ms |
35308 KB |
Output is correct |
90 |
Correct |
649 ms |
35180 KB |
Output is correct |
91 |
Correct |
673 ms |
35308 KB |
Output is correct |
92 |
Correct |
669 ms |
35392 KB |
Output is correct |
93 |
Correct |
641 ms |
35436 KB |
Output is correct |
94 |
Correct |
662 ms |
35436 KB |
Output is correct |
95 |
Correct |
640 ms |
35308 KB |
Output is correct |
96 |
Correct |
675 ms |
35436 KB |
Output is correct |
97 |
Correct |
631 ms |
35436 KB |
Output is correct |