#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define double long double
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(),x.end()
#define vi vector<int>
#define vvi vector<vi>
#define vp vector<pii>
using namespace std;
const int N=2e5+5;
vector<int>g[N],co[N];
int sz[N],c[N],pr[N],up[N];
bool vis[N]{0},use[N]{0},usec[N]{0};
int get(int r){
return pr[r]==r?r:pr[r]=get(pr[r]);
}
int getsize(int u,int p){
sz[u]=1;for(auto v:g[u])if(v!=p&&!vis[v])sz[u]+=getsize(v,u);
return sz[u];
}
int getct(int u,int p,int n){
for(auto v:g[u])if(v!=p&&!vis[v]&&sz[v]*2>n)return getct(v,u,n);
return u;
}
void dfs(int u,int p){
up[u]=p;use[u]=1;for(auto v:g[u])if(v!=p&&!vis[v])dfs(v,u);
}
void rdfs(int u,int p){
pr[u]=u;up[u]=u;use[u]=0;usec[c[u]]=0;
for(auto v:g[u])if(v!=p&&!vis[v])rdfs(v,u);
}int ans=1e8;
void play(int u){
u=getct(u,u,getsize(u,u));int cnt=1;
vis[u]=1;dfs(u,u);bool ok=1;
queue<int>q;q.push(c[u]);usec[c[u]]=1;
while(!q.empty()){
int cl=q.front();q.pop();
for(int it : co[cl]){
if(!use[it]){ok=0;break;}
while(get(it)!=get(u)){
int v=get(up[it]);pr[get(it)]=v;
if(!usec[c[v]])q.push(c[v]),usec[c[v]]=1,cnt++;it=v;
}
}
}if(ok)ans=min(cnt,ans);rdfs(u,u);
for(auto v :g[u])if(!vis[v])play(v);
}
int main(){
ios_base::sync_with_stdio(0);
int n,k;cin>>n>>k;iota(pr,pr+n+1,0);
for(int i=1,u,v;i<=n-1;i++){
cin>>u>>v;g[u].pb(v);g[v].pb(u);
}for(int i=1;i<=n;i++)cin>>c[i],co[c[i]].pb(i);
play(1);cout<<ans-1;
}
Compilation message
capital_city.cpp: In function 'void play(int)':
capital_city.cpp:46:17: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
46 | if(!usec[c[v]])q.push(c[v]),usec[c[v]]=1,cnt++;it=v;
| ^~
capital_city.cpp:46:64: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
46 | if(!usec[c[v]])q.push(c[v]),usec[c[v]]=1,cnt++;it=v;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
13404 KB |
Output is correct |
2 |
Correct |
4 ms |
13144 KB |
Output is correct |
3 |
Correct |
2 ms |
13404 KB |
Output is correct |
4 |
Correct |
3 ms |
13396 KB |
Output is correct |
5 |
Correct |
3 ms |
13404 KB |
Output is correct |
6 |
Correct |
3 ms |
13404 KB |
Output is correct |
7 |
Correct |
4 ms |
13400 KB |
Output is correct |
8 |
Correct |
5 ms |
13392 KB |
Output is correct |
9 |
Correct |
3 ms |
13404 KB |
Output is correct |
10 |
Correct |
3 ms |
13392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
13404 KB |
Output is correct |
2 |
Correct |
4 ms |
13144 KB |
Output is correct |
3 |
Correct |
2 ms |
13404 KB |
Output is correct |
4 |
Correct |
3 ms |
13396 KB |
Output is correct |
5 |
Correct |
3 ms |
13404 KB |
Output is correct |
6 |
Correct |
3 ms |
13404 KB |
Output is correct |
7 |
Correct |
4 ms |
13400 KB |
Output is correct |
8 |
Correct |
5 ms |
13392 KB |
Output is correct |
9 |
Correct |
3 ms |
13404 KB |
Output is correct |
10 |
Correct |
3 ms |
13392 KB |
Output is correct |
11 |
Correct |
4 ms |
13404 KB |
Output is correct |
12 |
Correct |
4 ms |
13404 KB |
Output is correct |
13 |
Correct |
4 ms |
13400 KB |
Output is correct |
14 |
Correct |
4 ms |
13404 KB |
Output is correct |
15 |
Correct |
5 ms |
13660 KB |
Output is correct |
16 |
Correct |
5 ms |
13404 KB |
Output is correct |
17 |
Correct |
4 ms |
13404 KB |
Output is correct |
18 |
Correct |
5 ms |
13400 KB |
Output is correct |
19 |
Correct |
5 ms |
13572 KB |
Output is correct |
20 |
Correct |
4 ms |
13404 KB |
Output is correct |
21 |
Correct |
4 ms |
13656 KB |
Output is correct |
22 |
Correct |
4 ms |
13404 KB |
Output is correct |
23 |
Correct |
5 ms |
13404 KB |
Output is correct |
24 |
Correct |
4 ms |
13404 KB |
Output is correct |
25 |
Correct |
6 ms |
13400 KB |
Output is correct |
26 |
Correct |
6 ms |
13404 KB |
Output is correct |
27 |
Correct |
4 ms |
13404 KB |
Output is correct |
28 |
Correct |
4 ms |
13404 KB |
Output is correct |
29 |
Correct |
4 ms |
13404 KB |
Output is correct |
30 |
Correct |
5 ms |
13404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
368 ms |
38500 KB |
Output is correct |
2 |
Correct |
184 ms |
39064 KB |
Output is correct |
3 |
Correct |
359 ms |
38404 KB |
Output is correct |
4 |
Correct |
159 ms |
38992 KB |
Output is correct |
5 |
Correct |
367 ms |
36008 KB |
Output is correct |
6 |
Correct |
177 ms |
39036 KB |
Output is correct |
7 |
Correct |
404 ms |
36120 KB |
Output is correct |
8 |
Correct |
168 ms |
38716 KB |
Output is correct |
9 |
Correct |
525 ms |
34628 KB |
Output is correct |
10 |
Correct |
537 ms |
32688 KB |
Output is correct |
11 |
Correct |
425 ms |
35064 KB |
Output is correct |
12 |
Correct |
450 ms |
37124 KB |
Output is correct |
13 |
Correct |
474 ms |
32596 KB |
Output is correct |
14 |
Correct |
429 ms |
37368 KB |
Output is correct |
15 |
Correct |
465 ms |
37104 KB |
Output is correct |
16 |
Correct |
428 ms |
33124 KB |
Output is correct |
17 |
Correct |
453 ms |
33568 KB |
Output is correct |
18 |
Correct |
483 ms |
33732 KB |
Output is correct |
19 |
Correct |
437 ms |
36268 KB |
Output is correct |
20 |
Correct |
508 ms |
38228 KB |
Output is correct |
21 |
Correct |
3 ms |
13144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
13404 KB |
Output is correct |
2 |
Correct |
4 ms |
13144 KB |
Output is correct |
3 |
Correct |
2 ms |
13404 KB |
Output is correct |
4 |
Correct |
3 ms |
13396 KB |
Output is correct |
5 |
Correct |
3 ms |
13404 KB |
Output is correct |
6 |
Correct |
3 ms |
13404 KB |
Output is correct |
7 |
Correct |
4 ms |
13400 KB |
Output is correct |
8 |
Correct |
5 ms |
13392 KB |
Output is correct |
9 |
Correct |
3 ms |
13404 KB |
Output is correct |
10 |
Correct |
3 ms |
13392 KB |
Output is correct |
11 |
Correct |
4 ms |
13404 KB |
Output is correct |
12 |
Correct |
4 ms |
13404 KB |
Output is correct |
13 |
Correct |
4 ms |
13400 KB |
Output is correct |
14 |
Correct |
4 ms |
13404 KB |
Output is correct |
15 |
Correct |
5 ms |
13660 KB |
Output is correct |
16 |
Correct |
5 ms |
13404 KB |
Output is correct |
17 |
Correct |
4 ms |
13404 KB |
Output is correct |
18 |
Correct |
5 ms |
13400 KB |
Output is correct |
19 |
Correct |
5 ms |
13572 KB |
Output is correct |
20 |
Correct |
4 ms |
13404 KB |
Output is correct |
21 |
Correct |
4 ms |
13656 KB |
Output is correct |
22 |
Correct |
4 ms |
13404 KB |
Output is correct |
23 |
Correct |
5 ms |
13404 KB |
Output is correct |
24 |
Correct |
4 ms |
13404 KB |
Output is correct |
25 |
Correct |
6 ms |
13400 KB |
Output is correct |
26 |
Correct |
6 ms |
13404 KB |
Output is correct |
27 |
Correct |
4 ms |
13404 KB |
Output is correct |
28 |
Correct |
4 ms |
13404 KB |
Output is correct |
29 |
Correct |
4 ms |
13404 KB |
Output is correct |
30 |
Correct |
5 ms |
13404 KB |
Output is correct |
31 |
Correct |
368 ms |
38500 KB |
Output is correct |
32 |
Correct |
184 ms |
39064 KB |
Output is correct |
33 |
Correct |
359 ms |
38404 KB |
Output is correct |
34 |
Correct |
159 ms |
38992 KB |
Output is correct |
35 |
Correct |
367 ms |
36008 KB |
Output is correct |
36 |
Correct |
177 ms |
39036 KB |
Output is correct |
37 |
Correct |
404 ms |
36120 KB |
Output is correct |
38 |
Correct |
168 ms |
38716 KB |
Output is correct |
39 |
Correct |
525 ms |
34628 KB |
Output is correct |
40 |
Correct |
537 ms |
32688 KB |
Output is correct |
41 |
Correct |
425 ms |
35064 KB |
Output is correct |
42 |
Correct |
450 ms |
37124 KB |
Output is correct |
43 |
Correct |
474 ms |
32596 KB |
Output is correct |
44 |
Correct |
429 ms |
37368 KB |
Output is correct |
45 |
Correct |
465 ms |
37104 KB |
Output is correct |
46 |
Correct |
428 ms |
33124 KB |
Output is correct |
47 |
Correct |
453 ms |
33568 KB |
Output is correct |
48 |
Correct |
483 ms |
33732 KB |
Output is correct |
49 |
Correct |
437 ms |
36268 KB |
Output is correct |
50 |
Correct |
508 ms |
38228 KB |
Output is correct |
51 |
Correct |
3 ms |
13144 KB |
Output is correct |
52 |
Correct |
256 ms |
24916 KB |
Output is correct |
53 |
Correct |
320 ms |
24952 KB |
Output is correct |
54 |
Correct |
250 ms |
24952 KB |
Output is correct |
55 |
Correct |
290 ms |
24960 KB |
Output is correct |
56 |
Correct |
301 ms |
24956 KB |
Output is correct |
57 |
Correct |
270 ms |
24956 KB |
Output is correct |
58 |
Correct |
287 ms |
27944 KB |
Output is correct |
59 |
Correct |
223 ms |
28352 KB |
Output is correct |
60 |
Correct |
278 ms |
27740 KB |
Output is correct |
61 |
Correct |
340 ms |
27412 KB |
Output is correct |
62 |
Correct |
159 ms |
38920 KB |
Output is correct |
63 |
Correct |
167 ms |
39060 KB |
Output is correct |
64 |
Correct |
159 ms |
38740 KB |
Output is correct |
65 |
Correct |
164 ms |
39032 KB |
Output is correct |
66 |
Correct |
278 ms |
32116 KB |
Output is correct |
67 |
Correct |
200 ms |
31940 KB |
Output is correct |
68 |
Correct |
228 ms |
31948 KB |
Output is correct |
69 |
Correct |
234 ms |
32104 KB |
Output is correct |
70 |
Correct |
217 ms |
31944 KB |
Output is correct |
71 |
Correct |
200 ms |
31952 KB |
Output is correct |
72 |
Correct |
201 ms |
32020 KB |
Output is correct |
73 |
Correct |
204 ms |
31268 KB |
Output is correct |
74 |
Correct |
216 ms |
31948 KB |
Output is correct |
75 |
Correct |
227 ms |
32052 KB |
Output is correct |
76 |
Correct |
337 ms |
31060 KB |
Output is correct |
77 |
Correct |
326 ms |
29592 KB |
Output is correct |
78 |
Correct |
425 ms |
33308 KB |
Output is correct |
79 |
Correct |
472 ms |
32120 KB |
Output is correct |
80 |
Correct |
434 ms |
37544 KB |
Output is correct |
81 |
Correct |
515 ms |
34796 KB |
Output is correct |
82 |
Correct |
457 ms |
35080 KB |
Output is correct |
83 |
Correct |
446 ms |
32084 KB |
Output is correct |
84 |
Correct |
473 ms |
36976 KB |
Output is correct |
85 |
Correct |
415 ms |
35692 KB |
Output is correct |
86 |
Correct |
423 ms |
31980 KB |
Output is correct |
87 |
Correct |
478 ms |
33364 KB |
Output is correct |
88 |
Correct |
393 ms |
34192 KB |
Output is correct |
89 |
Correct |
337 ms |
30964 KB |
Output is correct |
90 |
Correct |
332 ms |
30816 KB |
Output is correct |
91 |
Correct |
361 ms |
32596 KB |
Output is correct |
92 |
Correct |
341 ms |
31716 KB |
Output is correct |
93 |
Correct |
340 ms |
31420 KB |
Output is correct |
94 |
Correct |
331 ms |
30772 KB |
Output is correct |
95 |
Correct |
371 ms |
32080 KB |
Output is correct |
96 |
Correct |
380 ms |
30948 KB |
Output is correct |
97 |
Correct |
339 ms |
32892 KB |
Output is correct |