#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
#define pb push_back
#define sz(x) (int)(x).size()
void ckmin(int& a, int b){
a = min(a, b);
}
const int MOD = 1e9+7;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
struct HashFunc{
size_t operator()(ll a){
return ((a+123485122314LL)^((a+14123901283002LL)>>30));
}
};
gp_hash_table<ll, vi> g;
const int mx = 200005;
vi adj[mx];
int C[mx];
vi invC[mx];
int ans = MOD;
bool done[mx];
int sub[mx];
void genSub(int node, int p = -1){
sub[node] = 1;
for(auto u: adj[node]){
if(done[u]) continue;
if(u == p) continue;
genSub(u, node);
sub[node]+=sub[u];
}
}
bool in_subtree[mx];
bool comp_inc[mx];
void setInSubtree(int node, bool val, int p = -1){
in_subtree[node] = val;
for(auto u: adj[node]){
if(done[u]) continue;
if(u == p) continue;
setInSubtree(u, val, node);
}
}
int par[mx];
void genPar(int node, int p = -1){
par[node] = p;
for(auto u: adj[node]){
if(done[u]) continue;
if(u == p) continue;
genPar(u, node);
}
}
void centroidDecomp(int node){
// cout << "centroidDecomp: " << node << "\n";
// cout.flush();
genSub(node);
auto genCen = [&](int node){
int cen = node;
int p = -1;
while(true){
bool found = 0;
for(auto u: adj[cen]){
if(done[u]) continue;
if(u == p) continue;
if(sub[u]*2 > sub[node]){
found = 1;
p = cen;
cen = u;
break;
}
}
if(!found){
break;
}
}
return cen;
};
int cen = genCen(node);
// cout << "foundCen: " << cen << "\n";
// cout.flush();
setInSubtree(cen, 1);
genPar(cen);
//solve
vi comps;
queue<int> q;
comps.pb(C[cen]);
comp_inc[C[cen]] = 1;
q.push(C[cen]);
bool bad = 0;
while(sz(q)){
int c = q.front();
q.pop();
for(auto u: invC[c]){
if(!in_subtree[u]){
bad = 1;
break;
}
}
if(bad) break;
for(auto u: invC[c]){
int p = par[u];
if(p == -1) continue;
if(comp_inc[C[p]]) continue;
comps.pb(C[p]);
comp_inc[C[p]] = 1;
q.push(C[p]);
}
}
// cout << "comps: ";
// for(auto u: comps){
// cout << u << " ";
// }
// cout << "\n";
if(!bad){
ckmin(ans, sz(comps)-1);
}
setInSubtree(cen, 0);
for(auto u: comps){
comp_inc[u] = 0;
}
done[cen] = 1;
for(auto u: adj[cen]){
if(done[u]) continue;
centroidDecomp(u);
}
}
int main(){
cin.tie(0)->sync_with_stdio(0);
int N, K;
cin >> N >> K;
for(int i = 1; i <= N-1; i++){
int a, b;
cin >> a >> b;
adj[a].pb(b);
adj[b].pb(a);
}
for(int i = 1; i <= N; i++){
cin >> C[i];
invC[C[i]].pb(i);
}
centroidDecomp(1);
cout << ans << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9828 KB |
Output is correct |
2 |
Correct |
6 ms |
9680 KB |
Output is correct |
3 |
Correct |
6 ms |
9676 KB |
Output is correct |
4 |
Correct |
6 ms |
9644 KB |
Output is correct |
5 |
Correct |
6 ms |
9728 KB |
Output is correct |
6 |
Correct |
6 ms |
9724 KB |
Output is correct |
7 |
Correct |
6 ms |
9676 KB |
Output is correct |
8 |
Correct |
6 ms |
9720 KB |
Output is correct |
9 |
Correct |
6 ms |
9724 KB |
Output is correct |
10 |
Correct |
6 ms |
9676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9828 KB |
Output is correct |
2 |
Correct |
6 ms |
9680 KB |
Output is correct |
3 |
Correct |
6 ms |
9676 KB |
Output is correct |
4 |
Correct |
6 ms |
9644 KB |
Output is correct |
5 |
Correct |
6 ms |
9728 KB |
Output is correct |
6 |
Correct |
6 ms |
9724 KB |
Output is correct |
7 |
Correct |
6 ms |
9676 KB |
Output is correct |
8 |
Correct |
6 ms |
9720 KB |
Output is correct |
9 |
Correct |
6 ms |
9724 KB |
Output is correct |
10 |
Correct |
6 ms |
9676 KB |
Output is correct |
11 |
Correct |
8 ms |
9868 KB |
Output is correct |
12 |
Correct |
9 ms |
9804 KB |
Output is correct |
13 |
Correct |
8 ms |
9864 KB |
Output is correct |
14 |
Correct |
9 ms |
9804 KB |
Output is correct |
15 |
Correct |
9 ms |
9932 KB |
Output is correct |
16 |
Correct |
9 ms |
9864 KB |
Output is correct |
17 |
Correct |
8 ms |
9856 KB |
Output is correct |
18 |
Correct |
8 ms |
9932 KB |
Output is correct |
19 |
Correct |
8 ms |
9860 KB |
Output is correct |
20 |
Correct |
8 ms |
9932 KB |
Output is correct |
21 |
Correct |
8 ms |
9932 KB |
Output is correct |
22 |
Correct |
8 ms |
9996 KB |
Output is correct |
23 |
Correct |
9 ms |
9932 KB |
Output is correct |
24 |
Correct |
8 ms |
9932 KB |
Output is correct |
25 |
Correct |
10 ms |
9996 KB |
Output is correct |
26 |
Correct |
9 ms |
9992 KB |
Output is correct |
27 |
Correct |
9 ms |
9932 KB |
Output is correct |
28 |
Correct |
10 ms |
9932 KB |
Output is correct |
29 |
Correct |
9 ms |
9940 KB |
Output is correct |
30 |
Correct |
9 ms |
9932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
639 ms |
37492 KB |
Output is correct |
2 |
Correct |
282 ms |
37968 KB |
Output is correct |
3 |
Correct |
632 ms |
37164 KB |
Output is correct |
4 |
Correct |
282 ms |
37956 KB |
Output is correct |
5 |
Correct |
687 ms |
34288 KB |
Output is correct |
6 |
Correct |
276 ms |
37884 KB |
Output is correct |
7 |
Correct |
642 ms |
34968 KB |
Output is correct |
8 |
Correct |
275 ms |
37696 KB |
Output is correct |
9 |
Correct |
817 ms |
33204 KB |
Output is correct |
10 |
Correct |
818 ms |
30616 KB |
Output is correct |
11 |
Correct |
817 ms |
33472 KB |
Output is correct |
12 |
Correct |
833 ms |
36160 KB |
Output is correct |
13 |
Correct |
848 ms |
30208 KB |
Output is correct |
14 |
Correct |
826 ms |
36548 KB |
Output is correct |
15 |
Correct |
813 ms |
36100 KB |
Output is correct |
16 |
Correct |
793 ms |
31152 KB |
Output is correct |
17 |
Correct |
796 ms |
31684 KB |
Output is correct |
18 |
Correct |
828 ms |
31812 KB |
Output is correct |
19 |
Correct |
783 ms |
35012 KB |
Output is correct |
20 |
Correct |
805 ms |
37284 KB |
Output is correct |
21 |
Correct |
6 ms |
9676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9828 KB |
Output is correct |
2 |
Correct |
6 ms |
9680 KB |
Output is correct |
3 |
Correct |
6 ms |
9676 KB |
Output is correct |
4 |
Correct |
6 ms |
9644 KB |
Output is correct |
5 |
Correct |
6 ms |
9728 KB |
Output is correct |
6 |
Correct |
6 ms |
9724 KB |
Output is correct |
7 |
Correct |
6 ms |
9676 KB |
Output is correct |
8 |
Correct |
6 ms |
9720 KB |
Output is correct |
9 |
Correct |
6 ms |
9724 KB |
Output is correct |
10 |
Correct |
6 ms |
9676 KB |
Output is correct |
11 |
Correct |
8 ms |
9868 KB |
Output is correct |
12 |
Correct |
9 ms |
9804 KB |
Output is correct |
13 |
Correct |
8 ms |
9864 KB |
Output is correct |
14 |
Correct |
9 ms |
9804 KB |
Output is correct |
15 |
Correct |
9 ms |
9932 KB |
Output is correct |
16 |
Correct |
9 ms |
9864 KB |
Output is correct |
17 |
Correct |
8 ms |
9856 KB |
Output is correct |
18 |
Correct |
8 ms |
9932 KB |
Output is correct |
19 |
Correct |
8 ms |
9860 KB |
Output is correct |
20 |
Correct |
8 ms |
9932 KB |
Output is correct |
21 |
Correct |
8 ms |
9932 KB |
Output is correct |
22 |
Correct |
8 ms |
9996 KB |
Output is correct |
23 |
Correct |
9 ms |
9932 KB |
Output is correct |
24 |
Correct |
8 ms |
9932 KB |
Output is correct |
25 |
Correct |
10 ms |
9996 KB |
Output is correct |
26 |
Correct |
9 ms |
9992 KB |
Output is correct |
27 |
Correct |
9 ms |
9932 KB |
Output is correct |
28 |
Correct |
10 ms |
9932 KB |
Output is correct |
29 |
Correct |
9 ms |
9940 KB |
Output is correct |
30 |
Correct |
9 ms |
9932 KB |
Output is correct |
31 |
Correct |
639 ms |
37492 KB |
Output is correct |
32 |
Correct |
282 ms |
37968 KB |
Output is correct |
33 |
Correct |
632 ms |
37164 KB |
Output is correct |
34 |
Correct |
282 ms |
37956 KB |
Output is correct |
35 |
Correct |
687 ms |
34288 KB |
Output is correct |
36 |
Correct |
276 ms |
37884 KB |
Output is correct |
37 |
Correct |
642 ms |
34968 KB |
Output is correct |
38 |
Correct |
275 ms |
37696 KB |
Output is correct |
39 |
Correct |
817 ms |
33204 KB |
Output is correct |
40 |
Correct |
818 ms |
30616 KB |
Output is correct |
41 |
Correct |
817 ms |
33472 KB |
Output is correct |
42 |
Correct |
833 ms |
36160 KB |
Output is correct |
43 |
Correct |
848 ms |
30208 KB |
Output is correct |
44 |
Correct |
826 ms |
36548 KB |
Output is correct |
45 |
Correct |
813 ms |
36100 KB |
Output is correct |
46 |
Correct |
793 ms |
31152 KB |
Output is correct |
47 |
Correct |
796 ms |
31684 KB |
Output is correct |
48 |
Correct |
828 ms |
31812 KB |
Output is correct |
49 |
Correct |
783 ms |
35012 KB |
Output is correct |
50 |
Correct |
805 ms |
37284 KB |
Output is correct |
51 |
Correct |
6 ms |
9676 KB |
Output is correct |
52 |
Correct |
581 ms |
21116 KB |
Output is correct |
53 |
Correct |
610 ms |
21060 KB |
Output is correct |
54 |
Correct |
616 ms |
21132 KB |
Output is correct |
55 |
Correct |
614 ms |
21116 KB |
Output is correct |
56 |
Correct |
625 ms |
21044 KB |
Output is correct |
57 |
Correct |
646 ms |
21124 KB |
Output is correct |
58 |
Correct |
505 ms |
24644 KB |
Output is correct |
59 |
Correct |
474 ms |
24900 KB |
Output is correct |
60 |
Correct |
660 ms |
24220 KB |
Output is correct |
61 |
Correct |
730 ms |
23832 KB |
Output is correct |
62 |
Correct |
282 ms |
38084 KB |
Output is correct |
63 |
Correct |
276 ms |
37956 KB |
Output is correct |
64 |
Correct |
275 ms |
37956 KB |
Output is correct |
65 |
Correct |
288 ms |
37952 KB |
Output is correct |
66 |
Correct |
398 ms |
29428 KB |
Output is correct |
67 |
Correct |
429 ms |
29256 KB |
Output is correct |
68 |
Correct |
419 ms |
29344 KB |
Output is correct |
69 |
Correct |
412 ms |
29372 KB |
Output is correct |
70 |
Correct |
429 ms |
29500 KB |
Output is correct |
71 |
Correct |
389 ms |
29372 KB |
Output is correct |
72 |
Correct |
424 ms |
29404 KB |
Output is correct |
73 |
Correct |
403 ms |
28476 KB |
Output is correct |
74 |
Correct |
406 ms |
29428 KB |
Output is correct |
75 |
Correct |
416 ms |
29376 KB |
Output is correct |
76 |
Correct |
625 ms |
28272 KB |
Output is correct |
77 |
Correct |
589 ms |
26612 KB |
Output is correct |
78 |
Correct |
800 ms |
31812 KB |
Output is correct |
79 |
Correct |
835 ms |
29580 KB |
Output is correct |
80 |
Correct |
813 ms |
36676 KB |
Output is correct |
81 |
Correct |
799 ms |
33088 KB |
Output is correct |
82 |
Correct |
801 ms |
33504 KB |
Output is correct |
83 |
Correct |
814 ms |
30072 KB |
Output is correct |
84 |
Correct |
800 ms |
35884 KB |
Output is correct |
85 |
Correct |
783 ms |
34232 KB |
Output is correct |
86 |
Correct |
812 ms |
29564 KB |
Output is correct |
87 |
Correct |
794 ms |
31572 KB |
Output is correct |
88 |
Correct |
709 ms |
32328 KB |
Output is correct |
89 |
Correct |
696 ms |
28372 KB |
Output is correct |
90 |
Correct |
678 ms |
28100 KB |
Output is correct |
91 |
Correct |
689 ms |
30636 KB |
Output is correct |
92 |
Correct |
686 ms |
29180 KB |
Output is correct |
93 |
Correct |
694 ms |
28740 KB |
Output is correct |
94 |
Correct |
688 ms |
28260 KB |
Output is correct |
95 |
Correct |
720 ms |
29764 KB |
Output is correct |
96 |
Correct |
706 ms |
28532 KB |
Output is correct |
97 |
Correct |
691 ms |
30516 KB |
Output is correct |