#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")
#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;
}
/*
*/
Compilation message
capital_city.cpp:2: warning: ignoring #pragma comment [-Wunknown-pragmas]
2 | #pragma comment(linker, "/stack:200000000")
|
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
9836 KB |
Output is correct |
2 |
Correct |
8 ms |
9836 KB |
Output is correct |
3 |
Correct |
8 ms |
9836 KB |
Output is correct |
4 |
Correct |
8 ms |
9836 KB |
Output is correct |
5 |
Correct |
8 ms |
9836 KB |
Output is correct |
6 |
Correct |
8 ms |
9836 KB |
Output is correct |
7 |
Correct |
8 ms |
9836 KB |
Output is correct |
8 |
Correct |
8 ms |
9836 KB |
Output is correct |
9 |
Correct |
8 ms |
9836 KB |
Output is correct |
10 |
Correct |
8 ms |
9836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
9836 KB |
Output is correct |
2 |
Correct |
8 ms |
9836 KB |
Output is correct |
3 |
Correct |
8 ms |
9836 KB |
Output is correct |
4 |
Correct |
8 ms |
9836 KB |
Output is correct |
5 |
Correct |
8 ms |
9836 KB |
Output is correct |
6 |
Correct |
8 ms |
9836 KB |
Output is correct |
7 |
Correct |
8 ms |
9836 KB |
Output is correct |
8 |
Correct |
8 ms |
9836 KB |
Output is correct |
9 |
Correct |
8 ms |
9836 KB |
Output is correct |
10 |
Correct |
8 ms |
9836 KB |
Output is correct |
11 |
Correct |
9 ms |
9964 KB |
Output is correct |
12 |
Correct |
9 ms |
9964 KB |
Output is correct |
13 |
Correct |
9 ms |
9964 KB |
Output is correct |
14 |
Correct |
10 ms |
9964 KB |
Output is correct |
15 |
Correct |
9 ms |
10092 KB |
Output is correct |
16 |
Correct |
11 ms |
9964 KB |
Output is correct |
17 |
Correct |
9 ms |
9964 KB |
Output is correct |
18 |
Correct |
9 ms |
9964 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 |
9964 KB |
Output is correct |
24 |
Correct |
9 ms |
9964 KB |
Output is correct |
25 |
Correct |
9 ms |
10092 KB |
Output is correct |
26 |
Correct |
10 ms |
10092 KB |
Output is correct |
27 |
Correct |
10 ms |
10112 KB |
Output is correct |
28 |
Correct |
9 ms |
9964 KB |
Output is correct |
29 |
Correct |
9 ms |
10052 KB |
Output is correct |
30 |
Correct |
10 ms |
9964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
524 ms |
40260 KB |
Output is correct |
2 |
Correct |
200 ms |
38764 KB |
Output is correct |
3 |
Correct |
525 ms |
37868 KB |
Output is correct |
4 |
Correct |
205 ms |
38764 KB |
Output is correct |
5 |
Correct |
516 ms |
34796 KB |
Output is correct |
6 |
Correct |
201 ms |
38636 KB |
Output is correct |
7 |
Correct |
537 ms |
35180 KB |
Output is correct |
8 |
Correct |
198 ms |
38248 KB |
Output is correct |
9 |
Correct |
525 ms |
33516 KB |
Output is correct |
10 |
Correct |
483 ms |
30700 KB |
Output is correct |
11 |
Correct |
494 ms |
33900 KB |
Output is correct |
12 |
Correct |
508 ms |
36460 KB |
Output is correct |
13 |
Correct |
501 ms |
30316 KB |
Output is correct |
14 |
Correct |
504 ms |
36716 KB |
Output is correct |
15 |
Correct |
524 ms |
36460 KB |
Output is correct |
16 |
Correct |
506 ms |
31468 KB |
Output is correct |
17 |
Correct |
493 ms |
31980 KB |
Output is correct |
18 |
Correct |
494 ms |
32264 KB |
Output is correct |
19 |
Correct |
506 ms |
35420 KB |
Output is correct |
20 |
Correct |
520 ms |
37612 KB |
Output is correct |
21 |
Correct |
8 ms |
9836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
9836 KB |
Output is correct |
2 |
Correct |
8 ms |
9836 KB |
Output is correct |
3 |
Correct |
8 ms |
9836 KB |
Output is correct |
4 |
Correct |
8 ms |
9836 KB |
Output is correct |
5 |
Correct |
8 ms |
9836 KB |
Output is correct |
6 |
Correct |
8 ms |
9836 KB |
Output is correct |
7 |
Correct |
8 ms |
9836 KB |
Output is correct |
8 |
Correct |
8 ms |
9836 KB |
Output is correct |
9 |
Correct |
8 ms |
9836 KB |
Output is correct |
10 |
Correct |
8 ms |
9836 KB |
Output is correct |
11 |
Correct |
9 ms |
9964 KB |
Output is correct |
12 |
Correct |
9 ms |
9964 KB |
Output is correct |
13 |
Correct |
9 ms |
9964 KB |
Output is correct |
14 |
Correct |
10 ms |
9964 KB |
Output is correct |
15 |
Correct |
9 ms |
10092 KB |
Output is correct |
16 |
Correct |
11 ms |
9964 KB |
Output is correct |
17 |
Correct |
9 ms |
9964 KB |
Output is correct |
18 |
Correct |
9 ms |
9964 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 |
9964 KB |
Output is correct |
24 |
Correct |
9 ms |
9964 KB |
Output is correct |
25 |
Correct |
9 ms |
10092 KB |
Output is correct |
26 |
Correct |
10 ms |
10092 KB |
Output is correct |
27 |
Correct |
10 ms |
10112 KB |
Output is correct |
28 |
Correct |
9 ms |
9964 KB |
Output is correct |
29 |
Correct |
9 ms |
10052 KB |
Output is correct |
30 |
Correct |
10 ms |
9964 KB |
Output is correct |
31 |
Correct |
524 ms |
40260 KB |
Output is correct |
32 |
Correct |
200 ms |
38764 KB |
Output is correct |
33 |
Correct |
525 ms |
37868 KB |
Output is correct |
34 |
Correct |
205 ms |
38764 KB |
Output is correct |
35 |
Correct |
516 ms |
34796 KB |
Output is correct |
36 |
Correct |
201 ms |
38636 KB |
Output is correct |
37 |
Correct |
537 ms |
35180 KB |
Output is correct |
38 |
Correct |
198 ms |
38248 KB |
Output is correct |
39 |
Correct |
525 ms |
33516 KB |
Output is correct |
40 |
Correct |
483 ms |
30700 KB |
Output is correct |
41 |
Correct |
494 ms |
33900 KB |
Output is correct |
42 |
Correct |
508 ms |
36460 KB |
Output is correct |
43 |
Correct |
501 ms |
30316 KB |
Output is correct |
44 |
Correct |
504 ms |
36716 KB |
Output is correct |
45 |
Correct |
524 ms |
36460 KB |
Output is correct |
46 |
Correct |
506 ms |
31468 KB |
Output is correct |
47 |
Correct |
493 ms |
31980 KB |
Output is correct |
48 |
Correct |
494 ms |
32264 KB |
Output is correct |
49 |
Correct |
506 ms |
35420 KB |
Output is correct |
50 |
Correct |
520 ms |
37612 KB |
Output is correct |
51 |
Correct |
8 ms |
9836 KB |
Output is correct |
52 |
Correct |
353 ms |
21420 KB |
Output is correct |
53 |
Correct |
403 ms |
21484 KB |
Output is correct |
54 |
Correct |
410 ms |
21228 KB |
Output is correct |
55 |
Correct |
404 ms |
21356 KB |
Output is correct |
56 |
Correct |
391 ms |
21396 KB |
Output is correct |
57 |
Correct |
381 ms |
21484 KB |
Output is correct |
58 |
Correct |
460 ms |
25068 KB |
Output is correct |
59 |
Correct |
446 ms |
25452 KB |
Output is correct |
60 |
Correct |
542 ms |
24700 KB |
Output is correct |
61 |
Correct |
547 ms |
24428 KB |
Output is correct |
62 |
Correct |
203 ms |
38664 KB |
Output is correct |
63 |
Correct |
210 ms |
38636 KB |
Output is correct |
64 |
Correct |
197 ms |
38508 KB |
Output is correct |
65 |
Correct |
207 ms |
38636 KB |
Output is correct |
66 |
Correct |
309 ms |
29796 KB |
Output is correct |
67 |
Correct |
296 ms |
29796 KB |
Output is correct |
68 |
Correct |
295 ms |
30052 KB |
Output is correct |
69 |
Correct |
302 ms |
29924 KB |
Output is correct |
70 |
Correct |
324 ms |
29796 KB |
Output is correct |
71 |
Correct |
289 ms |
29796 KB |
Output is correct |
72 |
Correct |
288 ms |
29928 KB |
Output is correct |
73 |
Correct |
299 ms |
28900 KB |
Output is correct |
74 |
Correct |
301 ms |
29796 KB |
Output is correct |
75 |
Correct |
297 ms |
29796 KB |
Output is correct |
76 |
Correct |
581 ms |
28780 KB |
Output is correct |
77 |
Correct |
581 ms |
27116 KB |
Output is correct |
78 |
Correct |
517 ms |
31852 KB |
Output is correct |
79 |
Correct |
520 ms |
29824 KB |
Output is correct |
80 |
Correct |
505 ms |
37100 KB |
Output is correct |
81 |
Correct |
535 ms |
33516 KB |
Output is correct |
82 |
Correct |
506 ms |
33472 KB |
Output is correct |
83 |
Correct |
540 ms |
30316 KB |
Output is correct |
84 |
Correct |
538 ms |
36204 KB |
Output is correct |
85 |
Correct |
529 ms |
34540 KB |
Output is correct |
86 |
Correct |
525 ms |
29752 KB |
Output is correct |
87 |
Correct |
533 ms |
31596 KB |
Output is correct |
88 |
Correct |
443 ms |
32620 KB |
Output is correct |
89 |
Correct |
428 ms |
28588 KB |
Output is correct |
90 |
Correct |
449 ms |
28396 KB |
Output is correct |
91 |
Correct |
436 ms |
30956 KB |
Output is correct |
92 |
Correct |
467 ms |
29676 KB |
Output is correct |
93 |
Correct |
441 ms |
29292 KB |
Output is correct |
94 |
Correct |
451 ms |
28780 KB |
Output is correct |
95 |
Correct |
442 ms |
29932 KB |
Output is correct |
96 |
Correct |
474 ms |
28820 KB |
Output is correct |
97 |
Correct |
450 ms |
30956 KB |
Output is correct |