#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
int blo[N], col[N], par[N], vis[N], typ[N];
vector<int> co[N];
vector<int> E[N];
void color(int v, int p, int c){
col[v]=c;
for(int u:E[v]){
if(!blo[u] && u!=p){
color(u, v, c);
}
}
}
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;
}
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++);
}
cout<<ans-1<<"\n";
}
Compilation message
capital_city.cpp: In function 'void solve(int, int)':
capital_city.cpp:38:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | for(int i=0; i<V.size(); i++){
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
6 ms |
9684 KB |
Output is correct |
4 |
Correct |
6 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9604 KB |
Output is correct |
7 |
Correct |
5 ms |
9676 KB |
Output is correct |
8 |
Correct |
7 ms |
9708 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 |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
6 ms |
9684 KB |
Output is correct |
4 |
Correct |
6 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9604 KB |
Output is correct |
7 |
Correct |
5 ms |
9676 KB |
Output is correct |
8 |
Correct |
7 ms |
9708 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
19 ms |
9812 KB |
Output is correct |
12 |
Correct |
17 ms |
9852 KB |
Output is correct |
13 |
Correct |
21 ms |
9876 KB |
Output is correct |
14 |
Correct |
23 ms |
9848 KB |
Output is correct |
15 |
Correct |
9 ms |
9844 KB |
Output is correct |
16 |
Correct |
9 ms |
9812 KB |
Output is correct |
17 |
Correct |
9 ms |
9812 KB |
Output is correct |
18 |
Correct |
12 ms |
9940 KB |
Output is correct |
19 |
Correct |
12 ms |
9912 KB |
Output is correct |
20 |
Correct |
13 ms |
9820 KB |
Output is correct |
21 |
Correct |
9 ms |
9940 KB |
Output is correct |
22 |
Correct |
9 ms |
9940 KB |
Output is correct |
23 |
Correct |
8 ms |
9940 KB |
Output is correct |
24 |
Correct |
10 ms |
9812 KB |
Output is correct |
25 |
Correct |
8 ms |
9940 KB |
Output is correct |
26 |
Correct |
9 ms |
9940 KB |
Output is correct |
27 |
Correct |
9 ms |
9980 KB |
Output is correct |
28 |
Correct |
9 ms |
9940 KB |
Output is correct |
29 |
Correct |
8 ms |
9904 KB |
Output is correct |
30 |
Correct |
8 ms |
9848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
794 ms |
31656 KB |
Output is correct |
2 |
Correct |
399 ms |
31716 KB |
Output is correct |
3 |
Correct |
780 ms |
36244 KB |
Output is correct |
4 |
Correct |
430 ms |
31532 KB |
Output is correct |
5 |
Correct |
869 ms |
35572 KB |
Output is correct |
6 |
Correct |
397 ms |
34572 KB |
Output is correct |
7 |
Correct |
733 ms |
29708 KB |
Output is correct |
8 |
Correct |
397 ms |
29592 KB |
Output is correct |
9 |
Correct |
1010 ms |
34820 KB |
Output is correct |
10 |
Correct |
943 ms |
35240 KB |
Output is correct |
11 |
Correct |
980 ms |
36968 KB |
Output is correct |
12 |
Correct |
934 ms |
32956 KB |
Output is correct |
13 |
Correct |
881 ms |
32956 KB |
Output is correct |
14 |
Correct |
904 ms |
34900 KB |
Output is correct |
15 |
Correct |
897 ms |
35120 KB |
Output is correct |
16 |
Correct |
929 ms |
33076 KB |
Output is correct |
17 |
Correct |
971 ms |
37672 KB |
Output is correct |
18 |
Correct |
993 ms |
38204 KB |
Output is correct |
19 |
Correct |
954 ms |
33672 KB |
Output is correct |
20 |
Correct |
979 ms |
34804 KB |
Output is correct |
21 |
Correct |
5 ms |
9716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
6 ms |
9684 KB |
Output is correct |
4 |
Correct |
6 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9604 KB |
Output is correct |
7 |
Correct |
5 ms |
9676 KB |
Output is correct |
8 |
Correct |
7 ms |
9708 KB |
Output is correct |
9 |
Correct |
5 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
19 ms |
9812 KB |
Output is correct |
12 |
Correct |
17 ms |
9852 KB |
Output is correct |
13 |
Correct |
21 ms |
9876 KB |
Output is correct |
14 |
Correct |
23 ms |
9848 KB |
Output is correct |
15 |
Correct |
9 ms |
9844 KB |
Output is correct |
16 |
Correct |
9 ms |
9812 KB |
Output is correct |
17 |
Correct |
9 ms |
9812 KB |
Output is correct |
18 |
Correct |
12 ms |
9940 KB |
Output is correct |
19 |
Correct |
12 ms |
9912 KB |
Output is correct |
20 |
Correct |
13 ms |
9820 KB |
Output is correct |
21 |
Correct |
9 ms |
9940 KB |
Output is correct |
22 |
Correct |
9 ms |
9940 KB |
Output is correct |
23 |
Correct |
8 ms |
9940 KB |
Output is correct |
24 |
Correct |
10 ms |
9812 KB |
Output is correct |
25 |
Correct |
8 ms |
9940 KB |
Output is correct |
26 |
Correct |
9 ms |
9940 KB |
Output is correct |
27 |
Correct |
9 ms |
9980 KB |
Output is correct |
28 |
Correct |
9 ms |
9940 KB |
Output is correct |
29 |
Correct |
8 ms |
9904 KB |
Output is correct |
30 |
Correct |
8 ms |
9848 KB |
Output is correct |
31 |
Correct |
794 ms |
31656 KB |
Output is correct |
32 |
Correct |
399 ms |
31716 KB |
Output is correct |
33 |
Correct |
780 ms |
36244 KB |
Output is correct |
34 |
Correct |
430 ms |
31532 KB |
Output is correct |
35 |
Correct |
869 ms |
35572 KB |
Output is correct |
36 |
Correct |
397 ms |
34572 KB |
Output is correct |
37 |
Correct |
733 ms |
29708 KB |
Output is correct |
38 |
Correct |
397 ms |
29592 KB |
Output is correct |
39 |
Correct |
1010 ms |
34820 KB |
Output is correct |
40 |
Correct |
943 ms |
35240 KB |
Output is correct |
41 |
Correct |
980 ms |
36968 KB |
Output is correct |
42 |
Correct |
934 ms |
32956 KB |
Output is correct |
43 |
Correct |
881 ms |
32956 KB |
Output is correct |
44 |
Correct |
904 ms |
34900 KB |
Output is correct |
45 |
Correct |
897 ms |
35120 KB |
Output is correct |
46 |
Correct |
929 ms |
33076 KB |
Output is correct |
47 |
Correct |
971 ms |
37672 KB |
Output is correct |
48 |
Correct |
993 ms |
38204 KB |
Output is correct |
49 |
Correct |
954 ms |
33672 KB |
Output is correct |
50 |
Correct |
979 ms |
34804 KB |
Output is correct |
51 |
Correct |
5 ms |
9716 KB |
Output is correct |
52 |
Execution timed out |
3069 ms |
24944 KB |
Time limit exceeded |
53 |
Halted |
0 ms |
0 KB |
- |