#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
int blo[N], col[N], par[N], vis[N], typ[N], siz[N];
vector<int> co[N];
vector<int> E[N];
int cent;
void color(int v, int p, int c, int n){
col[v]=c;
bool czy=1;
siz[v]=1;
for(int u:E[v]){
if(!blo[u] && u!=p){
color(u, v, c, n);
siz[v]+=siz[u];
if(siz[u]*2>n)czy=0;
}
}
if(siz[v]*2<n)czy=0;
if(czy)cent=v;
}
void dfs(int v){
//cout<<v<<" "<<par[v]<<col[v]<<" "<<typ[v]<<"q\n";
for(int u:E[v]){
if(u!=par[v] && !blo[u]){
par[u]=v;
dfs(u);
}
}
}
int wsk=1;
int ans=N;
void solve(int v, int id){
//cout<<v<<" "<<id<<"\n";
/*for(int u=1; u<=12; u++)if(col[u]==id){
assert(!blo[u]);
cout<<u<<" ";
}
cout<<"\n";*/
par[v]=0;
dfs(v);
vector<int> V;
V.push_back(typ[v]);
vis[typ[v]]=1;
for(int i=0; i<V.size(); i++){
int c=V[i];
for(int v:co[c]){
//cout<<v<<" "<<col[v]<<" "<<typ[v]<<"\n";
if(col[v]!=id){
for(int u:V)vis[u]=0;
return;
}
if(par[v] && !vis[typ[par[v]]]){
vis[typ[par[v]]]=1;
V.push_back(typ[par[v]]);
}
}
}
//cout<<V.size()<<"\n";
ans=min(ans, (int)V.size());
for(int u:V)vis[u]=0;
}
void rek(int v, int n){
cent=v;
color(v, 0, wsk++, n);
solve(cent, col[cent]);
blo[cent]=1;
for(int u:E[cent])if(!blo[u])rek(u, siz[u]);
}
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
int main(){
int n, k;
cin>>n>>k;
for(int i=1; i<n; i++){
int u, v;
cin>>u>>v;
E[u].push_back(v);
E[v].push_back(u);
}
for(int i=1; i<=n; i++){
cin>>typ[i];
co[typ[i]].push_back(i);
}
vector<int> kol;
for(int i=1; i<=n; i++)kol.push_back(i);
/*shuffle(kol.begin(), kol.end(), rng);
for(int i:kol){
solve(i, col[i]);
blo[i]=1;
for(int u:E[i])if(!blo[u])color(u, i, wsk++);
}*/
rek(1, n);
cout<<ans-1<<"\n";
}
Compilation message
capital_city.cpp: In function 'void solve(int, int)':
capital_city.cpp:45:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | for(int i=0; i<V.size(); i++){
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9712 KB |
Output is correct |
2 |
Correct |
6 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9708 KB |
Output is correct |
4 |
Correct |
5 ms |
9704 KB |
Output is correct |
5 |
Correct |
6 ms |
9704 KB |
Output is correct |
6 |
Correct |
6 ms |
9704 KB |
Output is correct |
7 |
Correct |
5 ms |
9708 KB |
Output is correct |
8 |
Correct |
7 ms |
9684 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9712 KB |
Output is correct |
2 |
Correct |
6 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9708 KB |
Output is correct |
4 |
Correct |
5 ms |
9704 KB |
Output is correct |
5 |
Correct |
6 ms |
9704 KB |
Output is correct |
6 |
Correct |
6 ms |
9704 KB |
Output is correct |
7 |
Correct |
5 ms |
9708 KB |
Output is correct |
8 |
Correct |
7 ms |
9684 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
8 ms |
9812 KB |
Output is correct |
12 |
Correct |
9 ms |
9812 KB |
Output is correct |
13 |
Correct |
8 ms |
9812 KB |
Output is correct |
14 |
Correct |
8 ms |
9852 KB |
Output is correct |
15 |
Correct |
7 ms |
9940 KB |
Output is correct |
16 |
Correct |
7 ms |
9812 KB |
Output is correct |
17 |
Correct |
7 ms |
9940 KB |
Output is correct |
18 |
Correct |
8 ms |
9908 KB |
Output is correct |
19 |
Correct |
8 ms |
9976 KB |
Output is correct |
20 |
Correct |
7 ms |
10068 KB |
Output is correct |
21 |
Correct |
7 ms |
9972 KB |
Output is correct |
22 |
Correct |
7 ms |
9972 KB |
Output is correct |
23 |
Correct |
8 ms |
9940 KB |
Output is correct |
24 |
Correct |
8 ms |
9940 KB |
Output is correct |
25 |
Correct |
9 ms |
9980 KB |
Output is correct |
26 |
Correct |
9 ms |
10032 KB |
Output is correct |
27 |
Correct |
9 ms |
9940 KB |
Output is correct |
28 |
Correct |
9 ms |
9932 KB |
Output is correct |
29 |
Correct |
12 ms |
9856 KB |
Output is correct |
30 |
Correct |
8 ms |
9940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
571 ms |
42812 KB |
Output is correct |
2 |
Correct |
307 ms |
43272 KB |
Output is correct |
3 |
Correct |
609 ms |
42372 KB |
Output is correct |
4 |
Correct |
308 ms |
43244 KB |
Output is correct |
5 |
Correct |
652 ms |
38796 KB |
Output is correct |
6 |
Correct |
288 ms |
43452 KB |
Output is correct |
7 |
Correct |
583 ms |
39220 KB |
Output is correct |
8 |
Correct |
288 ms |
42876 KB |
Output is correct |
9 |
Correct |
668 ms |
37040 KB |
Output is correct |
10 |
Correct |
729 ms |
34164 KB |
Output is correct |
11 |
Correct |
792 ms |
37664 KB |
Output is correct |
12 |
Correct |
824 ms |
40840 KB |
Output is correct |
13 |
Correct |
736 ms |
33928 KB |
Output is correct |
14 |
Correct |
772 ms |
41436 KB |
Output is correct |
15 |
Correct |
806 ms |
40968 KB |
Output is correct |
16 |
Correct |
820 ms |
35068 KB |
Output is correct |
17 |
Correct |
729 ms |
35624 KB |
Output is correct |
18 |
Correct |
747 ms |
35932 KB |
Output is correct |
19 |
Correct |
692 ms |
39800 KB |
Output is correct |
20 |
Correct |
711 ms |
42476 KB |
Output is correct |
21 |
Correct |
6 ms |
9728 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9712 KB |
Output is correct |
2 |
Correct |
6 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9708 KB |
Output is correct |
4 |
Correct |
5 ms |
9704 KB |
Output is correct |
5 |
Correct |
6 ms |
9704 KB |
Output is correct |
6 |
Correct |
6 ms |
9704 KB |
Output is correct |
7 |
Correct |
5 ms |
9708 KB |
Output is correct |
8 |
Correct |
7 ms |
9684 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
8 ms |
9812 KB |
Output is correct |
12 |
Correct |
9 ms |
9812 KB |
Output is correct |
13 |
Correct |
8 ms |
9812 KB |
Output is correct |
14 |
Correct |
8 ms |
9852 KB |
Output is correct |
15 |
Correct |
7 ms |
9940 KB |
Output is correct |
16 |
Correct |
7 ms |
9812 KB |
Output is correct |
17 |
Correct |
7 ms |
9940 KB |
Output is correct |
18 |
Correct |
8 ms |
9908 KB |
Output is correct |
19 |
Correct |
8 ms |
9976 KB |
Output is correct |
20 |
Correct |
7 ms |
10068 KB |
Output is correct |
21 |
Correct |
7 ms |
9972 KB |
Output is correct |
22 |
Correct |
7 ms |
9972 KB |
Output is correct |
23 |
Correct |
8 ms |
9940 KB |
Output is correct |
24 |
Correct |
8 ms |
9940 KB |
Output is correct |
25 |
Correct |
9 ms |
9980 KB |
Output is correct |
26 |
Correct |
9 ms |
10032 KB |
Output is correct |
27 |
Correct |
9 ms |
9940 KB |
Output is correct |
28 |
Correct |
9 ms |
9932 KB |
Output is correct |
29 |
Correct |
12 ms |
9856 KB |
Output is correct |
30 |
Correct |
8 ms |
9940 KB |
Output is correct |
31 |
Correct |
571 ms |
42812 KB |
Output is correct |
32 |
Correct |
307 ms |
43272 KB |
Output is correct |
33 |
Correct |
609 ms |
42372 KB |
Output is correct |
34 |
Correct |
308 ms |
43244 KB |
Output is correct |
35 |
Correct |
652 ms |
38796 KB |
Output is correct |
36 |
Correct |
288 ms |
43452 KB |
Output is correct |
37 |
Correct |
583 ms |
39220 KB |
Output is correct |
38 |
Correct |
288 ms |
42876 KB |
Output is correct |
39 |
Correct |
668 ms |
37040 KB |
Output is correct |
40 |
Correct |
729 ms |
34164 KB |
Output is correct |
41 |
Correct |
792 ms |
37664 KB |
Output is correct |
42 |
Correct |
824 ms |
40840 KB |
Output is correct |
43 |
Correct |
736 ms |
33928 KB |
Output is correct |
44 |
Correct |
772 ms |
41436 KB |
Output is correct |
45 |
Correct |
806 ms |
40968 KB |
Output is correct |
46 |
Correct |
820 ms |
35068 KB |
Output is correct |
47 |
Correct |
729 ms |
35624 KB |
Output is correct |
48 |
Correct |
747 ms |
35932 KB |
Output is correct |
49 |
Correct |
692 ms |
39800 KB |
Output is correct |
50 |
Correct |
711 ms |
42476 KB |
Output is correct |
51 |
Correct |
6 ms |
9728 KB |
Output is correct |
52 |
Correct |
533 ms |
23204 KB |
Output is correct |
53 |
Correct |
556 ms |
26112 KB |
Output is correct |
54 |
Correct |
550 ms |
26248 KB |
Output is correct |
55 |
Correct |
565 ms |
26212 KB |
Output is correct |
56 |
Correct |
572 ms |
26076 KB |
Output is correct |
57 |
Correct |
495 ms |
26116 KB |
Output is correct |
58 |
Correct |
528 ms |
30556 KB |
Output is correct |
59 |
Correct |
563 ms |
30940 KB |
Output is correct |
60 |
Correct |
591 ms |
30172 KB |
Output is correct |
61 |
Correct |
585 ms |
29624 KB |
Output is correct |
62 |
Correct |
303 ms |
46808 KB |
Output is correct |
63 |
Correct |
292 ms |
46736 KB |
Output is correct |
64 |
Correct |
293 ms |
46432 KB |
Output is correct |
65 |
Correct |
285 ms |
46636 KB |
Output is correct |
66 |
Correct |
394 ms |
36556 KB |
Output is correct |
67 |
Correct |
456 ms |
36348 KB |
Output is correct |
68 |
Correct |
446 ms |
36416 KB |
Output is correct |
69 |
Correct |
403 ms |
36512 KB |
Output is correct |
70 |
Correct |
442 ms |
36428 KB |
Output is correct |
71 |
Correct |
414 ms |
36532 KB |
Output is correct |
72 |
Correct |
435 ms |
36424 KB |
Output is correct |
73 |
Correct |
422 ms |
35388 KB |
Output is correct |
74 |
Correct |
393 ms |
36492 KB |
Output is correct |
75 |
Correct |
445 ms |
36500 KB |
Output is correct |
76 |
Correct |
612 ms |
34864 KB |
Output is correct |
77 |
Correct |
600 ms |
32636 KB |
Output is correct |
78 |
Correct |
652 ms |
38932 KB |
Output is correct |
79 |
Correct |
693 ms |
36304 KB |
Output is correct |
80 |
Correct |
728 ms |
45004 KB |
Output is correct |
81 |
Correct |
660 ms |
40908 KB |
Output is correct |
82 |
Correct |
673 ms |
40828 KB |
Output is correct |
83 |
Correct |
699 ms |
36900 KB |
Output is correct |
84 |
Correct |
728 ms |
43996 KB |
Output is correct |
85 |
Correct |
806 ms |
42068 KB |
Output is correct |
86 |
Correct |
807 ms |
36452 KB |
Output is correct |
87 |
Correct |
794 ms |
38520 KB |
Output is correct |
88 |
Correct |
759 ms |
39668 KB |
Output is correct |
89 |
Correct |
690 ms |
34912 KB |
Output is correct |
90 |
Correct |
674 ms |
34896 KB |
Output is correct |
91 |
Correct |
667 ms |
37652 KB |
Output is correct |
92 |
Correct |
634 ms |
35956 KB |
Output is correct |
93 |
Correct |
635 ms |
35708 KB |
Output is correct |
94 |
Correct |
583 ms |
34800 KB |
Output is correct |
95 |
Correct |
609 ms |
36528 KB |
Output is correct |
96 |
Correct |
609 ms |
35032 KB |
Output is correct |
97 |
Correct |
622 ms |
37544 KB |
Output is correct |