#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);
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;
}
}
// 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(){
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";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
9700 KB |
Output is correct |
2 |
Correct |
7 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 |
9704 KB |
Output is correct |
6 |
Correct |
7 ms |
9704 KB |
Output is correct |
7 |
Correct |
6 ms |
9676 KB |
Output is correct |
8 |
Correct |
7 ms |
9720 KB |
Output is correct |
9 |
Correct |
6 ms |
9676 KB |
Output is correct |
10 |
Correct |
7 ms |
9712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
9700 KB |
Output is correct |
2 |
Correct |
7 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 |
9704 KB |
Output is correct |
6 |
Correct |
7 ms |
9704 KB |
Output is correct |
7 |
Correct |
6 ms |
9676 KB |
Output is correct |
8 |
Correct |
7 ms |
9720 KB |
Output is correct |
9 |
Correct |
6 ms |
9676 KB |
Output is correct |
10 |
Correct |
7 ms |
9712 KB |
Output is correct |
11 |
Correct |
9 ms |
9844 KB |
Output is correct |
12 |
Correct |
10 ms |
9804 KB |
Output is correct |
13 |
Correct |
9 ms |
9804 KB |
Output is correct |
14 |
Correct |
9 ms |
9848 KB |
Output is correct |
15 |
Correct |
11 ms |
9804 KB |
Output is correct |
16 |
Correct |
10 ms |
9804 KB |
Output is correct |
17 |
Correct |
9 ms |
9932 KB |
Output is correct |
18 |
Correct |
11 ms |
9848 KB |
Output is correct |
19 |
Correct |
10 ms |
9932 KB |
Output is correct |
20 |
Correct |
11 ms |
9932 KB |
Output is correct |
21 |
Correct |
9 ms |
9840 KB |
Output is correct |
22 |
Correct |
9 ms |
9932 KB |
Output is correct |
23 |
Correct |
10 ms |
9932 KB |
Output is correct |
24 |
Correct |
10 ms |
9816 KB |
Output is correct |
25 |
Correct |
10 ms |
9932 KB |
Output is correct |
26 |
Correct |
10 ms |
10008 KB |
Output is correct |
27 |
Correct |
11 ms |
9932 KB |
Output is correct |
28 |
Correct |
10 ms |
9932 KB |
Output is correct |
29 |
Correct |
20 ms |
9844 KB |
Output is correct |
30 |
Correct |
11 ms |
9932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
868 ms |
40728 KB |
Output is correct |
2 |
Correct |
464 ms |
41208 KB |
Output is correct |
3 |
Correct |
789 ms |
40408 KB |
Output is correct |
4 |
Correct |
451 ms |
41228 KB |
Output is correct |
5 |
Correct |
855 ms |
37620 KB |
Output is correct |
6 |
Correct |
508 ms |
41128 KB |
Output is correct |
7 |
Correct |
911 ms |
37964 KB |
Output is correct |
8 |
Correct |
477 ms |
41004 KB |
Output is correct |
9 |
Correct |
1025 ms |
36264 KB |
Output is correct |
10 |
Correct |
1057 ms |
33824 KB |
Output is correct |
11 |
Correct |
1068 ms |
36684 KB |
Output is correct |
12 |
Correct |
1111 ms |
39328 KB |
Output is correct |
13 |
Correct |
1043 ms |
33392 KB |
Output is correct |
14 |
Correct |
1098 ms |
39684 KB |
Output is correct |
15 |
Correct |
1020 ms |
39288 KB |
Output is correct |
16 |
Correct |
1004 ms |
34352 KB |
Output is correct |
17 |
Correct |
969 ms |
34808 KB |
Output is correct |
18 |
Correct |
1142 ms |
35100 KB |
Output is correct |
19 |
Correct |
1060 ms |
38172 KB |
Output is correct |
20 |
Correct |
985 ms |
40500 KB |
Output is correct |
21 |
Correct |
6 ms |
9676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
9700 KB |
Output is correct |
2 |
Correct |
7 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 |
9704 KB |
Output is correct |
6 |
Correct |
7 ms |
9704 KB |
Output is correct |
7 |
Correct |
6 ms |
9676 KB |
Output is correct |
8 |
Correct |
7 ms |
9720 KB |
Output is correct |
9 |
Correct |
6 ms |
9676 KB |
Output is correct |
10 |
Correct |
7 ms |
9712 KB |
Output is correct |
11 |
Correct |
9 ms |
9844 KB |
Output is correct |
12 |
Correct |
10 ms |
9804 KB |
Output is correct |
13 |
Correct |
9 ms |
9804 KB |
Output is correct |
14 |
Correct |
9 ms |
9848 KB |
Output is correct |
15 |
Correct |
11 ms |
9804 KB |
Output is correct |
16 |
Correct |
10 ms |
9804 KB |
Output is correct |
17 |
Correct |
9 ms |
9932 KB |
Output is correct |
18 |
Correct |
11 ms |
9848 KB |
Output is correct |
19 |
Correct |
10 ms |
9932 KB |
Output is correct |
20 |
Correct |
11 ms |
9932 KB |
Output is correct |
21 |
Correct |
9 ms |
9840 KB |
Output is correct |
22 |
Correct |
9 ms |
9932 KB |
Output is correct |
23 |
Correct |
10 ms |
9932 KB |
Output is correct |
24 |
Correct |
10 ms |
9816 KB |
Output is correct |
25 |
Correct |
10 ms |
9932 KB |
Output is correct |
26 |
Correct |
10 ms |
10008 KB |
Output is correct |
27 |
Correct |
11 ms |
9932 KB |
Output is correct |
28 |
Correct |
10 ms |
9932 KB |
Output is correct |
29 |
Correct |
20 ms |
9844 KB |
Output is correct |
30 |
Correct |
11 ms |
9932 KB |
Output is correct |
31 |
Correct |
868 ms |
40728 KB |
Output is correct |
32 |
Correct |
464 ms |
41208 KB |
Output is correct |
33 |
Correct |
789 ms |
40408 KB |
Output is correct |
34 |
Correct |
451 ms |
41228 KB |
Output is correct |
35 |
Correct |
855 ms |
37620 KB |
Output is correct |
36 |
Correct |
508 ms |
41128 KB |
Output is correct |
37 |
Correct |
911 ms |
37964 KB |
Output is correct |
38 |
Correct |
477 ms |
41004 KB |
Output is correct |
39 |
Correct |
1025 ms |
36264 KB |
Output is correct |
40 |
Correct |
1057 ms |
33824 KB |
Output is correct |
41 |
Correct |
1068 ms |
36684 KB |
Output is correct |
42 |
Correct |
1111 ms |
39328 KB |
Output is correct |
43 |
Correct |
1043 ms |
33392 KB |
Output is correct |
44 |
Correct |
1098 ms |
39684 KB |
Output is correct |
45 |
Correct |
1020 ms |
39288 KB |
Output is correct |
46 |
Correct |
1004 ms |
34352 KB |
Output is correct |
47 |
Correct |
969 ms |
34808 KB |
Output is correct |
48 |
Correct |
1142 ms |
35100 KB |
Output is correct |
49 |
Correct |
1060 ms |
38172 KB |
Output is correct |
50 |
Correct |
985 ms |
40500 KB |
Output is correct |
51 |
Correct |
6 ms |
9676 KB |
Output is correct |
52 |
Correct |
902 ms |
24196 KB |
Output is correct |
53 |
Correct |
800 ms |
24260 KB |
Output is correct |
54 |
Correct |
795 ms |
24164 KB |
Output is correct |
55 |
Correct |
880 ms |
24272 KB |
Output is correct |
56 |
Correct |
838 ms |
24288 KB |
Output is correct |
57 |
Correct |
765 ms |
24172 KB |
Output is correct |
58 |
Correct |
694 ms |
27844 KB |
Output is correct |
59 |
Correct |
708 ms |
28044 KB |
Output is correct |
60 |
Correct |
882 ms |
27464 KB |
Output is correct |
61 |
Correct |
970 ms |
27208 KB |
Output is correct |
62 |
Correct |
454 ms |
41232 KB |
Output is correct |
63 |
Correct |
426 ms |
41324 KB |
Output is correct |
64 |
Correct |
431 ms |
41016 KB |
Output is correct |
65 |
Correct |
426 ms |
41392 KB |
Output is correct |
66 |
Correct |
597 ms |
32788 KB |
Output is correct |
67 |
Correct |
610 ms |
32548 KB |
Output is correct |
68 |
Correct |
643 ms |
32704 KB |
Output is correct |
69 |
Correct |
548 ms |
32716 KB |
Output is correct |
70 |
Correct |
543 ms |
32572 KB |
Output is correct |
71 |
Correct |
662 ms |
32624 KB |
Output is correct |
72 |
Correct |
564 ms |
32572 KB |
Output is correct |
73 |
Correct |
579 ms |
31808 KB |
Output is correct |
74 |
Correct |
616 ms |
32720 KB |
Output is correct |
75 |
Correct |
596 ms |
32832 KB |
Output is correct |
76 |
Correct |
813 ms |
31600 KB |
Output is correct |
77 |
Correct |
820 ms |
29672 KB |
Output is correct |
78 |
Correct |
991 ms |
34744 KB |
Output is correct |
79 |
Correct |
1046 ms |
32860 KB |
Output is correct |
80 |
Correct |
1018 ms |
39952 KB |
Output is correct |
81 |
Correct |
1077 ms |
36428 KB |
Output is correct |
82 |
Correct |
1066 ms |
36584 KB |
Output is correct |
83 |
Correct |
1006 ms |
33212 KB |
Output is correct |
84 |
Correct |
978 ms |
39236 KB |
Output is correct |
85 |
Correct |
1049 ms |
37468 KB |
Output is correct |
86 |
Correct |
1004 ms |
32732 KB |
Output is correct |
87 |
Correct |
953 ms |
34460 KB |
Output is correct |
88 |
Correct |
972 ms |
35628 KB |
Output is correct |
89 |
Correct |
896 ms |
31656 KB |
Output is correct |
90 |
Correct |
901 ms |
31372 KB |
Output is correct |
91 |
Correct |
923 ms |
33820 KB |
Output is correct |
92 |
Correct |
907 ms |
32352 KB |
Output is correct |
93 |
Correct |
887 ms |
32084 KB |
Output is correct |
94 |
Correct |
890 ms |
31436 KB |
Output is correct |
95 |
Correct |
946 ms |
32952 KB |
Output is correct |
96 |
Correct |
926 ms |
31764 KB |
Output is correct |
97 |
Correct |
863 ms |
33752 KB |
Output is correct |