#include <bits/stdc++.h>
using namespace std;
const int N=200050;
int n,k,a[N],sz[N],cnt[N],par[N],ans;
bool was[N],clr[N];
vector<int> E[N],ids[N];
void DFS(int u,int p){sz[u]=1;for(int v:E[u])if(!was[v]&&v!=p)DFS(v,u),sz[u]+=sz[v];}
int Find(int u,int p,int n){for(int v:E[u])if(!was[v]&&v!=p&&sz[v]*2>=n)return Find(v,u,n);return u;}
int Find(int u){DFS(u,u);u=Find(u,u,sz[u]);was[u]=1;return u;}
void Clr(int u,int p){
par[u]=p;
cnt[a[u]]=0;
clr[a[u]]=false;
for(int v:E[u])if(!was[v]&&v!=p)Clr(v,u);
}
void DFS1(int u,int p){
cnt[a[u]]++;
for(int v:E[u])if(!was[v]&&v!=p)DFS1(v,u);
}
void Solve(int u){
u=Find(u);
Clr(u,u);
DFS1(u,u);
int cc=0;
stack<int> stk;
stk.push(a[u]);
while(!stk.empty()){
int x=stk.top();
stk.pop();
if(clr[x])continue;
if(cnt[x]!=ids[x].size()){cc=1e9;break;}
clr[x]=true;
cc++;
for(int v:ids[x])stk.push(a[par[v]]);
}
ans=min(ans,cc-1);
for(int v:E[u])if(!was[v])Solve(v);
}
int main(){
scanf("%i%i",&n,&k);
for(int i=1;i<n;i++){
int u,v;scanf("%i%i",&u,&v);
E[u].push_back(v);
E[v].push_back(u);
}
for(int i=1;i<=n;i++)scanf("%i",&a[i]),ids[a[i]].push_back(i);
ans=1e9;
Solve(1);
printf("%i\n",ans);
return 0;
}
Compilation message
capital_city.cpp: In function 'void Solve(int)':
capital_city.cpp:31:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
31 | if(cnt[x]!=ids[x].size()){cc=1e9;break;}
| ~~~~~~^~~~~~~~~~~~~~~
capital_city.cpp: In function 'int main()':
capital_city.cpp:40:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | scanf("%i%i",&n,&k);
| ~~~~~^~~~~~~~~~~~~~
capital_city.cpp:42:16: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
42 | int u,v;scanf("%i%i",&u,&v);
| ~~~~~^~~~~~~~~~~~~~
capital_city.cpp:46:28: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
46 | for(int i=1;i<=n;i++)scanf("%i",&a[i]),ids[a[i]].push_back(i);
| ~~~~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9704 KB |
Output is correct |
4 |
Correct |
5 ms |
9708 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
6 ms |
9684 KB |
Output is correct |
7 |
Correct |
7 ms |
9684 KB |
Output is correct |
8 |
Correct |
7 ms |
9708 KB |
Output is correct |
9 |
Correct |
6 ms |
9708 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9704 KB |
Output is correct |
4 |
Correct |
5 ms |
9708 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
6 ms |
9684 KB |
Output is correct |
7 |
Correct |
7 ms |
9684 KB |
Output is correct |
8 |
Correct |
7 ms |
9708 KB |
Output is correct |
9 |
Correct |
6 ms |
9708 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
6 ms |
9812 KB |
Output is correct |
12 |
Correct |
7 ms |
9940 KB |
Output is correct |
13 |
Correct |
7 ms |
9848 KB |
Output is correct |
14 |
Correct |
7 ms |
9852 KB |
Output is correct |
15 |
Correct |
8 ms |
9892 KB |
Output is correct |
16 |
Correct |
9 ms |
9812 KB |
Output is correct |
17 |
Correct |
7 ms |
9856 KB |
Output is correct |
18 |
Correct |
6 ms |
9924 KB |
Output is correct |
19 |
Correct |
7 ms |
9912 KB |
Output is correct |
20 |
Correct |
7 ms |
9940 KB |
Output is correct |
21 |
Correct |
7 ms |
9892 KB |
Output is correct |
22 |
Correct |
7 ms |
9960 KB |
Output is correct |
23 |
Correct |
6 ms |
9940 KB |
Output is correct |
24 |
Correct |
8 ms |
9924 KB |
Output is correct |
25 |
Correct |
8 ms |
9924 KB |
Output is correct |
26 |
Correct |
7 ms |
9916 KB |
Output is correct |
27 |
Correct |
8 ms |
9972 KB |
Output is correct |
28 |
Correct |
8 ms |
9940 KB |
Output is correct |
29 |
Correct |
7 ms |
9852 KB |
Output is correct |
30 |
Correct |
9 ms |
9852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
586 ms |
40948 KB |
Output is correct |
2 |
Correct |
270 ms |
41452 KB |
Output is correct |
3 |
Correct |
593 ms |
40532 KB |
Output is correct |
4 |
Correct |
236 ms |
41404 KB |
Output is correct |
5 |
Correct |
556 ms |
37640 KB |
Output is correct |
6 |
Correct |
244 ms |
41496 KB |
Output is correct |
7 |
Correct |
574 ms |
37892 KB |
Output is correct |
8 |
Correct |
238 ms |
40996 KB |
Output is correct |
9 |
Correct |
693 ms |
36148 KB |
Output is correct |
10 |
Correct |
706 ms |
33764 KB |
Output is correct |
11 |
Correct |
680 ms |
36544 KB |
Output is correct |
12 |
Correct |
745 ms |
39296 KB |
Output is correct |
13 |
Correct |
705 ms |
33396 KB |
Output is correct |
14 |
Correct |
727 ms |
39612 KB |
Output is correct |
15 |
Correct |
675 ms |
39212 KB |
Output is correct |
16 |
Correct |
794 ms |
34200 KB |
Output is correct |
17 |
Correct |
675 ms |
34852 KB |
Output is correct |
18 |
Correct |
719 ms |
34920 KB |
Output is correct |
19 |
Correct |
732 ms |
38124 KB |
Output is correct |
20 |
Correct |
665 ms |
40324 KB |
Output is correct |
21 |
Correct |
7 ms |
9684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9704 KB |
Output is correct |
4 |
Correct |
5 ms |
9708 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
6 ms |
9684 KB |
Output is correct |
7 |
Correct |
7 ms |
9684 KB |
Output is correct |
8 |
Correct |
7 ms |
9708 KB |
Output is correct |
9 |
Correct |
6 ms |
9708 KB |
Output is correct |
10 |
Correct |
5 ms |
9684 KB |
Output is correct |
11 |
Correct |
6 ms |
9812 KB |
Output is correct |
12 |
Correct |
7 ms |
9940 KB |
Output is correct |
13 |
Correct |
7 ms |
9848 KB |
Output is correct |
14 |
Correct |
7 ms |
9852 KB |
Output is correct |
15 |
Correct |
8 ms |
9892 KB |
Output is correct |
16 |
Correct |
9 ms |
9812 KB |
Output is correct |
17 |
Correct |
7 ms |
9856 KB |
Output is correct |
18 |
Correct |
6 ms |
9924 KB |
Output is correct |
19 |
Correct |
7 ms |
9912 KB |
Output is correct |
20 |
Correct |
7 ms |
9940 KB |
Output is correct |
21 |
Correct |
7 ms |
9892 KB |
Output is correct |
22 |
Correct |
7 ms |
9960 KB |
Output is correct |
23 |
Correct |
6 ms |
9940 KB |
Output is correct |
24 |
Correct |
8 ms |
9924 KB |
Output is correct |
25 |
Correct |
8 ms |
9924 KB |
Output is correct |
26 |
Correct |
7 ms |
9916 KB |
Output is correct |
27 |
Correct |
8 ms |
9972 KB |
Output is correct |
28 |
Correct |
8 ms |
9940 KB |
Output is correct |
29 |
Correct |
7 ms |
9852 KB |
Output is correct |
30 |
Correct |
9 ms |
9852 KB |
Output is correct |
31 |
Correct |
586 ms |
40948 KB |
Output is correct |
32 |
Correct |
270 ms |
41452 KB |
Output is correct |
33 |
Correct |
593 ms |
40532 KB |
Output is correct |
34 |
Correct |
236 ms |
41404 KB |
Output is correct |
35 |
Correct |
556 ms |
37640 KB |
Output is correct |
36 |
Correct |
244 ms |
41496 KB |
Output is correct |
37 |
Correct |
574 ms |
37892 KB |
Output is correct |
38 |
Correct |
238 ms |
40996 KB |
Output is correct |
39 |
Correct |
693 ms |
36148 KB |
Output is correct |
40 |
Correct |
706 ms |
33764 KB |
Output is correct |
41 |
Correct |
680 ms |
36544 KB |
Output is correct |
42 |
Correct |
745 ms |
39296 KB |
Output is correct |
43 |
Correct |
705 ms |
33396 KB |
Output is correct |
44 |
Correct |
727 ms |
39612 KB |
Output is correct |
45 |
Correct |
675 ms |
39212 KB |
Output is correct |
46 |
Correct |
794 ms |
34200 KB |
Output is correct |
47 |
Correct |
675 ms |
34852 KB |
Output is correct |
48 |
Correct |
719 ms |
34920 KB |
Output is correct |
49 |
Correct |
732 ms |
38124 KB |
Output is correct |
50 |
Correct |
665 ms |
40324 KB |
Output is correct |
51 |
Correct |
7 ms |
9684 KB |
Output is correct |
52 |
Correct |
435 ms |
24536 KB |
Output is correct |
53 |
Correct |
518 ms |
24436 KB |
Output is correct |
54 |
Correct |
510 ms |
24432 KB |
Output is correct |
55 |
Correct |
504 ms |
24332 KB |
Output is correct |
56 |
Correct |
520 ms |
24384 KB |
Output is correct |
57 |
Correct |
444 ms |
24420 KB |
Output is correct |
58 |
Correct |
447 ms |
27800 KB |
Output is correct |
59 |
Correct |
450 ms |
28384 KB |
Output is correct |
60 |
Correct |
545 ms |
27504 KB |
Output is correct |
61 |
Correct |
567 ms |
27304 KB |
Output is correct |
62 |
Correct |
223 ms |
41416 KB |
Output is correct |
63 |
Correct |
245 ms |
41328 KB |
Output is correct |
64 |
Correct |
259 ms |
41148 KB |
Output is correct |
65 |
Correct |
216 ms |
41384 KB |
Output is correct |
66 |
Correct |
392 ms |
32964 KB |
Output is correct |
67 |
Correct |
331 ms |
32940 KB |
Output is correct |
68 |
Correct |
392 ms |
33060 KB |
Output is correct |
69 |
Correct |
344 ms |
33148 KB |
Output is correct |
70 |
Correct |
396 ms |
33004 KB |
Output is correct |
71 |
Correct |
362 ms |
32936 KB |
Output is correct |
72 |
Correct |
390 ms |
33124 KB |
Output is correct |
73 |
Correct |
386 ms |
32068 KB |
Output is correct |
74 |
Correct |
399 ms |
32984 KB |
Output is correct |
75 |
Correct |
339 ms |
32968 KB |
Output is correct |
76 |
Correct |
562 ms |
31492 KB |
Output is correct |
77 |
Correct |
546 ms |
29672 KB |
Output is correct |
78 |
Correct |
635 ms |
34728 KB |
Output is correct |
79 |
Correct |
678 ms |
32716 KB |
Output is correct |
80 |
Correct |
741 ms |
39836 KB |
Output is correct |
81 |
Correct |
719 ms |
36336 KB |
Output is correct |
82 |
Correct |
736 ms |
36456 KB |
Output is correct |
83 |
Correct |
680 ms |
33068 KB |
Output is correct |
84 |
Correct |
694 ms |
38988 KB |
Output is correct |
85 |
Correct |
730 ms |
37400 KB |
Output is correct |
86 |
Correct |
695 ms |
32816 KB |
Output is correct |
87 |
Correct |
676 ms |
34460 KB |
Output is correct |
88 |
Correct |
542 ms |
35568 KB |
Output is correct |
89 |
Correct |
605 ms |
31648 KB |
Output is correct |
90 |
Correct |
570 ms |
31436 KB |
Output is correct |
91 |
Correct |
601 ms |
33756 KB |
Output is correct |
92 |
Correct |
594 ms |
32388 KB |
Output is correct |
93 |
Correct |
657 ms |
32160 KB |
Output is correct |
94 |
Correct |
553 ms |
31484 KB |
Output is correct |
95 |
Correct |
532 ms |
33004 KB |
Output is correct |
96 |
Correct |
593 ms |
31692 KB |
Output is correct |
97 |
Correct |
624 ms |
33640 KB |
Output is correct |