#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){
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){
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]){
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]]);
}
}
}
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(){
ios_base::sync_with_stdio(0);cin.tie(0);
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);
rek(1, n);
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++){
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9692 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 ms |
9684 KB |
Output is correct |
9 |
Correct |
5 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 |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9692 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 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 |
7 ms |
9812 KB |
Output is correct |
12 |
Correct |
8 ms |
9972 KB |
Output is correct |
13 |
Correct |
8 ms |
9812 KB |
Output is correct |
14 |
Correct |
7 ms |
9900 KB |
Output is correct |
15 |
Correct |
7 ms |
9960 KB |
Output is correct |
16 |
Correct |
7 ms |
9812 KB |
Output is correct |
17 |
Correct |
8 ms |
9940 KB |
Output is correct |
18 |
Correct |
6 ms |
9940 KB |
Output is correct |
19 |
Correct |
6 ms |
9940 KB |
Output is correct |
20 |
Correct |
8 ms |
9888 KB |
Output is correct |
21 |
Correct |
6 ms |
9940 KB |
Output is correct |
22 |
Correct |
9 ms |
9992 KB |
Output is correct |
23 |
Correct |
9 ms |
9904 KB |
Output is correct |
24 |
Correct |
7 ms |
9948 KB |
Output is correct |
25 |
Correct |
9 ms |
9896 KB |
Output is correct |
26 |
Correct |
10 ms |
9948 KB |
Output is correct |
27 |
Correct |
7 ms |
9940 KB |
Output is correct |
28 |
Correct |
7 ms |
9936 KB |
Output is correct |
29 |
Correct |
7 ms |
9940 KB |
Output is correct |
30 |
Correct |
7 ms |
9940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
579 ms |
42812 KB |
Output is correct |
2 |
Correct |
190 ms |
43676 KB |
Output is correct |
3 |
Correct |
530 ms |
42656 KB |
Output is correct |
4 |
Correct |
185 ms |
43732 KB |
Output is correct |
5 |
Correct |
528 ms |
39100 KB |
Output is correct |
6 |
Correct |
192 ms |
43700 KB |
Output is correct |
7 |
Correct |
568 ms |
39588 KB |
Output is correct |
8 |
Correct |
223 ms |
43228 KB |
Output is correct |
9 |
Correct |
693 ms |
37292 KB |
Output is correct |
10 |
Correct |
676 ms |
34400 KB |
Output is correct |
11 |
Correct |
726 ms |
37876 KB |
Output is correct |
12 |
Correct |
708 ms |
41116 KB |
Output is correct |
13 |
Correct |
660 ms |
33960 KB |
Output is correct |
14 |
Correct |
676 ms |
41516 KB |
Output is correct |
15 |
Correct |
673 ms |
41080 KB |
Output is correct |
16 |
Correct |
643 ms |
35152 KB |
Output is correct |
17 |
Correct |
556 ms |
35824 KB |
Output is correct |
18 |
Correct |
524 ms |
36028 KB |
Output is correct |
19 |
Correct |
529 ms |
39860 KB |
Output is correct |
20 |
Correct |
557 ms |
42416 KB |
Output is correct |
21 |
Correct |
6 ms |
9812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9692 KB |
Output is correct |
7 |
Correct |
5 ms |
9684 KB |
Output is correct |
8 |
Correct |
5 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 |
7 ms |
9812 KB |
Output is correct |
12 |
Correct |
8 ms |
9972 KB |
Output is correct |
13 |
Correct |
8 ms |
9812 KB |
Output is correct |
14 |
Correct |
7 ms |
9900 KB |
Output is correct |
15 |
Correct |
7 ms |
9960 KB |
Output is correct |
16 |
Correct |
7 ms |
9812 KB |
Output is correct |
17 |
Correct |
8 ms |
9940 KB |
Output is correct |
18 |
Correct |
6 ms |
9940 KB |
Output is correct |
19 |
Correct |
6 ms |
9940 KB |
Output is correct |
20 |
Correct |
8 ms |
9888 KB |
Output is correct |
21 |
Correct |
6 ms |
9940 KB |
Output is correct |
22 |
Correct |
9 ms |
9992 KB |
Output is correct |
23 |
Correct |
9 ms |
9904 KB |
Output is correct |
24 |
Correct |
7 ms |
9948 KB |
Output is correct |
25 |
Correct |
9 ms |
9896 KB |
Output is correct |
26 |
Correct |
10 ms |
9948 KB |
Output is correct |
27 |
Correct |
7 ms |
9940 KB |
Output is correct |
28 |
Correct |
7 ms |
9936 KB |
Output is correct |
29 |
Correct |
7 ms |
9940 KB |
Output is correct |
30 |
Correct |
7 ms |
9940 KB |
Output is correct |
31 |
Correct |
579 ms |
42812 KB |
Output is correct |
32 |
Correct |
190 ms |
43676 KB |
Output is correct |
33 |
Correct |
530 ms |
42656 KB |
Output is correct |
34 |
Correct |
185 ms |
43732 KB |
Output is correct |
35 |
Correct |
528 ms |
39100 KB |
Output is correct |
36 |
Correct |
192 ms |
43700 KB |
Output is correct |
37 |
Correct |
568 ms |
39588 KB |
Output is correct |
38 |
Correct |
223 ms |
43228 KB |
Output is correct |
39 |
Correct |
693 ms |
37292 KB |
Output is correct |
40 |
Correct |
676 ms |
34400 KB |
Output is correct |
41 |
Correct |
726 ms |
37876 KB |
Output is correct |
42 |
Correct |
708 ms |
41116 KB |
Output is correct |
43 |
Correct |
660 ms |
33960 KB |
Output is correct |
44 |
Correct |
676 ms |
41516 KB |
Output is correct |
45 |
Correct |
673 ms |
41080 KB |
Output is correct |
46 |
Correct |
643 ms |
35152 KB |
Output is correct |
47 |
Correct |
556 ms |
35824 KB |
Output is correct |
48 |
Correct |
524 ms |
36028 KB |
Output is correct |
49 |
Correct |
529 ms |
39860 KB |
Output is correct |
50 |
Correct |
557 ms |
42416 KB |
Output is correct |
51 |
Correct |
6 ms |
9812 KB |
Output is correct |
52 |
Correct |
408 ms |
23084 KB |
Output is correct |
53 |
Correct |
456 ms |
22976 KB |
Output is correct |
54 |
Correct |
417 ms |
23036 KB |
Output is correct |
55 |
Correct |
445 ms |
22980 KB |
Output is correct |
56 |
Correct |
482 ms |
22964 KB |
Output is correct |
57 |
Correct |
397 ms |
23036 KB |
Output is correct |
58 |
Correct |
380 ms |
27488 KB |
Output is correct |
59 |
Correct |
360 ms |
27936 KB |
Output is correct |
60 |
Correct |
475 ms |
27092 KB |
Output is correct |
61 |
Correct |
518 ms |
26492 KB |
Output is correct |
62 |
Correct |
165 ms |
43744 KB |
Output is correct |
63 |
Correct |
167 ms |
43736 KB |
Output is correct |
64 |
Correct |
185 ms |
43288 KB |
Output is correct |
65 |
Correct |
176 ms |
43696 KB |
Output is correct |
66 |
Correct |
323 ms |
33248 KB |
Output is correct |
67 |
Correct |
277 ms |
33064 KB |
Output is correct |
68 |
Correct |
384 ms |
33280 KB |
Output is correct |
69 |
Correct |
289 ms |
33212 KB |
Output is correct |
70 |
Correct |
311 ms |
33236 KB |
Output is correct |
71 |
Correct |
304 ms |
33168 KB |
Output is correct |
72 |
Correct |
358 ms |
33208 KB |
Output is correct |
73 |
Correct |
277 ms |
32148 KB |
Output is correct |
74 |
Correct |
300 ms |
33248 KB |
Output is correct |
75 |
Correct |
363 ms |
33288 KB |
Output is correct |
76 |
Correct |
493 ms |
31800 KB |
Output is correct |
77 |
Correct |
441 ms |
29628 KB |
Output is correct |
78 |
Correct |
545 ms |
35708 KB |
Output is correct |
79 |
Correct |
577 ms |
33248 KB |
Output is correct |
80 |
Correct |
532 ms |
41792 KB |
Output is correct |
81 |
Correct |
529 ms |
37508 KB |
Output is correct |
82 |
Correct |
543 ms |
37724 KB |
Output is correct |
83 |
Correct |
557 ms |
33696 KB |
Output is correct |
84 |
Correct |
537 ms |
40744 KB |
Output is correct |
85 |
Correct |
539 ms |
38836 KB |
Output is correct |
86 |
Correct |
560 ms |
32960 KB |
Output is correct |
87 |
Correct |
537 ms |
35232 KB |
Output is correct |
88 |
Correct |
458 ms |
36428 KB |
Output is correct |
89 |
Correct |
427 ms |
31576 KB |
Output is correct |
90 |
Correct |
448 ms |
31400 KB |
Output is correct |
91 |
Correct |
472 ms |
34308 KB |
Output is correct |
92 |
Correct |
494 ms |
32664 KB |
Output is correct |
93 |
Correct |
459 ms |
32268 KB |
Output is correct |
94 |
Correct |
457 ms |
31548 KB |
Output is correct |
95 |
Correct |
469 ms |
33284 KB |
Output is correct |
96 |
Correct |
457 ms |
31840 KB |
Output is correct |
97 |
Correct |
445 ms |
34316 KB |
Output is correct |