// #pragma GCC optimize("Ofast,unroll-loops")
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma,tune=native")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef int ll;
typedef long double ld;
typedef pair <ll, ll> pll;
#ifdef SINA
#define dbg(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << std::endl; }
template <typename Arg1, typename... Args> void __f (const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...); }
#define dbg2(x, j, n) cout<< #x << " : "; output((j), (n), x, 1); cout.flush();
#else
#define dbg(...) 0
#define dbg2(x, j, n) 0
#endif
#define SZ(x) ((ll)((x).size()))
#define File(s, t) freopen(s ".txt", "r", stdin); freopen(t ".txt", "w", stdout);
#define input(j, n, a) for (int _i = (j); _i < (n)+(j); _i++) cin>> a[_i];
#define output(j, n, a, t) for (int _i = (j); _i < (n)+(j); _i++) cout<< a[_i] << (((t) && _i != (n)+(j)-1)? ' ' : '\n');
#define kill(x) return cout<< (x) << endl, 0
#define cl const ll
#define fr first
#define sc second
#define lc (v << 1)
#define rc (lc | 1)
#define mid ((l + r) >> 1)
#define All(x) (x).begin(), (x).end()
cl inf = sizeof(ll) == 4 ? (1e9 + 10) : (3e18), mod = 1e9 + 7, MOD = 998244353;
template <class A,class B> ostream& operator << (ostream& out,const pair<A,B>&a){return out<<'('<<a.first<<", "<<a.second<<')';}
template <class A> ostream& operator << (ostream& out, const vector<A> &a) {
out<< '['; for (int i = -1; ++i < int(a.size());) out<< a[i] << (i + 1 < int(a.size()) ? ", " : ""); return out<<']'; }
template <class T, typename _t = less <T> > using Tree = tree <T, null_type, _t, rb_tree_tag, tree_order_statistics_node_update>;
cl N = 2e5 + 7;
ll C [N], sz [N], mark [N], q [N], par [N], iter [N], R, n, k, sum, ans, tar;
bool hide [N], hide_color [N];
vector <ll> adj [N], col [N];
ll plant (cl &v, cl &pr = 0) {
sz[v] = 1;
for (auto &u : adj[v]) if (!hide[u] && u != pr) sz[v] += plant(u, v);
return sz[v];
}
inline ll find_centroid (ll v, cl &S, ll pr = 0) {
for (ll i = 0; i < SZ(adj[v]); i++) {
ll &u = adj[v][i];
if (sz[u] << 1 > S && !hide[u] && u != pr) pr = v, v = u, i = -1;
}
return v;
}
void dfs (cl &v, cl &pr = 0) {
par[v] = pr;
iter[v] = tar;
for (auto &u : adj[v]) if (!hide_color[C[u]] && u != pr) dfs(u, v);
}
void centroid_decomposition (ll v = 1) {
v = find_centroid(v, plant(v));
if (!hide_color[C[v]]) {
tar = C[v]; sum = -1;
dfs(v);
R = 1; q[0] = C[v]; mark[0] = mark[C[v]] = tar;
for (ll l = 0; l < R; l++) {
sum++;
for (auto &u : col[q[l]]) {
if (iter[u] != tar) goto Hell;
if (mark[C[par[u]]] != tar) q[R++] = C[par[u]], mark[C[par[u]]] = tar;
}
}
ans = min(ans, sum);
}
Hell:
hide_color[C[v]] = hide[v] = true;
for (auto &u : adj[v]) if (!hide[u]) centroid_decomposition(u);
}
int main ()
{
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin>> n >> k;
for (ll i = 1, v, u; i < n; i++) {
cin>> v >> u;
adj[v].push_back(u);
adj[u].push_back(v);
}
for (ll i = 1; i <= n; i++) {
cin>> C[i];
col[C[i]].push_back(i);
}
ans = n;
centroid_decomposition();
cout<< ans << endl;
cerr<< "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
return 0;
}
/*
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9836 KB |
Output is correct |
2 |
Correct |
6 ms |
9836 KB |
Output is correct |
3 |
Correct |
6 ms |
9836 KB |
Output is correct |
4 |
Correct |
6 ms |
9728 KB |
Output is correct |
5 |
Correct |
6 ms |
9836 KB |
Output is correct |
6 |
Correct |
6 ms |
9836 KB |
Output is correct |
7 |
Correct |
8 ms |
9836 KB |
Output is correct |
8 |
Correct |
6 ms |
9836 KB |
Output is correct |
9 |
Correct |
6 ms |
9836 KB |
Output is correct |
10 |
Correct |
6 ms |
9836 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9836 KB |
Output is correct |
2 |
Correct |
6 ms |
9836 KB |
Output is correct |
3 |
Correct |
6 ms |
9836 KB |
Output is correct |
4 |
Correct |
6 ms |
9728 KB |
Output is correct |
5 |
Correct |
6 ms |
9836 KB |
Output is correct |
6 |
Correct |
6 ms |
9836 KB |
Output is correct |
7 |
Correct |
8 ms |
9836 KB |
Output is correct |
8 |
Correct |
6 ms |
9836 KB |
Output is correct |
9 |
Correct |
6 ms |
9836 KB |
Output is correct |
10 |
Correct |
6 ms |
9836 KB |
Output is correct |
11 |
Correct |
8 ms |
9964 KB |
Output is correct |
12 |
Correct |
8 ms |
9964 KB |
Output is correct |
13 |
Correct |
8 ms |
9964 KB |
Output is correct |
14 |
Correct |
8 ms |
9964 KB |
Output is correct |
15 |
Correct |
9 ms |
9964 KB |
Output is correct |
16 |
Correct |
9 ms |
9892 KB |
Output is correct |
17 |
Correct |
8 ms |
9964 KB |
Output is correct |
18 |
Correct |
8 ms |
9964 KB |
Output is correct |
19 |
Correct |
8 ms |
9964 KB |
Output is correct |
20 |
Correct |
8 ms |
9964 KB |
Output is correct |
21 |
Correct |
8 ms |
9964 KB |
Output is correct |
22 |
Correct |
8 ms |
10092 KB |
Output is correct |
23 |
Correct |
8 ms |
9964 KB |
Output is correct |
24 |
Correct |
8 ms |
9964 KB |
Output is correct |
25 |
Correct |
8 ms |
9964 KB |
Output is correct |
26 |
Correct |
8 ms |
9964 KB |
Output is correct |
27 |
Correct |
8 ms |
9964 KB |
Output is correct |
28 |
Correct |
8 ms |
9964 KB |
Output is correct |
29 |
Correct |
8 ms |
9964 KB |
Output is correct |
30 |
Correct |
8 ms |
9964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
486 ms |
35308 KB |
Output is correct |
2 |
Correct |
197 ms |
39148 KB |
Output is correct |
3 |
Correct |
493 ms |
38636 KB |
Output is correct |
4 |
Correct |
193 ms |
39148 KB |
Output is correct |
5 |
Correct |
495 ms |
36076 KB |
Output is correct |
6 |
Correct |
198 ms |
39276 KB |
Output is correct |
7 |
Correct |
484 ms |
36588 KB |
Output is correct |
8 |
Correct |
190 ms |
38760 KB |
Output is correct |
9 |
Correct |
504 ms |
34668 KB |
Output is correct |
10 |
Correct |
472 ms |
32700 KB |
Output is correct |
11 |
Correct |
478 ms |
35180 KB |
Output is correct |
12 |
Correct |
486 ms |
37216 KB |
Output is correct |
13 |
Correct |
475 ms |
32364 KB |
Output is correct |
14 |
Correct |
487 ms |
37484 KB |
Output is correct |
15 |
Correct |
499 ms |
37280 KB |
Output is correct |
16 |
Correct |
476 ms |
33260 KB |
Output is correct |
17 |
Correct |
475 ms |
33772 KB |
Output is correct |
18 |
Correct |
474 ms |
33904 KB |
Output is correct |
19 |
Correct |
495 ms |
36460 KB |
Output is correct |
20 |
Correct |
502 ms |
38272 KB |
Output is correct |
21 |
Correct |
7 ms |
9836 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9836 KB |
Output is correct |
2 |
Correct |
6 ms |
9836 KB |
Output is correct |
3 |
Correct |
6 ms |
9836 KB |
Output is correct |
4 |
Correct |
6 ms |
9728 KB |
Output is correct |
5 |
Correct |
6 ms |
9836 KB |
Output is correct |
6 |
Correct |
6 ms |
9836 KB |
Output is correct |
7 |
Correct |
8 ms |
9836 KB |
Output is correct |
8 |
Correct |
6 ms |
9836 KB |
Output is correct |
9 |
Correct |
6 ms |
9836 KB |
Output is correct |
10 |
Correct |
6 ms |
9836 KB |
Output is correct |
11 |
Correct |
8 ms |
9964 KB |
Output is correct |
12 |
Correct |
8 ms |
9964 KB |
Output is correct |
13 |
Correct |
8 ms |
9964 KB |
Output is correct |
14 |
Correct |
8 ms |
9964 KB |
Output is correct |
15 |
Correct |
9 ms |
9964 KB |
Output is correct |
16 |
Correct |
9 ms |
9892 KB |
Output is correct |
17 |
Correct |
8 ms |
9964 KB |
Output is correct |
18 |
Correct |
8 ms |
9964 KB |
Output is correct |
19 |
Correct |
8 ms |
9964 KB |
Output is correct |
20 |
Correct |
8 ms |
9964 KB |
Output is correct |
21 |
Correct |
8 ms |
9964 KB |
Output is correct |
22 |
Correct |
8 ms |
10092 KB |
Output is correct |
23 |
Correct |
8 ms |
9964 KB |
Output is correct |
24 |
Correct |
8 ms |
9964 KB |
Output is correct |
25 |
Correct |
8 ms |
9964 KB |
Output is correct |
26 |
Correct |
8 ms |
9964 KB |
Output is correct |
27 |
Correct |
8 ms |
9964 KB |
Output is correct |
28 |
Correct |
8 ms |
9964 KB |
Output is correct |
29 |
Correct |
8 ms |
9964 KB |
Output is correct |
30 |
Correct |
8 ms |
9964 KB |
Output is correct |
31 |
Correct |
486 ms |
35308 KB |
Output is correct |
32 |
Correct |
197 ms |
39148 KB |
Output is correct |
33 |
Correct |
493 ms |
38636 KB |
Output is correct |
34 |
Correct |
193 ms |
39148 KB |
Output is correct |
35 |
Correct |
495 ms |
36076 KB |
Output is correct |
36 |
Correct |
198 ms |
39276 KB |
Output is correct |
37 |
Correct |
484 ms |
36588 KB |
Output is correct |
38 |
Correct |
190 ms |
38760 KB |
Output is correct |
39 |
Correct |
504 ms |
34668 KB |
Output is correct |
40 |
Correct |
472 ms |
32700 KB |
Output is correct |
41 |
Correct |
478 ms |
35180 KB |
Output is correct |
42 |
Correct |
486 ms |
37216 KB |
Output is correct |
43 |
Correct |
475 ms |
32364 KB |
Output is correct |
44 |
Correct |
487 ms |
37484 KB |
Output is correct |
45 |
Correct |
499 ms |
37280 KB |
Output is correct |
46 |
Correct |
476 ms |
33260 KB |
Output is correct |
47 |
Correct |
475 ms |
33772 KB |
Output is correct |
48 |
Correct |
474 ms |
33904 KB |
Output is correct |
49 |
Correct |
495 ms |
36460 KB |
Output is correct |
50 |
Correct |
502 ms |
38272 KB |
Output is correct |
51 |
Correct |
7 ms |
9836 KB |
Output is correct |
52 |
Correct |
345 ms |
24916 KB |
Output is correct |
53 |
Correct |
372 ms |
24812 KB |
Output is correct |
54 |
Correct |
380 ms |
24812 KB |
Output is correct |
55 |
Correct |
374 ms |
24812 KB |
Output is correct |
56 |
Correct |
370 ms |
24940 KB |
Output is correct |
57 |
Correct |
360 ms |
24940 KB |
Output is correct |
58 |
Correct |
445 ms |
28268 KB |
Output is correct |
59 |
Correct |
431 ms |
28396 KB |
Output is correct |
60 |
Correct |
476 ms |
28012 KB |
Output is correct |
61 |
Correct |
550 ms |
27756 KB |
Output is correct |
62 |
Correct |
195 ms |
39148 KB |
Output is correct |
63 |
Correct |
196 ms |
39148 KB |
Output is correct |
64 |
Correct |
196 ms |
39148 KB |
Output is correct |
65 |
Correct |
199 ms |
39148 KB |
Output is correct |
66 |
Correct |
276 ms |
31972 KB |
Output is correct |
67 |
Correct |
310 ms |
31972 KB |
Output is correct |
68 |
Correct |
273 ms |
32100 KB |
Output is correct |
69 |
Correct |
307 ms |
32084 KB |
Output is correct |
70 |
Correct |
279 ms |
31972 KB |
Output is correct |
71 |
Correct |
282 ms |
31972 KB |
Output is correct |
72 |
Correct |
273 ms |
31972 KB |
Output is correct |
73 |
Correct |
278 ms |
31332 KB |
Output is correct |
74 |
Correct |
305 ms |
31988 KB |
Output is correct |
75 |
Correct |
303 ms |
31968 KB |
Output is correct |
76 |
Correct |
553 ms |
31212 KB |
Output is correct |
77 |
Correct |
536 ms |
29804 KB |
Output is correct |
78 |
Correct |
494 ms |
33644 KB |
Output is correct |
79 |
Correct |
503 ms |
31832 KB |
Output is correct |
80 |
Correct |
489 ms |
37800 KB |
Output is correct |
81 |
Correct |
495 ms |
34796 KB |
Output is correct |
82 |
Correct |
501 ms |
34924 KB |
Output is correct |
83 |
Correct |
500 ms |
32236 KB |
Output is correct |
84 |
Correct |
487 ms |
37100 KB |
Output is correct |
85 |
Correct |
506 ms |
35692 KB |
Output is correct |
86 |
Correct |
541 ms |
31724 KB |
Output is correct |
87 |
Correct |
487 ms |
33292 KB |
Output is correct |
88 |
Correct |
398 ms |
34156 KB |
Output is correct |
89 |
Correct |
398 ms |
30956 KB |
Output is correct |
90 |
Correct |
392 ms |
30952 KB |
Output is correct |
91 |
Correct |
396 ms |
32768 KB |
Output is correct |
92 |
Correct |
405 ms |
31724 KB |
Output is correct |
93 |
Correct |
417 ms |
31384 KB |
Output is correct |
94 |
Correct |
414 ms |
30956 KB |
Output is correct |
95 |
Correct |
415 ms |
32108 KB |
Output is correct |
96 |
Correct |
400 ms |
31340 KB |
Output is correct |
97 |
Correct |
407 ms |
32748 KB |
Output is correct |