#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], zle[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){
zle[c]=1;
for(int u:V)vis[u]=0;
return;
}
if(par[v] && !vis[typ[par[v]]]){
if(zle[typ[par[v]]]){
for(int u:V)vis[u]=0;
return;
}
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++){
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9704 KB |
Output is correct |
3 |
Correct |
5 ms |
9704 KB |
Output is correct |
4 |
Correct |
5 ms |
9728 KB |
Output is correct |
5 |
Correct |
5 ms |
9644 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9708 KB |
Output is correct |
9 |
Correct |
6 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9704 KB |
Output is correct |
3 |
Correct |
5 ms |
9704 KB |
Output is correct |
4 |
Correct |
5 ms |
9728 KB |
Output is correct |
5 |
Correct |
5 ms |
9644 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9708 KB |
Output is correct |
9 |
Correct |
6 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
8 ms |
9940 KB |
Output is correct |
12 |
Correct |
8 ms |
9812 KB |
Output is correct |
13 |
Correct |
7 ms |
9812 KB |
Output is correct |
14 |
Correct |
10 ms |
9812 KB |
Output is correct |
15 |
Correct |
8 ms |
9876 KB |
Output is correct |
16 |
Correct |
8 ms |
9812 KB |
Output is correct |
17 |
Correct |
8 ms |
9844 KB |
Output is correct |
18 |
Correct |
9 ms |
9976 KB |
Output is correct |
19 |
Correct |
7 ms |
9940 KB |
Output is correct |
20 |
Correct |
7 ms |
9984 KB |
Output is correct |
21 |
Correct |
7 ms |
9976 KB |
Output is correct |
22 |
Correct |
8 ms |
10068 KB |
Output is correct |
23 |
Correct |
9 ms |
9972 KB |
Output is correct |
24 |
Correct |
7 ms |
9848 KB |
Output is correct |
25 |
Correct |
8 ms |
9976 KB |
Output is correct |
26 |
Correct |
7 ms |
9976 KB |
Output is correct |
27 |
Correct |
8 ms |
9940 KB |
Output is correct |
28 |
Correct |
11 ms |
9924 KB |
Output is correct |
29 |
Correct |
9 ms |
9972 KB |
Output is correct |
30 |
Correct |
8 ms |
9884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
592 ms |
43320 KB |
Output is correct |
2 |
Correct |
317 ms |
43964 KB |
Output is correct |
3 |
Correct |
565 ms |
42996 KB |
Output is correct |
4 |
Correct |
303 ms |
44084 KB |
Output is correct |
5 |
Correct |
594 ms |
39296 KB |
Output is correct |
6 |
Correct |
284 ms |
43952 KB |
Output is correct |
7 |
Correct |
590 ms |
39696 KB |
Output is correct |
8 |
Correct |
307 ms |
43572 KB |
Output is correct |
9 |
Correct |
702 ms |
37564 KB |
Output is correct |
10 |
Correct |
654 ms |
34716 KB |
Output is correct |
11 |
Correct |
651 ms |
38100 KB |
Output is correct |
12 |
Correct |
639 ms |
41152 KB |
Output is correct |
13 |
Correct |
643 ms |
34012 KB |
Output is correct |
14 |
Correct |
692 ms |
41384 KB |
Output is correct |
15 |
Correct |
661 ms |
40968 KB |
Output is correct |
16 |
Correct |
704 ms |
34960 KB |
Output is correct |
17 |
Correct |
628 ms |
35632 KB |
Output is correct |
18 |
Correct |
681 ms |
35868 KB |
Output is correct |
19 |
Correct |
698 ms |
39724 KB |
Output is correct |
20 |
Correct |
736 ms |
42368 KB |
Output is correct |
21 |
Correct |
6 ms |
9684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9704 KB |
Output is correct |
3 |
Correct |
5 ms |
9704 KB |
Output is correct |
4 |
Correct |
5 ms |
9728 KB |
Output is correct |
5 |
Correct |
5 ms |
9644 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9708 KB |
Output is correct |
9 |
Correct |
6 ms |
9684 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
8 ms |
9940 KB |
Output is correct |
12 |
Correct |
8 ms |
9812 KB |
Output is correct |
13 |
Correct |
7 ms |
9812 KB |
Output is correct |
14 |
Correct |
10 ms |
9812 KB |
Output is correct |
15 |
Correct |
8 ms |
9876 KB |
Output is correct |
16 |
Correct |
8 ms |
9812 KB |
Output is correct |
17 |
Correct |
8 ms |
9844 KB |
Output is correct |
18 |
Correct |
9 ms |
9976 KB |
Output is correct |
19 |
Correct |
7 ms |
9940 KB |
Output is correct |
20 |
Correct |
7 ms |
9984 KB |
Output is correct |
21 |
Correct |
7 ms |
9976 KB |
Output is correct |
22 |
Correct |
8 ms |
10068 KB |
Output is correct |
23 |
Correct |
9 ms |
9972 KB |
Output is correct |
24 |
Correct |
7 ms |
9848 KB |
Output is correct |
25 |
Correct |
8 ms |
9976 KB |
Output is correct |
26 |
Correct |
7 ms |
9976 KB |
Output is correct |
27 |
Correct |
8 ms |
9940 KB |
Output is correct |
28 |
Correct |
11 ms |
9924 KB |
Output is correct |
29 |
Correct |
9 ms |
9972 KB |
Output is correct |
30 |
Correct |
8 ms |
9884 KB |
Output is correct |
31 |
Correct |
592 ms |
43320 KB |
Output is correct |
32 |
Correct |
317 ms |
43964 KB |
Output is correct |
33 |
Correct |
565 ms |
42996 KB |
Output is correct |
34 |
Correct |
303 ms |
44084 KB |
Output is correct |
35 |
Correct |
594 ms |
39296 KB |
Output is correct |
36 |
Correct |
284 ms |
43952 KB |
Output is correct |
37 |
Correct |
590 ms |
39696 KB |
Output is correct |
38 |
Correct |
307 ms |
43572 KB |
Output is correct |
39 |
Correct |
702 ms |
37564 KB |
Output is correct |
40 |
Correct |
654 ms |
34716 KB |
Output is correct |
41 |
Correct |
651 ms |
38100 KB |
Output is correct |
42 |
Correct |
639 ms |
41152 KB |
Output is correct |
43 |
Correct |
643 ms |
34012 KB |
Output is correct |
44 |
Correct |
692 ms |
41384 KB |
Output is correct |
45 |
Correct |
661 ms |
40968 KB |
Output is correct |
46 |
Correct |
704 ms |
34960 KB |
Output is correct |
47 |
Correct |
628 ms |
35632 KB |
Output is correct |
48 |
Correct |
681 ms |
35868 KB |
Output is correct |
49 |
Correct |
698 ms |
39724 KB |
Output is correct |
50 |
Correct |
736 ms |
42368 KB |
Output is correct |
51 |
Correct |
6 ms |
9684 KB |
Output is correct |
52 |
Correct |
589 ms |
22944 KB |
Output is correct |
53 |
Correct |
516 ms |
23272 KB |
Output is correct |
54 |
Correct |
576 ms |
23260 KB |
Output is correct |
55 |
Correct |
510 ms |
23272 KB |
Output is correct |
56 |
Correct |
600 ms |
23232 KB |
Output is correct |
57 |
Correct |
485 ms |
23220 KB |
Output is correct |
58 |
Correct |
491 ms |
27692 KB |
Output is correct |
59 |
Correct |
479 ms |
28160 KB |
Output is correct |
60 |
Correct |
544 ms |
27204 KB |
Output is correct |
61 |
Correct |
617 ms |
26692 KB |
Output is correct |
62 |
Correct |
313 ms |
43956 KB |
Output is correct |
63 |
Correct |
280 ms |
43944 KB |
Output is correct |
64 |
Correct |
276 ms |
43532 KB |
Output is correct |
65 |
Correct |
311 ms |
43920 KB |
Output is correct |
66 |
Correct |
370 ms |
33568 KB |
Output is correct |
67 |
Correct |
413 ms |
33388 KB |
Output is correct |
68 |
Correct |
392 ms |
33608 KB |
Output is correct |
69 |
Correct |
379 ms |
33404 KB |
Output is correct |
70 |
Correct |
403 ms |
33436 KB |
Output is correct |
71 |
Correct |
437 ms |
33396 KB |
Output is correct |
72 |
Correct |
428 ms |
33396 KB |
Output is correct |
73 |
Correct |
428 ms |
32436 KB |
Output is correct |
74 |
Correct |
418 ms |
33412 KB |
Output is correct |
75 |
Correct |
392 ms |
33508 KB |
Output is correct |
76 |
Correct |
583 ms |
32080 KB |
Output is correct |
77 |
Correct |
601 ms |
29880 KB |
Output is correct |
78 |
Correct |
671 ms |
35920 KB |
Output is correct |
79 |
Correct |
675 ms |
33460 KB |
Output is correct |
80 |
Correct |
614 ms |
42108 KB |
Output is correct |
81 |
Correct |
632 ms |
37812 KB |
Output is correct |
82 |
Correct |
659 ms |
37916 KB |
Output is correct |
83 |
Correct |
682 ms |
33944 KB |
Output is correct |
84 |
Correct |
680 ms |
40876 KB |
Output is correct |
85 |
Correct |
659 ms |
38804 KB |
Output is correct |
86 |
Correct |
680 ms |
32920 KB |
Output is correct |
87 |
Correct |
642 ms |
35028 KB |
Output is correct |
88 |
Correct |
607 ms |
36240 KB |
Output is correct |
89 |
Correct |
608 ms |
31420 KB |
Output is correct |
90 |
Correct |
605 ms |
31228 KB |
Output is correct |
91 |
Correct |
579 ms |
34148 KB |
Output is correct |
92 |
Correct |
579 ms |
32708 KB |
Output is correct |
93 |
Correct |
596 ms |
32096 KB |
Output is correct |
94 |
Correct |
641 ms |
31396 KB |
Output is correct |
95 |
Correct |
590 ms |
33196 KB |
Output is correct |
96 |
Correct |
627 ms |
31596 KB |
Output is correct |
97 |
Correct |
665 ms |
34196 KB |
Output is correct |