#include <bits/stdc++.h>
#define PRINT(x) cerr<<#x<<'='<<x<<endl;
#define NL(x) " \n"[(x)]
#define lld long long
#define pil pair<int,lld>
#define pli pair<lld,int>
#define pll pair<lld,lld>
#define pii pair<int,int>
#define pb push_back
#define fi first
#define se second
#define mid (l+r)/2
#define endl '\n'
#define all(a) begin(a),end(a)
#define sz(a) int((a).size())
#define LINF 1000000000000000LL
#define INF 1000000000
#define EPS 1e-9
using namespace std;
#define MAXN 200010
int N, K, C[MAXN], siz[MAXN], par[MAXN], sol;
bool vis[MAXN], col[MAXN], pos[MAXN], ovde[MAXN];
vector<int> adj[MAXN], v, pC[MAXN];
int dfsSize(int node, int prev) {
siz[node] = 1;
for(auto x : adj[node]) {
if(!vis[x] && x != prev) siz[node] += dfsSize(x, node);
}
return siz[node];
}
int dfsCent(int node, int prev, int S) {
for(auto x : adj[node]) {
if(!vis[x] && x != prev && siz[x] > S/2) return dfsCent(x, node, S);
}
return node;
}
void dfsParent(int node, int prev) {
par[node] = prev;
ovde[node] = true;
v.pb(node);
for(auto x : adj[node]) {
if(!vis[x] && x != prev) dfsParent(x, node);
}
}
vector<int> nv;
int boja = 0;
bool addColor(int color) {
if(col[color]) return true;
boja++;
col[color] = true;
for(auto x : pC[color]) {
if(!ovde[x]) return false;
if(!pos[x]) nv.pb(x);
}
return true;
}
void centDecomp(int node) {
int S = dfsSize(node, node);
node = dfsCent(node, node, S);
vis[node] = 1;
dfsParent(node, node);
boja = 0;
if(!addColor(C[node])) {
nv.clear();
boja = K;
}
while(!nv.empty()) {
int cur = nv.back(); nv.pop_back();
if(pos[cur]) continue;
while(1) {
pos[cur] = true;
if(!addColor(C[cur])) {
nv.clear();
boja = K;
break;
}
if(par[cur] == cur || pos[par[cur]]) break;
cur = par[cur];
}
}
sol = min(sol, boja);
for(auto x : v) pos[x] = ovde[x] = col[C[x]] = 0;
v.clear();
for(auto x : adj[node]) if(!vis[x]) centDecomp(x);
}
int main() {
ios::sync_with_stdio(false); cin.tie(0);
cin >> N >> K;
for(int i = 1; i <= N-1; i++) {
int x, y; cin >> x >> y;
adj[x].pb(y);
adj[y].pb(x);
}
for(int i = 1; i <= N; i++) {
cin >> C[i];
pC[C[i]].pb(i);
}
sol = K;
centDecomp(1);
cout << sol-1;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9676 KB |
Output is correct |
3 |
Correct |
6 ms |
9676 KB |
Output is correct |
4 |
Correct |
6 ms |
9676 KB |
Output is correct |
5 |
Correct |
6 ms |
9620 KB |
Output is correct |
6 |
Correct |
6 ms |
9676 KB |
Output is correct |
7 |
Correct |
6 ms |
9676 KB |
Output is correct |
8 |
Correct |
6 ms |
9676 KB |
Output is correct |
9 |
Correct |
6 ms |
9644 KB |
Output is correct |
10 |
Correct |
6 ms |
9628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9676 KB |
Output is correct |
3 |
Correct |
6 ms |
9676 KB |
Output is correct |
4 |
Correct |
6 ms |
9676 KB |
Output is correct |
5 |
Correct |
6 ms |
9620 KB |
Output is correct |
6 |
Correct |
6 ms |
9676 KB |
Output is correct |
7 |
Correct |
6 ms |
9676 KB |
Output is correct |
8 |
Correct |
6 ms |
9676 KB |
Output is correct |
9 |
Correct |
6 ms |
9644 KB |
Output is correct |
10 |
Correct |
6 ms |
9628 KB |
Output is correct |
11 |
Correct |
9 ms |
9884 KB |
Output is correct |
12 |
Correct |
7 ms |
9804 KB |
Output is correct |
13 |
Correct |
7 ms |
9804 KB |
Output is correct |
14 |
Correct |
8 ms |
9840 KB |
Output is correct |
15 |
Correct |
9 ms |
9804 KB |
Output is correct |
16 |
Correct |
8 ms |
9804 KB |
Output is correct |
17 |
Correct |
8 ms |
9804 KB |
Output is correct |
18 |
Correct |
8 ms |
9936 KB |
Output is correct |
19 |
Correct |
7 ms |
9932 KB |
Output is correct |
20 |
Correct |
9 ms |
9932 KB |
Output is correct |
21 |
Correct |
8 ms |
9932 KB |
Output is correct |
22 |
Correct |
8 ms |
9976 KB |
Output is correct |
23 |
Correct |
8 ms |
9932 KB |
Output is correct |
24 |
Correct |
8 ms |
9804 KB |
Output is correct |
25 |
Correct |
8 ms |
9864 KB |
Output is correct |
26 |
Correct |
9 ms |
9932 KB |
Output is correct |
27 |
Correct |
8 ms |
9932 KB |
Output is correct |
28 |
Correct |
8 ms |
9952 KB |
Output is correct |
29 |
Correct |
8 ms |
9932 KB |
Output is correct |
30 |
Correct |
8 ms |
9932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
577 ms |
35076 KB |
Output is correct |
2 |
Correct |
232 ms |
39128 KB |
Output is correct |
3 |
Correct |
569 ms |
38500 KB |
Output is correct |
4 |
Correct |
236 ms |
39196 KB |
Output is correct |
5 |
Correct |
561 ms |
36080 KB |
Output is correct |
6 |
Correct |
233 ms |
39180 KB |
Output is correct |
7 |
Correct |
549 ms |
36380 KB |
Output is correct |
8 |
Correct |
229 ms |
39096 KB |
Output is correct |
9 |
Correct |
639 ms |
35504 KB |
Output is correct |
10 |
Correct |
633 ms |
33320 KB |
Output is correct |
11 |
Correct |
638 ms |
35512 KB |
Output is correct |
12 |
Correct |
646 ms |
37556 KB |
Output is correct |
13 |
Correct |
633 ms |
33204 KB |
Output is correct |
14 |
Correct |
682 ms |
37764 KB |
Output is correct |
15 |
Correct |
671 ms |
37848 KB |
Output is correct |
16 |
Correct |
643 ms |
33868 KB |
Output is correct |
17 |
Correct |
653 ms |
34400 KB |
Output is correct |
18 |
Correct |
679 ms |
34352 KB |
Output is correct |
19 |
Correct |
645 ms |
36996 KB |
Output is correct |
20 |
Correct |
667 ms |
38456 KB |
Output is correct |
21 |
Correct |
6 ms |
9640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9676 KB |
Output is correct |
3 |
Correct |
6 ms |
9676 KB |
Output is correct |
4 |
Correct |
6 ms |
9676 KB |
Output is correct |
5 |
Correct |
6 ms |
9620 KB |
Output is correct |
6 |
Correct |
6 ms |
9676 KB |
Output is correct |
7 |
Correct |
6 ms |
9676 KB |
Output is correct |
8 |
Correct |
6 ms |
9676 KB |
Output is correct |
9 |
Correct |
6 ms |
9644 KB |
Output is correct |
10 |
Correct |
6 ms |
9628 KB |
Output is correct |
11 |
Correct |
9 ms |
9884 KB |
Output is correct |
12 |
Correct |
7 ms |
9804 KB |
Output is correct |
13 |
Correct |
7 ms |
9804 KB |
Output is correct |
14 |
Correct |
8 ms |
9840 KB |
Output is correct |
15 |
Correct |
9 ms |
9804 KB |
Output is correct |
16 |
Correct |
8 ms |
9804 KB |
Output is correct |
17 |
Correct |
8 ms |
9804 KB |
Output is correct |
18 |
Correct |
8 ms |
9936 KB |
Output is correct |
19 |
Correct |
7 ms |
9932 KB |
Output is correct |
20 |
Correct |
9 ms |
9932 KB |
Output is correct |
21 |
Correct |
8 ms |
9932 KB |
Output is correct |
22 |
Correct |
8 ms |
9976 KB |
Output is correct |
23 |
Correct |
8 ms |
9932 KB |
Output is correct |
24 |
Correct |
8 ms |
9804 KB |
Output is correct |
25 |
Correct |
8 ms |
9864 KB |
Output is correct |
26 |
Correct |
9 ms |
9932 KB |
Output is correct |
27 |
Correct |
8 ms |
9932 KB |
Output is correct |
28 |
Correct |
8 ms |
9952 KB |
Output is correct |
29 |
Correct |
8 ms |
9932 KB |
Output is correct |
30 |
Correct |
8 ms |
9932 KB |
Output is correct |
31 |
Correct |
577 ms |
35076 KB |
Output is correct |
32 |
Correct |
232 ms |
39128 KB |
Output is correct |
33 |
Correct |
569 ms |
38500 KB |
Output is correct |
34 |
Correct |
236 ms |
39196 KB |
Output is correct |
35 |
Correct |
561 ms |
36080 KB |
Output is correct |
36 |
Correct |
233 ms |
39180 KB |
Output is correct |
37 |
Correct |
549 ms |
36380 KB |
Output is correct |
38 |
Correct |
229 ms |
39096 KB |
Output is correct |
39 |
Correct |
639 ms |
35504 KB |
Output is correct |
40 |
Correct |
633 ms |
33320 KB |
Output is correct |
41 |
Correct |
638 ms |
35512 KB |
Output is correct |
42 |
Correct |
646 ms |
37556 KB |
Output is correct |
43 |
Correct |
633 ms |
33204 KB |
Output is correct |
44 |
Correct |
682 ms |
37764 KB |
Output is correct |
45 |
Correct |
671 ms |
37848 KB |
Output is correct |
46 |
Correct |
643 ms |
33868 KB |
Output is correct |
47 |
Correct |
653 ms |
34400 KB |
Output is correct |
48 |
Correct |
679 ms |
34352 KB |
Output is correct |
49 |
Correct |
645 ms |
36996 KB |
Output is correct |
50 |
Correct |
667 ms |
38456 KB |
Output is correct |
51 |
Correct |
6 ms |
9640 KB |
Output is correct |
52 |
Correct |
475 ms |
26200 KB |
Output is correct |
53 |
Correct |
501 ms |
26224 KB |
Output is correct |
54 |
Correct |
506 ms |
26296 KB |
Output is correct |
55 |
Correct |
517 ms |
26280 KB |
Output is correct |
56 |
Correct |
507 ms |
26276 KB |
Output is correct |
57 |
Correct |
496 ms |
26188 KB |
Output is correct |
58 |
Correct |
486 ms |
28432 KB |
Output is correct |
59 |
Correct |
438 ms |
28476 KB |
Output is correct |
60 |
Correct |
560 ms |
27964 KB |
Output is correct |
61 |
Correct |
589 ms |
27832 KB |
Output is correct |
62 |
Correct |
231 ms |
39100 KB |
Output is correct |
63 |
Correct |
231 ms |
39152 KB |
Output is correct |
64 |
Correct |
227 ms |
38964 KB |
Output is correct |
65 |
Correct |
227 ms |
39116 KB |
Output is correct |
66 |
Correct |
366 ms |
32888 KB |
Output is correct |
67 |
Correct |
378 ms |
32836 KB |
Output is correct |
68 |
Correct |
386 ms |
33016 KB |
Output is correct |
69 |
Correct |
391 ms |
32892 KB |
Output is correct |
70 |
Correct |
377 ms |
32904 KB |
Output is correct |
71 |
Correct |
402 ms |
32884 KB |
Output is correct |
72 |
Correct |
378 ms |
32888 KB |
Output is correct |
73 |
Correct |
370 ms |
32264 KB |
Output is correct |
74 |
Correct |
375 ms |
33004 KB |
Output is correct |
75 |
Correct |
379 ms |
32936 KB |
Output is correct |
76 |
Correct |
544 ms |
31164 KB |
Output is correct |
77 |
Correct |
519 ms |
29672 KB |
Output is correct |
78 |
Correct |
638 ms |
34252 KB |
Output is correct |
79 |
Correct |
638 ms |
32504 KB |
Output is correct |
80 |
Correct |
668 ms |
38220 KB |
Output is correct |
81 |
Correct |
631 ms |
35512 KB |
Output is correct |
82 |
Correct |
657 ms |
35404 KB |
Output is correct |
83 |
Correct |
667 ms |
32996 KB |
Output is correct |
84 |
Correct |
686 ms |
37728 KB |
Output is correct |
85 |
Correct |
672 ms |
36568 KB |
Output is correct |
86 |
Correct |
667 ms |
32588 KB |
Output is correct |
87 |
Correct |
657 ms |
34108 KB |
Output is correct |
88 |
Correct |
602 ms |
34892 KB |
Output is correct |
89 |
Correct |
571 ms |
31676 KB |
Output is correct |
90 |
Correct |
589 ms |
31604 KB |
Output is correct |
91 |
Correct |
574 ms |
33316 KB |
Output is correct |
92 |
Correct |
561 ms |
32296 KB |
Output is correct |
93 |
Correct |
567 ms |
31888 KB |
Output is correct |
94 |
Correct |
591 ms |
31316 KB |
Output is correct |
95 |
Correct |
613 ms |
32952 KB |
Output is correct |
96 |
Correct |
551 ms |
31576 KB |
Output is correct |
97 |
Correct |
553 ms |
33184 KB |
Output is correct |